forked from projectfluent/python-fluent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_reference.py
More file actions
58 lines (39 loc) · 1.55 KB
/
test_reference.py
File metadata and controls
58 lines (39 loc) · 1.55 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
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
fixtures = os.path.join(os.path.dirname(__file__), "fixtures_reference")
class TestReferenceMeta(type):
def __new__(mcs, name, bases, attrs):
def remove_untested(obj):
if obj["type"] == "Junk":
obj["annotations"] = []
if "span" in obj:
del obj["span"]
return obj
def gen_test(file_name):
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 = read_file(ast_path)
ast = parse(source)
self.assertEqual(ast.to_json(remove_untested), json.loads(expected))
return test
for f in os.listdir(fixtures):
file_name, ext = os.path.splitext(f)
if ext != ".ftl":
continue
# Skip fixtures which are known to differ between the reference
# parser and the tooling parser.
if file_name in ("leading_dots", "variant_lists"):
continue
test_name = f"test_{file_name}"
attrs[test_name] = gen_test(file_name)
return type.__new__(mcs, name, bases, attrs)
class TestReference(unittest.TestCase, metaclass=TestReferenceMeta):
maxDiff = None