OILS / asdl / gen_python_test.py View on Github | oilshell.org

180 lines, 114 significant
1#!/usr/bin/env python2
2"""gen_python_test.py."""
3from __future__ import print_function
4
5import unittest
6
7from asdl import pybase
8
9from _devbuild.gen import typed_demo_asdl
10from _devbuild.gen.typed_arith_asdl import arith_expr, arith_expr_e
11from _devbuild.gen.typed_demo_asdl import (source_location, flag_type,
12 flag_type_str, op_id_e, op_id_str,
13 cflow, cflow_t, Strings, Dicts,
14 Maybes, op_array)
15
16
17class ArithAstTest(unittest.TestCase):
18
19 def testPrettyPrint(self):
20 s = op_id_str(op_id_e.Plus)
21 self.assertEqual('op_id.Plus', s)
22 print(s)
23
24 obj = flag_type.Bool
25 s = flag_type_str(obj.tag())
26
27 self.assertEqual('flag_type.Bool', s)
28 print(s)
29
30 s = flag_type_str(obj.tag(), dot=False)
31 self.assertEqual('Bool', s)
32 print(s)
33
34 def testStringDefaults(self):
35 st = Strings('', '')
36 self.assertEqual('', st.required)
37 self.assertEqual('', st.optional)
38
39 # ZERO ARG "constructor" (static method)
40 st = Strings.CreateNull(alloc_lists=True)
41 self.assertEqual('', st.required)
42 self.assertEqual(None, st.optional)
43
44 # N arg constructor with None
45 st = Strings('', None)
46 self.assertEqual('', st.required)
47 self.assertEqual(None, st.optional)
48
49 def testArrayDefault(self):
50 obj = op_array.CreateNull()
51 self.assertEqual(None, obj.ops)
52
53 obj = op_array.CreateNull(alloc_lists=True)
54 self.assertEqual([], obj.ops)
55
56 def testMapDefault(self):
57 # TODO: alloc_dicts=True
58 obj = Dicts.CreateNull(alloc_lists=True)
59 self.assertEqual(None, obj.ss)
60
61 def testOptionalDefault(self):
62 obj = Maybes.CreateNull(alloc_lists=True)
63
64 # These are None
65 self.assertEqual(None, obj.op)
66 self.assertEqual(None, obj.arg)
67
68 def testFieldDefaults(self):
69 s = arith_expr.Slice.CreateNull(alloc_lists=True)
70 s.a = arith_expr.Var('foo')
71 self.assertEqual(None, s.begin)
72 self.assertEqual(None, s.end)
73 self.assertEqual(None, s.stride)
74 print(s)
75
76 func = arith_expr.FuncCall.CreateNull(alloc_lists=True)
77 func.name = 'f'
78 self.assertEqual([], func.args)
79 print(func)
80
81 def testExtraFields(self):
82 v = arith_expr.Var('z')
83
84 # TODO: Attach this to EVERY non-simple constructor? Those are subclasses
85 # of Sum types.
86 # What about product types?
87 #print(v.xspans)
88
89 def testConstructor(self):
90 n1 = arith_expr.Var('x')
91 n2 = arith_expr.Var(name='y')
92 print(n1)
93 print(n2)
94
95 # Not good because not assigned?
96 n3 = arith_expr.Var.CreateNull(alloc_lists=True)
97
98 # NOTE: You cannot instantiate a product type directly? It's just used for
99 # type checking. What about OCaml?
100 # That means you just need to create classes for the records (arith_expr.Constructor).
101 # They all descend from Obj. They don't need
102
103 n3 = arith_expr.Var.CreateNull(alloc_lists=True)
104 try:
105 n4 = arith_expr.Var('x', name='X')
106 except TypeError as e:
107 pass
108 else:
109 raise AssertionError("Should have failed")
110
111 def testProductType(self):
112 print()
113 print('-- PRODUCT --')
114 print()
115
116 s = source_location.CreateNull(alloc_lists=True)
117 s.path = 'hi'
118 s.line = 1
119 s.col = 2
120 s.length = 3
121 print(s)
122
123 # Implementation detail for dynamic type checking
124 assert isinstance(s, pybase.CompoundObj)
125
126 def testSimpleSumType(self):
127 # TODO: Should be op_id_i.Plus -- instance
128 # Should be op_id_s.Plus
129
130 print()
131 print('-- SIMPLE SUM --')
132 print()
133
134 o = op_id_e.Plus
135 assert isinstance(o, pybase.SimpleObj)
136
137 def testCompoundSumType(self):
138 print()
139 print('-- COMPOUND SUM --')
140 print()
141
142 c = cflow.Break
143 assert isinstance(c, typed_demo_asdl.cflow__Break)
144 assert isinstance(c, cflow_t)
145 assert isinstance(c, pybase.CompoundObj)
146
147 def testOtherTypes(self):
148 c = arith_expr.Const(66)
149 print(c)
150
151 sl = arith_expr.Slice(
152 arith_expr.Const(1),
153 arith_expr.Const(5),
154 arith_expr.Const(2),
155 arith_expr.Const(3),
156 )
157 print(sl)
158
159 print((op_id_e.Plus))
160
161 # Class for sum type
162 print(arith_expr)
163
164 # Invalid because only half were assigned
165 #print(arith_expr.Binary(op_id_e.Plus, arith_expr.Const(5)))
166
167 n = arith_expr.Binary.CreateNull(alloc_lists=True)
168 #n.CheckUnassigned()
169 n.op_id = op_id_e.Plus
170 n.left = arith_expr.Const(5)
171 #n.CheckUnassigned()
172 n.right = arith_expr.Const(6)
173 #n.CheckUnassigned()
174
175 self.assertEqual(arith_expr_e.Const, c.tag())
176 self.assertEqual(arith_expr_e.Binary, n.tag())
177
178
179if __name__ == '__main__':
180 unittest.main()