Skip to content

Commit 7888ba1

Browse files
committed
Add versions of the syntax tests without spans
Prior to this change, all of the syntax tests covered the behaviour of the parser when with_spans is set to True. This change updates the test generation to create a version of each test which tests the parser when with_spans is set to False. To achieve this, we strip the span information from the expected file (rather than needing to maintain two files).
1 parent bc5b0e5 commit 7888ba1

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

fluent.syntax/tests/syntax/test_structure.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,47 @@ def read_file(path):
1212
return text
1313

1414

15+
def without_spans(expected):
16+
"""
17+
Given an expected JSON fragment with span information, recursively replace all of the spans
18+
with None.
19+
"""
20+
if isinstance(expected, dict):
21+
result = {}
22+
for key, value in expected.items():
23+
if key == "span":
24+
result[key] = None
25+
else:
26+
result[key] = without_spans(value)
27+
28+
return result
29+
elif isinstance(expected, list):
30+
return [without_spans(item) for item in expected]
31+
else:
32+
# We have been passed something which would not have span information in it
33+
return expected
34+
35+
1536
fixtures = os.path.join(os.path.dirname(__file__), "fixtures_structure")
1637

1738

1839
class TestStructureMeta(type):
1940
def __new__(mcs, name, bases, attrs):
2041

21-
def gen_test(file_name):
42+
def gen_test(file_name, with_spans):
2243
def test(self):
2344
ftl_path = os.path.join(fixtures, file_name + ".ftl")
2445
ast_path = os.path.join(fixtures, file_name + ".json")
2546

2647
source = read_file(ftl_path)
27-
expected = read_file(ast_path)
48+
expected = json.loads(read_file(ast_path))
49+
50+
if not with_spans:
51+
expected = without_spans(expected)
2852

29-
ast = parse(source)
53+
ast = parse(source, with_spans=with_spans)
3054

31-
self.assertEqual(ast.to_json(), json.loads(expected))
55+
self.assertEqual(ast.to_json(), expected)
3256

3357
return test
3458

@@ -38,8 +62,8 @@ def test(self):
3862
if ext != ".ftl":
3963
continue
4064

41-
test_name = f"test_{file_name}"
42-
attrs[test_name] = gen_test(file_name)
65+
attrs[f"test_{file_name}_with_spans"] = gen_test(file_name, with_spans=True)
66+
attrs[f"test_{file_name}_without_spans"] = gen_test(file_name, with_spans=False)
4367

4468
return type.__new__(mcs, name, bases, attrs)
4569

0 commit comments

Comments
 (0)