-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_metaL.py
492 lines (393 loc) · 14.1 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import pytest
from metaL import *
class TestObject:
def test_hello(self):
hello = Object("hello")
world = Object("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>'
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
class TestOperators:
def test_add(self):
add = Op('+') // Integer(137) // Integer(349)
assert add.test() ==\
'\n<op:+>' +\
'\n\t0: <integer:137>' +\
'\n\t1: <integer:349>'
assert add.eval(vm).test() ==\
'\n<integer:486>'
def test_sub(self):
sub = Op('-') // Integer(1000) // Integer(334)
assert sub.test() ==\
'\n<op:->' +\
'\n\t0: <integer:1000>' +\
'\n\t1: <integer:334>'
assert sub.eval(vm).test() ==\
'\n<integer:666>'
# class TestFrame:
# def test_empty(self):
# assert Frame('').test() == '<frame:>'
# def test_hello(self):
# hello = Frame('hello')
# assert hello.test() == '<frame:hello>'
# def test_dump_pad(self):
# hello = Frame('hello')
# assert hello._pad(2) == '\n' + '\t' * 2
# def test_dump_val(self):
# hello = Frame('hello')
# assert hello._val() == 'hello'
# def test_dump_head(self):
# hello = Frame('hello')
# assert re.match(r'<frame:hello> @[0-9a-f]+', hello.head())
# def test_dump(self):
# hello = Frame('hello')
# world = Frame('world')
# hello << world // world
# print(hello)
# h = r' @[0-9a-f]+'
# a = r'\n<frame:hello>' + h
# b = r'\n\s+world = <frame:world>' + h
# c = r'\n\s+<frame:world>' + h
# assert re.match(a + b + c, hello.__repr__())
# def test_operator_set(self):
# hello = Frame('hello')
# world = Frame('world')
# hello['key'] = world
# assert hello.test() == '<frame:hello>\tkey <frame:world>'
# def test_operator_get(self):
# hello = Frame('hello')
# world = Frame('world')
# hello['key'] = world
# assert hello['key'].test() == '<frame:world>'
# def test_operator_shift(self):
# hello = Frame('hello')
# world = Frame('world')
# hello << world
# assert hello.test() == '<frame:hello>\tworld <frame:world>'
# def test_operator_haskey(self):
# hello = Frame('hello')
# world = Frame('world')
# hello['key'] = world
# hello << world
# assert hello.has('key')
# assert hello.has('world')
# assert not hello.has('nothing')
# def test_operator_push(self):
# hello = Frame('hello')
# world = Frame('world')
# hello // hello // world
# assert hello.test() == '<frame:hello>\t<frame:hello> _/\t<frame:world>'
# def test_stack_dropall(self):
# hello = Frame('hello')
# world = Frame('world')
# hello // hello // world
# assert hello.dropall().test() == '<frame:hello>'
# def test_stack_push(self):
# hello = Frame('hello')
# world = Frame('world')
# hello.push(world)
# assert hello.test() == '<frame:hello>\t<frame:world>'
# def test_stack_pop(self):
# hello = Frame('hello')
# a = Frame('a')
# b = Frame('b')
# hello // a // b
# assert hello.test() == '<frame:hello>\t<frame:a>\t<frame:b>'
# assert hello.pop().test() == '<frame:b>'
# assert hello.test() == '<frame:hello>\t<frame:a>'
# def test_stack_pip(self):
# hello = Frame('hello')
# a = Frame('a')
# b = Frame('b')
# hello // a // b
# assert hello.test() == '<frame:hello>\t<frame:a>\t<frame:b>'
# assert hello.pip().test() == '<frame:a>'
# assert hello.test() == '<frame:hello>\t<frame:b>'
# def test_stack_top(self):
# hello = Frame('hello')
# a = Frame('a')
# b = Frame('b')
# hello // a // b
# assert hello.test() == '<frame:hello>\t<frame:a>\t<frame:b>'
# assert hello.top().test() == '<frame:b>'
# assert hello.test() == '<frame:hello>\t<frame:a>\t<frame:b>'
# def test_stack_tip(self):
# hello = Frame('hello')
# a = Frame('a')
# b = Frame('b')
# hello // a // b
# assert hello.test() == '<frame:hello>\t<frame:a>\t<frame:b>'
# assert hello.tip().test() == '<frame:a>'
# assert hello.test() == '<frame:hello>\t<frame:a>\t<frame:b>'
# def test_stack_dup(self):
# hello = Frame('hello')
# a = Frame('a')
# hello // a
# assert hello.dup().test() == '<frame:hello>\t<frame:a>\t<frame:a> _/'
# def test_stack_drop(self):
# hello = Frame('hello')
# a = Frame('a')
# b = Frame('b')
# hello // a // b
# assert hello.drop().test() == '<frame:hello>\t<frame:a>'
# def test_stack_swap(self):
# hello = Frame('hello')
# a = Frame('a')
# b = Frame('b')
# hello // a // b
# assert hello.swap().test() == '<frame:hello>\t<frame:b>\t<frame:a>'
# def test_stack_over(self):
# hello = Frame('hello')
# a = Frame('a')
# b = Frame('b')
# hello // a // b
# assert hello.over().test() == '<frame:hello>\t<frame:a>\t<frame:b>\t<frame:a> _/'
# def test_stack_press(self):
# hello = Frame('hello')
# a = Frame('a')
# b = Frame('b')
# hello // a // b
# assert hello.press().test() == '<frame:hello>\t<frame:b>'
# def test_eval(self):
# hello = Frame('hello')
# assert hello.eval(hello).test() == '<frame:hello>\t<frame:hello> _/'
# ##################################################################### primitive
# class TestString:
# def test_String(self):
# str = String("hello\n\tworld")
# h = r' @[0-9a-f]+'
# assert re.match(r'<string:hello\\n\\tworld>' + h, str.head())
# class TestSymbol:
# def test_Symbol(self):
# assert True
# class TestNumber:
# def test_Number(self):
# num = Number('-012.340')
# assert type(num.val) == float
# assert num.test() == '<number:-12.34>'
# def test_add(self):
# a = Number(-12.34)
# b = Number(+56.78)
# c = String('')
# assert (a + b).test() == '<number:44.44>'
# try:
# a + c
# assert False
# except TypeError:
# assert True
# def test_sub(self):
# a = Number(-12.34)
# b = Number(+56.78)
# c = String('')
# assert (a - b).test() == '<number:-69.12>'
# try:
# a - c
# assert False
# except TypeError:
# assert True
# def test_mul(self):
# a = Number(-12.34)
# b = Number(+56.78)
# c = String('')
# assert (a * b).test() == '<number:-700.6652>'
# try:
# a * c
# assert False
# except TypeError:
# assert True
# def test_div(self):
# a = Number(-12.34)
# b = Number(+56.78)
# c = String('')
# assert (a / b).test() == '<number:-0.2173300457907714>'
# assert (a.__div__(b)).test() == '<number:-0.2173300457907714>'
# try:
# a / c
# assert False
# except TypeError:
# assert True
# def test_mod(self):
# a = Number(-12.34)
# b = Number(+56.78)
# c = String('')
# assert (a % b).test() == '<number:44.44>'
# try:
# a % c
# assert False
# except TypeError:
# assert True
# def test_neg(self):
# a = Number(-12.34)
# assert (-a).test() == '<number:12.34>'
# def test_int(self):
# a = Number(-12.34)
# b = int(a)
# assert b == -12
# def test_toint(self):
# a = Number(-12.34)
# b = a.toint()
# assert b.test() == '<integer:-12>'
# class TestInteger:
# def test_Integer(self):
# num = Integer('-01234')
# assert type(num.val) == int
# assert num.test() == '<integer:-1234>'
# def test_int(self):
# a = Integer(-12.34)
# b = int(a)
# assert b == -12
# def test_toint(self):
# a = Integer(-12.34)
# b = a.toint()
# assert b.test() == '<integer:-12>'
# class TestHex:
# def test_hex(self):
# hex = Hex('0xDeadBeef')
# assert type(hex.val) == int
# assert hex.test() == '<hex:0xDEADBEEF>'
# def test_int(self):
# a = Hex('0xDeadBeef')
# b = int(a)
# assert b == 3735928559
# def test_toint(self):
# a = Hex('0xDeadBeef')
# b = a.toint()
# assert b.test() == '<integer:3735928559>'
# class TestBin:
# def test_bin(self):
# bin = Bin('0b1101')
# assert type(bin.val) == int
# assert bin.test() == '<bin:0b1101>'
# def test_int(self):
# a = Bin('0b1101')
# b = int(a)
# assert b == 13
# def test_toint(self):
# a = Bin('0b1101')
# b = a.toint()
# assert b.test() == '<integer:13>'
# ############################################################## no-syntax parser
# class TestPLY:
# def test_eol(self):
# lexer = lex.lex()
# lexer.input("'\\t\\r\nx'")
# assert lexer.token().test() == r'<string:\t\r\nx>'
# def test_inc(self):
# vm // String('.inc /dev/null')
# INTERPRET(vm)
# assert vm.test(voc=False) == '<vm:metaL>'
# ################################################################### interpreter
# class TestInterpreter:
# def test_empty(self):
# DOT(vm)
# assert vm.test(voc=False) == '<vm:metaL>'
# vm // String('')
# assert vm.test(voc=False) == '<vm:metaL>\t<string:>'
# INTERPRET(vm)
# assert vm.test(voc=False) == '<vm:metaL>'
# def test_symbol(self):
# DOT(vm)
# assert vm.test(voc=False) == '<vm:metaL>'
# vm // String('`quoted')
# assert vm.test(voc=False) == '<vm:metaL>\t<string:`quoted>'
# INTERPRET(vm)
# assert vm.test(voc=False) == '<vm:metaL>\t<symbol:quoted>'
# def test_string(self):
# DOT(vm)
# assert vm.test(voc=False) == '<vm:metaL>'
# vm // String("\t\r\n'test\\t\\r\\nstring'\n")
# assert vm.test(voc=False) == \
# "<vm:metaL>\t<string:\\t\\r\\n'test\\t\\r\\nstring'\\n>"
# INTERPRET(vm)
# assert vm.test(voc=False) == '<vm:metaL>\t<string:test\\t\\r\\nstring>'
# def test_numbers(self):
# DOT(vm)
# assert vm.test(voc=False) == '<vm:metaL>'
# vm // String('-01 +02.30 -04e+5 0xDeadBeef 0b1101')
# assert vm.test(voc=False) == \
# '<vm:metaL>\t<string:-01 +02.30 -04e+5 0xDeadBeef 0b1101>'
# INTERPRET(vm)
# assert vm.test(voc=False) == \
# '<vm:metaL>\t<integer:-1>\t<number:2.3>\t<number:-400000.0>' +\
# '\t<hex:0xDEADBEEF>\t<bin:0b1101>'
# ###################################################################### compiler
# class TestCompiler:
# def no_test_compile(self):
# DOT(vm)
# assert vm.test(voc=False) == '<vm:metaL>'
# vm // String('[ ] { }')
# assert vm.test(voc=False) == '<vm:metaL>\t<string:[ ] { }>'
# INTERPRET(vm)
# assert vm.test(voc=False) == '<vm:metaL>\t<symbol:quoted>'
# ################################################################## web platform
# class TestWeb:
# def test_create(self):
# web = Web('test')
# assert web.test(voc=False) == '<web:test>'
# assert web['IP'].test() == '<ip:127.0.0.1>'
# assert web['PORT'].test() == '<port:8888>'
# assert web['font'].test() == '<font:monospace>\tsize <size:3mm>'
# assert web['back'].test() == '<color:black>'
# assert web['color'].test() == '<color:lightgreen>'
# ################################################################### system init
# class TestInit:
# def test_init(self):
# sys.argv = [sys.argv[0], '/dev/null']
# INIT(vm)
# ######################################################## Python code generation
# class TestPython:
# def test_Frame(self):
# hello = Frame('hello')
# a = Frame('a')
# b = Frame('b')
# hello // a // b
# assert hello.py(None) == r"Frame('hello') // Frame('a') // Frame('b')"
# def test_Primitive(self):
# hello = Primitive('prim')
# a = Frame('a')
# b = Frame('b')
# hello // a // b
# assert hello.py(hello) == r"prim"
# def test_String(self):
# str = String("hello\n\tworld")
# assert str.py(None) == r"'hello\n\tworld'"
# def test_Integer(self):
# num = Integer('-01234')
# assert num.py(None) == r"-1234"
# def test_Hex(self):
# hex = Hex('0xDeadBeef')
# assert hex.py(None) == r"0xDEADBEEF"
# def test_Bin(self):
# bin = Bin('0b1101')
# assert bin.py(None) == r"0b1101"
# def test_import(self):
# os = Import('os')
# assert os.test() == '<import:os>'
# assert 'sys' in os.module.__dict__
# ################################################# embedded C/C++ code generator
# class TestCpp:
# def test_frame(self):
# assert C('C99').test() == '<c:C99>'
# assert Cpp('C++03').test() == '<cpp:C++03>'