forked from projectfluent/python-fluent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_structure.py
More file actions
71 lines (49 loc) · 1.96 KB
/
test_structure.py
File metadata and controls
71 lines (49 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import json
import os
import unittest
from fluent.syntax import parse
def read_file(path):
with open(path, "r", encoding="utf-8", newline="\n") as file:
text = file.read()
return text
def without_spans(expected):
"""
Given an expected JSON fragment with span information, recursively replace all of the spans
with None.
"""
if isinstance(expected, dict):
result = {}
for key, value in expected.items():
if key == "span":
result[key] = None
else:
result[key] = without_spans(value)
return result
elif isinstance(expected, list):
return [without_spans(item) for item in expected]
else:
# We have been passed something which would not have span information in it
return expected
fixtures = os.path.join(os.path.dirname(__file__), "fixtures_structure")
class TestStructureMeta(type):
def __new__(mcs, name, bases, attrs):
def gen_test(file_name, with_spans):
def test(self):
ftl_path = os.path.join(fixtures, file_name + ".ftl")
ast_path = os.path.join(fixtures, file_name + ".json")
source = read_file(ftl_path)
expected = json.loads(read_file(ast_path))
if not with_spans:
expected = without_spans(expected)
ast = parse(source, with_spans=with_spans)
self.assertEqual(ast.to_json(), expected)
return test
for f in os.listdir(fixtures):
file_name, ext = os.path.splitext(f)
if ext != ".ftl":
continue
attrs[f"test_{file_name}_with_spans"] = gen_test(file_name, with_spans=True)
attrs[f"test_{file_name}_without_spans"] = gen_test(file_name, with_spans=False)
return type.__new__(mcs, name, bases, attrs)
class TestStructure(unittest.TestCase, metaclass=TestStructureMeta):
maxDiff = None