Skip to content

Commit df5ef40

Browse files
authored
Respect the with_spans parser option for annotations (#201)
* 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). * Respect the with_spans setting for Annotations Prior to this change, Annotations would always have span information, irrespective of the value of the with_spans argument to the parser. This change changes the behaviour to only include span information if with_spans if set to True.
1 parent 88f9da2 commit df5ef40

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

fluent.syntax/fluent/syntax/parser.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ def get_entry_or_junk(self, ps: FluentParserStream) -> ast.EntryType:
133133
annot = ast.Annotation(
134134
err.code, list(err.args) if err.args else None, err.message
135135
)
136-
annot.add_span(error_index, error_index)
136+
if self.with_spans:
137+
annot.add_span(error_index, error_index)
137138
junk.add_annotation(annot)
138139
return junk
139140

fluent.syntax/tests/syntax/test_entry.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_return_junk(self):
6565
"arguments": ["="],
6666
"code": "E0003",
6767
"message": 'Expected token: "="',
68-
"span": {"end": 23, "start": 23, "type": "Span"},
68+
"span": None,
6969
"type": "Annotation",
7070
}
7171
],
@@ -111,7 +111,7 @@ def test_do_not_ignore_invalid_comments(self):
111111
"arguments": [" "],
112112
"code": "E0003",
113113
"message": 'Expected token: " "',
114-
"span": {"end": 21, "start": 21, "type": "Span"},
114+
"span": None,
115115
"type": "Annotation",
116116
}
117117
],

fluent.syntax/tests/syntax/test_structure.py

+30-6
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)