Skip to content

Commit b631243

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 b631243

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
@@ -11,24 +11,48 @@ def read_file(path):
1111
text = file.read()
1212
return text
1313

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

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)