File: test_ast_jy.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (156 lines) | stat: -rw-r--r-- 4,265 bytes parent folder | download | duplicates (8)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Extra unittests for ast in Jython.  Look to integrate into CPython in the
future"""

import unittest
import ast
from test import test_support

def srcExprToTree(source, kind='exec'):
    return compile(source, '<module>', kind, ast.PyCF_ONLY_AST)

class TestCompile(unittest.TestCase):

    def test_compile_ast(self):
        node = srcExprToTree("1/2")
        compile(node, "<string>", 'exec')

    def test_alias_trim(self):
        node = srcExprToTree("import os. path")
        self.assertEquals(node.body[0].names[0].name, "os.path")

        node = srcExprToTree("import os .path")
        self.assertEquals(node.body[0].names[0].name, "os.path")

        node = srcExprToTree("import os . path")
        self.assertEquals(node.body[0].names[0].name, "os.path")

    def test_cmpop(self):
        expr = srcExprToTree('a < b < c', 'eval')
        compare = expr.body
        self.assert_(isinstance(compare.ops[0], ast.Lt))
        self.assert_(isinstance(compare.comparators[0], ast.Name))
        self.assert_(isinstance(compare.ops[1], ast.Lt))
        self.assert_(isinstance(compare.comparators[1], ast.Name))
        self.assert_(isinstance(compare.ops[1:][0], ast.Lt))
        self.assert_(isinstance(compare.comparators[1:][0], ast.Name))
        z = zip( compare.ops[1:], compare.comparators[1:])
        self.assert_(isinstance(z[0][0], ast.Lt))
        self.assert_(isinstance(z[0][1], ast.Name))

    def test_empty_init(self):
        # Jython 2.5.0 did not allow empty constructors for many ast node types
        # but CPython ast nodes do allow this.  For the moment, I don't see a
        # reason to allow construction of the super types (like ast.AST and
        # ast.stmt) as well as the op types that are implemented as enums in
        # Jython (like boolop), but I've left them in but commented out for
        # now.  We may need them in the future since CPython allows this, but
        # it may fall under implementation detail.

        #ast.AST()
        ast.Add()
        ast.And()
        ast.Assert()
        ast.Assign()
        ast.Attribute()
        ast.AugAssign()
        ast.AugLoad()
        ast.AugStore()
        ast.BinOp()
        ast.BitAnd()
        ast.BitOr()
        ast.BitXor()
        ast.BoolOp()
        ast.Break()
        ast.Call()
        ast.ClassDef()
        ast.Compare()
        ast.Continue()
        ast.Del()
        ast.Delete()
        ast.Dict()
        ast.Div()
        ast.Ellipsis()
        ast.Eq()
        ast.Exec()
        ast.Expr()
        ast.Expression()
        ast.ExtSlice()
        ast.FloorDiv()
        ast.For()
        ast.FunctionDef()
        ast.GeneratorExp()
        ast.Global()
        ast.Gt()
        ast.GtE()
        ast.If()
        ast.IfExp()
        ast.Import()
        ast.ImportFrom()
        ast.In()
        ast.Index()
        ast.Interactive()
        ast.Invert()
        ast.Is()
        ast.IsNot()
        ast.LShift()
        ast.Lambda()
        ast.List()
        ast.ListComp()
        ast.Load()
        ast.Lt()
        ast.LtE()
        ast.Mod()
        ast.Module()
        ast.Mult()
        ast.Name()
        ast.Not()
        ast.NotEq()
        ast.NotIn()
        ast.Num()
        ast.Or()
        ast.Param()
        ast.Pass()
        ast.Pow()
        ast.Print()
        ast.RShift()
        ast.Raise()
        ast.Repr()
        ast.Return()
        ast.Slice()
        ast.Store()
        ast.Str()
        ast.Sub()
        ast.Subscript()
        ast.Suite()
        ast.TryExcept()
        ast.TryFinally()
        ast.Tuple()
        ast.UAdd()
        ast.USub()
        ast.UnaryOp()
        ast.While()
        ast.With()
        ast.Yield()
        ast.alias()
        ast.arguments()
        #ast.boolop()
        #ast.cmpop()
        ast.comprehension()
        #ast.excepthandler()
        #ast.expr()
        #ast.expr_context()
        ast.keyword()
        #ast.mod()
        #ast.operator()
        #ast.slice()
        #ast.stmt()
        #ast.unaryop()

#==============================================================================

def test_main(verbose=None):
    test_classes = [TestCompile]
    test_support.run_unittest(*test_classes)

if __name__ == "__main__":
    test_main(verbose=True)