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