ยปCore Development>Code coverage>Lib/test/test_asdl_parser.py

Python code coverage for Lib/test/test_asdl_parser.py

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