File: ast_test.py

package info (click to toggle)
python-tatsu 5.13.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: python: 10,202; makefile: 54
file content (62 lines) | stat: -rw-r--r-- 1,444 bytes parent folder | download | duplicates (2)
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
import pickle
import unittest

from tatsu.ast import AST


def test_ast_pickling():
    a = AST(parseinfo='Some parseinfo')
    b = pickle.loads(pickle.dumps(a))
    assert a == b


class ASTTests(unittest.TestCase):
    def test_ast(self):
        ast = AST()
        self.assertEqual([], list(ast.items()))
        self.assertTrue(hasattr(ast, '__json__'))

    def test_init(self):
        ast = AST()
        data = list(
            reversed([(0, 0), (1, 2), (2, 4), (3, 6), (4, 8), (5, 10)]),
        )
        for k, v in data:
            ast[k] = v
        self.assertEqual(data, list(ast.items()))

    def test_empty(self):
        ast = AST()
        self.assertIsNone(ast.name)

    def test_add(self):
        ast = AST()
        ast['name'] = 'hello'
        self.assertIsNotNone(ast.name)
        self.assertEqual('hello', ast.name)

        ast['name'] = 'world'
        self.assertEqual(['hello', 'world'], ast.name)

        ast['value'] = 1
        self.assertEqual(1, ast.value)

    def test_iter(self):
        ast = AST()
        ast['name'] = 'hello'
        ast['name'] = 'world'
        ast['value'] = 1
        self.assertEqual(['name', 'value'], list(ast))
        self.assertEqual([['hello', 'world'], 1], list(ast.values()))


def suite():
    return unittest.TestLoader().loadTestsFromTestCase(ASTTests)


def main():
    unittest.TextTestRunner(verbosity=2).run(suite())


if __name__ == '__main__':
    main()