1 | n/a | """Tests for the asdl parser in Parser/asdl.py""" |
---|
2 | n/a | |
---|
3 | n/a | import importlib.machinery |
---|
4 | n/a | import os |
---|
5 | n/a | from os.path import dirname |
---|
6 | n/a | import sys |
---|
7 | n/a | import sysconfig |
---|
8 | n/a | import unittest |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | # This test is only relevant for from-source builds of Python. |
---|
12 | n/a | if not sysconfig.is_python_build(): |
---|
13 | n/a | raise unittest.SkipTest('test irrelevant for an installed Python') |
---|
14 | n/a | |
---|
15 | n/a | src_base = dirname(dirname(dirname(__file__))) |
---|
16 | n/a | parser_dir = os.path.join(src_base, 'Parser') |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | class TestAsdlParser(unittest.TestCase): |
---|
20 | n/a | @classmethod |
---|
21 | n/a | def setUpClass(cls): |
---|
22 | n/a | # Loads the asdl module dynamically, since it's not in a real importable |
---|
23 | n/a | # package. |
---|
24 | n/a | # Parses Python.asdl into an ast.Module and run the check on it. |
---|
25 | n/a | # There's no need to do this for each test method, hence setUpClass. |
---|
26 | n/a | sys.path.insert(0, parser_dir) |
---|
27 | n/a | loader = importlib.machinery.SourceFileLoader( |
---|
28 | n/a | 'asdl', os.path.join(parser_dir, 'asdl.py')) |
---|
29 | n/a | cls.asdl = loader.load_module() |
---|
30 | n/a | cls.mod = cls.asdl.parse(os.path.join(parser_dir, 'Python.asdl')) |
---|
31 | n/a | cls.assertTrue(cls.asdl.check(cls.mod), 'Module validation failed') |
---|
32 | n/a | |
---|
33 | n/a | @classmethod |
---|
34 | n/a | def tearDownClass(cls): |
---|
35 | n/a | del sys.path[0] |
---|
36 | n/a | |
---|
37 | n/a | def setUp(self): |
---|
38 | n/a | # alias stuff from the class, for convenience |
---|
39 | n/a | self.asdl = TestAsdlParser.asdl |
---|
40 | n/a | self.mod = TestAsdlParser.mod |
---|
41 | n/a | self.types = self.mod.types |
---|
42 | n/a | |
---|
43 | n/a | def test_module(self): |
---|
44 | n/a | self.assertEqual(self.mod.name, 'Python') |
---|
45 | n/a | self.assertIn('stmt', self.types) |
---|
46 | n/a | self.assertIn('expr', self.types) |
---|
47 | n/a | self.assertIn('mod', self.types) |
---|
48 | n/a | |
---|
49 | n/a | def test_definitions(self): |
---|
50 | n/a | defs = self.mod.dfns |
---|
51 | n/a | self.assertIsInstance(defs[0], self.asdl.Type) |
---|
52 | n/a | self.assertIsInstance(defs[0].value, self.asdl.Sum) |
---|
53 | n/a | |
---|
54 | n/a | self.assertIsInstance(self.types['withitem'], self.asdl.Product) |
---|
55 | n/a | self.assertIsInstance(self.types['alias'], self.asdl.Product) |
---|
56 | n/a | |
---|
57 | n/a | def test_product(self): |
---|
58 | n/a | alias = self.types['alias'] |
---|
59 | n/a | self.assertEqual( |
---|
60 | n/a | str(alias), |
---|
61 | n/a | 'Product([Field(identifier, name), Field(identifier, asname, opt=True)])') |
---|
62 | n/a | |
---|
63 | n/a | def test_attributes(self): |
---|
64 | n/a | stmt = self.types['stmt'] |
---|
65 | n/a | self.assertEqual(len(stmt.attributes), 2) |
---|
66 | n/a | self.assertEqual(str(stmt.attributes[0]), 'Field(int, lineno)') |
---|
67 | n/a | self.assertEqual(str(stmt.attributes[1]), 'Field(int, col_offset)') |
---|
68 | n/a | |
---|
69 | n/a | def test_constructor_fields(self): |
---|
70 | n/a | ehandler = self.types['excepthandler'] |
---|
71 | n/a | self.assertEqual(len(ehandler.types), 1) |
---|
72 | n/a | self.assertEqual(len(ehandler.attributes), 2) |
---|
73 | n/a | |
---|
74 | n/a | cons = ehandler.types[0] |
---|
75 | n/a | self.assertIsInstance(cons, self.asdl.Constructor) |
---|
76 | n/a | self.assertEqual(len(cons.fields), 3) |
---|
77 | n/a | |
---|
78 | n/a | f0 = cons.fields[0] |
---|
79 | n/a | self.assertEqual(f0.type, 'expr') |
---|
80 | n/a | self.assertEqual(f0.name, 'type') |
---|
81 | n/a | self.assertTrue(f0.opt) |
---|
82 | n/a | |
---|
83 | n/a | f1 = cons.fields[1] |
---|
84 | n/a | self.assertEqual(f1.type, 'identifier') |
---|
85 | n/a | self.assertEqual(f1.name, 'name') |
---|
86 | n/a | self.assertTrue(f1.opt) |
---|
87 | n/a | |
---|
88 | n/a | f2 = cons.fields[2] |
---|
89 | n/a | self.assertEqual(f2.type, 'stmt') |
---|
90 | n/a | self.assertEqual(f2.name, 'body') |
---|
91 | n/a | self.assertFalse(f2.opt) |
---|
92 | n/a | self.assertTrue(f2.seq) |
---|
93 | n/a | |
---|
94 | n/a | def test_visitor(self): |
---|
95 | n/a | class CustomVisitor(self.asdl.VisitorBase): |
---|
96 | n/a | def __init__(self): |
---|
97 | n/a | super().__init__() |
---|
98 | n/a | self.names_with_seq = [] |
---|
99 | n/a | |
---|
100 | n/a | def visitModule(self, mod): |
---|
101 | n/a | for dfn in mod.dfns: |
---|
102 | n/a | self.visit(dfn) |
---|
103 | n/a | |
---|
104 | n/a | def visitType(self, type): |
---|
105 | n/a | self.visit(type.value) |
---|
106 | n/a | |
---|
107 | n/a | def visitSum(self, sum): |
---|
108 | n/a | for t in sum.types: |
---|
109 | n/a | self.visit(t) |
---|
110 | n/a | |
---|
111 | n/a | def visitConstructor(self, cons): |
---|
112 | n/a | for f in cons.fields: |
---|
113 | n/a | if f.seq: |
---|
114 | n/a | self.names_with_seq.append(cons.name) |
---|
115 | n/a | |
---|
116 | n/a | v = CustomVisitor() |
---|
117 | n/a | v.visit(self.types['mod']) |
---|
118 | n/a | self.assertEqual(v.names_with_seq, ['Module', 'Interactive', 'Suite']) |
---|
119 | n/a | |
---|
120 | n/a | |
---|
121 | n/a | if __name__ == '__main__': |
---|
122 | n/a | unittest.main() |
---|