-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_metaL.py
245 lines (204 loc) · 6.65 KB
/
test_metaL.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
## @file
## @brief `metaL` core test bundle
## @defgroup test tests
import pytest
from metaL import *
## @ref object
## @ingroup test
class TestObject:
def hello(self): return Object('Hello')
def world(self): return Object('World')
def test_hello(self):
assert self.hello().test() ==\
'\n<object:Hello>'
def test_world(self):
assert (self.hello() // self.world()).test() ==\
'\n<object:Hello>' +\
'\n\t0: <object:World>'
def test_leftright(self):
hello = self.hello()
world = self.world()
left = Object('left')
right = Object('right')
assert (hello // world << left >> right).test() ==\
'\n<object:Hello>' +\
'\n\tobject = <object:left>' +\
'\n\tright = <object:right>' +\
'\n\t0: <object:World>'
## @ref prim
## @ingroup test
class TestPrimitive:
def test_number(self):
assert Number(486).test() ==\
'\n<number:486.0>'
def test_integer(self):
assert Integer('-01').test() ==\
'\n<integer:-1>'
def test_number_dot(self):
assert Number('+02.30').test() ==\
'\n<number:2.3>'
def test_hex(self):
x = Hex('0xDeadBeef')
assert x.test() == '\n<hex:0xdeadbeef>'
assert x.val == 3735928559
def test_bin(self):
x = Bin('0b1101')
assert x.test() == '\n<bin:0b1101>'
assert x.val == 13
## @ref lexer
## @ingroup test
class TestLexer:
def test_none(self):
lexer.input('')
assert lexer.token() == None
def test_comment(self):
lexer.input('# comment')
assert lexer.token() == None
def test_spaces(self):
lexer.input(' \t\r\n')
assert lexer.token() != None # nl
def test_name(self):
lexer.input('name')
token = lexer.token()
assert token and token.value.test() ==\
'\n<name:name>'
## @ref parser
## @ingroup test
class TestParser:
def test_none(self):
assert parser.parse('').test() ==\
'\n<ast:>'
def test_name(self):
ast = parser.parse('MODULE')
assert ast.test() ==\
'\n<ast:>\n\t0: <name:MODULE>'
assert ast.eval(vm).test() ==\
'\n<ast:>\n\t0: <module:metaL>'
def test_vector_empty(self):
empty = parser.parse('[]')
assert empty.test() ==\
'\n<ast:>\n\t0: <vector:>'
assert empty.eval(vm).test() ==\
'\n<ast:>\n\t0: <vector:>'
def test_vector_single(self):
single = parser.parse('[MODULE]')
assert single.test() ==\
'\n<ast:>' +\
'\n\t0: <vector:>' +\
'\n\t\t0: <name:MODULE>'
assert single.eval(vm).test() ==\
'\n<ast:>' +\
'\n\t0: <vector:>' +\
'\n\t\t0: <module:metaL>'
def test_vector_multy(self):
multy = parser.parse('[1,2.3,MODULE]')
assert multy.test() ==\
'\n<ast:>\n\t0: <vector:>' +\
'\n\t\t0: <integer:1>' +\
'\n\t\t1: <number:2.3>' +\
'\n\t\t2: <name:MODULE>'
assert multy.eval(vm).test() ==\
'\n<ast:>\n\t0: <vector:>' +\
'\n\t\t0: <integer:1>' +\
'\n\t\t1: <number:2.3>' +\
'\n\t\t2: <module:metaL>'
def test_numbers(self):
ast = parser.parse('''
# numbers
[ -01 , +02.30 , -4e+5 , +6.7e-8 , 0xDeadBeef , 0b1101 ]
''')
# ast
assert ast.test() ==\
'\n<ast:>\n\t0: <vector:>' +\
'\n\t\t0: <op:->\n\t\t\t0: <integer:1>' +\
'\n\t\t1: <op:+>\n\t\t\t0: <number:2.3>' +\
'\n\t\t2: <op:->\n\t\t\t0: <number:400000.0>' +\
'\n\t\t3: <op:+>\n\t\t\t0: <number:6.7e-08>' +\
'\n\t\t4: <hex:0xdeadbeef>' +\
'\n\t\t5: <bin:0b1101>'
# evaled
assert ast.eval(vm).test() ==\
'\n<ast:>\n\t0: <vector:>' +\
'\n\t\t0: <integer:-1>' +\
'\n\t\t1: <number:2.3>' +\
'\n\t\t2: <number:-400000.0>' +\
'\n\t\t3: <number:6.7e-08>' +\
'\n\t\t4: <hex:0xdeadbeef>' +\
'\n\t\t5: <bin:0b1101>'
class TestIntMath():
def test_plus(self):
ast = parser.parse('+486')[0]
assert ast.test() ==\
'\n<op:+>' +\
'\n\t0: <integer:486>'
assert ast.eval(vm).test() ==\
'\n<integer:486>'
def test_minus(self):
ast = parser.parse('- 486')[0] # spaces allowed
assert ast.test() ==\
'\n<op:->' +\
'\n\t0: <integer:486>'
assert ast.eval(vm).test() ==\
'\n<integer:-486>'
def test_add(self):
ast = parser.parse('137+349')[0]
assert ast.test() ==\
'\n<op:+>' +\
'\n\t0: <integer:137>' +\
'\n\t1: <integer:349>'
assert ast.eval(vm).test() ==\
'\n<integer:486>'
def test_sub(self):
ast = parser.parse('1000-334')[0]
assert ast.test() ==\
'\n<op:->' +\
'\n\t0: <integer:1000>' +\
'\n\t1: <integer:334>'
assert ast.eval(vm).test() ==\
'\n<integer:666>'
def test_mul(self):
ast = parser.parse('5*99')[0]
assert ast.test() ==\
'\n<op:*>' +\
'\n\t0: <integer:5>' +\
'\n\t1: <integer:99>'
assert ast.eval(vm).test() ==\
'\n<integer:495>'
def test_div(self):
ast = parser.parse('10/5')[0]
assert ast.test() ==\
'\n<op:/>' +\
'\n\t0: <integer:10>' +\
'\n\t1: <integer:5>'
assert ast.eval(vm).test() ==\
'\n<integer:2>'
## @ref parser
## @ingroup test
class TestFn:
def test_args(self):
assert Args([]).test() == '\n<args:>'
assert argc.test() == '\n<cint:argc>'
assert argc.file() == 'int argc'
assert argv.test() == '\n<arg:char *argv[]>'
assert argv.file() == 'char *argv[]'
assert main.test() ==\
'\n<cfn:main>' +\
'\n\targs = <args:>' +\
'\n\t\t0: <cint:argc>' +\
'\n\t\t1: <arg:char *argv[]>' +\
'\n\tret = <cint:0>'
def test_empty(self):
ast = parser.parse('{}')
assert ast.test() == \
'\n<ast:>' +\
'\n\t0: <fn:>' +\
'\n\t\targs = <args:>' +\
'\n\t\tret = <nil:>'
def test_single(self):
ast = parser.parse('{123}')
assert ast.test() == \
'\n<ast:>' +\
'\n\t0: <fn:>' +\
'\n\t\targs = <args:>' +\
'\n\t\tret = <nil:>' +\
'\n\t\t0: <integer:123>'