File: test_unparser.py

package info (click to toggle)
python-gast 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: python: 2,208; sh: 7; makefile: 3
file content (37 lines) | stat: -rw-r--r-- 897 bytes parent folder | download
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
import unittest

import ast
import gast
import sys


class UnparserTestCase(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)
        self.maxDiff = None

    def assertUnparse(self, code):
        normalized_code = ast.unparse(ast.parse(code))
        tree = gast.parse(normalized_code)
        compile(gast.gast_to_ast(tree), '<test>', 'exec')
        unparsed = gast.unparse(tree)
        self.assertEqual(normalized_code, unparsed)

    def test_FunctionDef(self):
        self.assertUnparse('def foo(x, y): return x, y')

    def test_BinaryOp(self):
        self.assertUnparse('1 + 3')

    if sys.version_info >= (3, 12):

        def test_TypeParameter(self):
            self.assertUnparse('type x[T] = list[T]')


if sys.version_info < (3, 9):
    del UnparserTestCase

if __name__ == '__main__':
    unittest.main()