-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
616 lines (462 loc) · 23.5 KB
/
tests.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
from contextlib import contextmanager
from io import StringIO
import unittest
import ast
import calc
import sys
@contextmanager
def captured_output():
new_out, new_err = StringIO(), StringIO()
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = new_out, new_err
yield sys.stdout, sys.stderr
finally:
sys.stdout, sys.stderr = old_out, old_err
class test_2_1(unittest.TestCase):
# obsługa tokenów dla liczb całkowitych
def test_1_1_real_token_number(self):
token_input = "begin 2; end"
ast_output = """{"P_expression_number" :["int",2]}"""
with captured_output() as (out, err):
x, _, _ = calc.parse(token_input)
self.assertEqual(repr(x.block.blocks.block.casual_statement.operation.expression), ast_output)
self.assertEqual(out.getvalue(), '')
# obsługa tokenów dla liczb rzeczywistych
def test_1_2_float_token_number(self):
token_input = "begin 2.0; end"
ast_output = """{"P_expression_number" :["float",2.0]}"""
with captured_output() as (out, err):
x, _, _ = calc.parse(token_input)
self.assertEqual(repr(x.block.blocks.block.casual_statement.operation.expression), ast_output)
self.assertEqual(out.getvalue(), '')
# obsługa tokenów dla funkcji specjalnych sin, cos, itd
def test_2_1_sin(self):
token_suite = {
"begin sin(2); end" : "sin",
"begin sin2; end" : "sin",
"begin sin(2.0); end" : "sin",
"begin sin 2; end" : "sin"
}
for expected_input, expected_output in token_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.expression.op, expected_output)
self.assertEqual(out.getvalue(), '')
def test_2_2_cos(self):
token_suite = {
"begin cos(2); end" : "cos",
"begin cos2; end" : "cos",
"begin cos(2.0); end" : "cos",
"begin cos 2; end" : "cos"
}
for expected_input, expected_output in token_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.expression.op, expected_output)
self.assertEqual(out.getvalue(), '')
def test_2_3_unary_operations(self):
token_suite = {
"begin exp(2); end" : "exp",
"begin log(2); end" : "log",
"begin sqrt(2); end" : "sqrt"
}
for expected_input, expected_output in token_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.expression.op, expected_output)
self.assertEqual(out.getvalue(), '')
# obsługa tokenów dla operatora potęgowania, itd
def test_3_1_power(self):
token_suite = {
"begin 2 ** 4; end" : "**",
"begin 3**3; end" : "**",
"begin 2 **3; end" : "**",
"begin 2** 3; end" : "**"
}
for expected_input, expected_output in token_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.expression.op, expected_output)
self.assertEqual(out.getvalue(), '')
def test_3_2_addition(self):
token_suite = {
"begin 2 + 4; end" : "+",
"begin 3+3; end" : "+",
"begin 2 +3; end" : "+",
"begin 2+ 3; end" : "+"
}
for expected_input, expected_output in token_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.expression.op, expected_output)
self.assertEqual(out.getvalue(), '')
def test_3_3_binary_operations(self):
token_suite = {
"begin 2 - 4; end" : "-",
"begin 2- 4; end" : "-",
"begin 2 + -4; end" : "+",
"begin 2 * 4; end" : "*",
"begin 2* 4; end" : "*",
"begin 2 *4; end" : "*",
"begin 2 / 4; end" : "/",
"begin 2/ 4; end" : "/",
"begin 2 /4; end" : "/",
}
for expected_input, expected_output in token_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.expression.op, expected_output)
self.assertEqual(out.getvalue(), '')
# automatyczna koretka błędów w tokenach
def test_4_1_auto_token_correction(self):
# not implemented
pass
def assertThose(testcase, text_input, expect):
with captured_output() as (out, err):
x, _, _ = calc.parse(text_input)
return testcase.assertEqual(out.getvalue(), expect)
class test_2_2(unittest.TestCase):
# potegowanie
def test_1_1_power(self):
assertThose(self, "begin print(2 ** 4); end", "16\n")
assertThose(self, "begin print(2**4); end", "16\n")
# sin
def test_1_2_sin(self):
assertThose(self, "begin print(sin(3.1415926535)); end", "8.979318433952318e-11\n")
assertThose(self, "begin print(sin3.1415926535); end", "8.979318433952318e-11\n")
# cos
def test_1_3_cos(self):
assertThose(self, "begin print(cos(3.1415926535)); end", "-1.0\n")
assertThose(self, "begin print(cos3.1415926535); end", "-1.0\n")
# equality_true
def test_1_4_equality_true(self):
assertThose(self, "begin print(1 == 1); end", "True\n")
assertThose(self, "begin print(1==1); end", "True\n")
# equality_false
def test_1_5_equality_false(self):
assertThose(self, "begin print(1 == 4); end", "False\n")
assertThose(self, "begin print(1==4); end", "False\n")
def test_1_6_unequality_true(self):
assertThose(self, "begin print(1 != 4); end", "True\n")
assertThose(self, "begin print(1!=4); end", "True\n")
def test_1_7_unequality_false(self):
assertThose(self, "begin print(1 != 1); end", "False\n")
assertThose(self, "begin print(1!=1); end", "False\n")
def test_1_8_greater_true(self):
assertThose(self, "begin print(3 > 1); end", "True\n")
assertThose(self, "begin print(3>1); end", "True\n")
def test_1_9_greater_false(self):
assertThose(self, "begin print(1 > 3); end", "False\n")
assertThose(self, "begin print(1>3); end", "False\n")
def test_1_10_lesser_true(self):
assertThose(self, "begin print(1 < 3); end", "True\n")
assertThose(self, "begin print(1<3); end", "True\n")
def test_1_11_lesser_false(self):
assertThose(self, "begin print(3 < 1); end", "False\n")
assertThose(self, "begin print(3<1); end", "False\n")
def test_1_12_greater_or_equal_true(self):
assertThose(self, "begin print(3 >= 1); end", "True\n")
assertThose(self, "begin print(3>=1); end", "True\n")
def test_1_13_greater_or_equal_false(self):
assertThose(self, "begin print(1 >= 3); end", "False\n")
assertThose(self, "begin print(1>=3); end", "False\n")
def test_1_14_lesser_or_equal_true(self):
assertThose(self, "begin print(1 <= 3); end", "True\n")
assertThose(self, "begin print(1<=3); end", "True\n")
def test_1_15_lesser_or_equal_false(self):
assertThose(self, "begin print(3 <= 1); end", "False\n")
assertThose(self, "begin print(3<=1); end", "False\n")
def test_1_16_negative_sign(self):
assertThose(self, "begin print(-3); end", "-3\n")
assertThose(self, "begin print(-3 - -3); end", "0\n")
def test_2_1_negative_sign(self):
assertThose(self, "begin print(1); print(2); print(3); end", "1\n2\n3\n")
assertThose(self, "begin print(1);print(2);print(3); end", "1\n2\n3\n")
def test_3_1_continue_evalutaion(self):
assertThose(self, "begin print(1); asdfaerasdf; print(3); end", "1\nUndefined name 'asdfaerasdf'\nUndefined name 'asdfaerasdf'\n3\n")
def test_4_1_continue_evalutaion(self):
pass
def test_5_1_if(self):
assertThose(self, "begin if(True) print(3) else print(4); end", "3\n")
assertThose(self, "begin if(1 == 1) print(3) else print(4); end", "3\n")
assertThose(self, "begin if(1 == 2) print(3) else print(4); end", "4\n")
assertThose(self, "begin if(False) print(3) else print(4); end", "4\n")
def test_5_2_while(self):
assertThose(self, "begin int i = 0; while(i < 1) i = i + 1; end", "")
def test_5_3_for(self):
assertThose(self, "begin for(int i = 0; i < 3; i = i + 1) print(i); end", "0\n1\n2\n")
class test_2_3(unittest.TestCase):
# wizualizacja drzewa składniowego
def test_1_1_ast_simple_addition(self):
token_input = "begin print(2+2); end"
pass_function = lambda : "dummy function"
ast_output = ast.P_start(
# block
ast.P_block(
# blocks
ast.P_blocks(
# block
ast.P_block(
# blocks
None,
# casual_statements
ast.P_casual_statements(
# operation
ast.P_print(
# fun
ast.P_statement_expr(
# fun
pass_function(),
# expression
ast.P_expression_binop(
# fun
pass_function(),
# expr1
ast.P_expression_number(
# fun
pass_function(),
# typeo
"int",
# val
2
),
# op
"+",
# expr2
ast.P_expression_number(
# fun
pass_function(),
# typeo
"int",
# val
2
)
)
)
)
)
),
# blocks
None
),
# casual_statements
None
)
)
ast_repr = """{"P_start" :[{"P_block" :[{"P_blocks" :[{"P_block" :["None",{"P_casual_statements" :[{"P_print"}]}]},"None"]},"None"]}]}"""
with captured_output() as (out, err):
x, _, _ = calc.parse(token_input)
self.assertEqual(repr(x), repr(ast_output))
self.assertEqual(repr(x), ast_repr)
def test_1_2_ast_complex_addition(self):
token_input = "begin int i = 2; define add(int a) = a + 5; add(i); end"
ast_repr = ('' +
'{"P_start" :[' +
'{"P_block" :[' +
'{"P_blocks" :[' +
'{"P_block" :[' +
'"None",' +
'{"P_casual_statements" :[' +
'{"P_statement_assign" :[' +
'"int",' +
'"i",' +
'{"P_statement_expr" :[' +
'{"P_expression_number" :[' +
'"int",' +
'2]}' +
']}' +
']}' +
']}' +
']},' +
'{"P_blocks" :[' +
'{"P_block" :[' +
'"None",' +
'{"P_casual_statements" :[' +
'{"P_define" :[' +
'"add",' +
'{"P_params" :[' +
'"int",' +
'"a",' +
'"None"' +
']},' +
'{"P_statement_expr" :[' +
'{"P_expression_binop" :[' +
'{"P_expression_name" :[' +
'"a"' +
']},' +
'"+",' +
'{"P_expression_number" :[' +
'"int",' +
'5' +
']}' +
']}' +
']}' +
']}' +
']}' +
']},' +
'{"P_blocks" :[' +
'{"P_block" :[' +
'"None",' +
'{"P_casual_statements" :[' +
'{"P_statement_expr" :[' +
'{"P_call_function" :[' +
'"add",' +
'{"P_statements" :[' +
'{"P_statement_expr" :[' +
'{"P_expression_name" :[' +
'"i"' +
']}' +
']},' +
'"None"' +
']}' +
']}' +
']}' +
']}' +
']},' +
'"None"' +
']}' +
']}' +
']},' +
'"None"' +
']}' +
']}' +
'')
with captured_output() as (out, err):
x, _, _ = calc.parse(token_input)
self.assertEqual(repr(x), ast_repr)
# deklarowanie typów dla zmiennyc
def test_2_1_real_types(self):
token_suite = {
"begin int i = 2; print(i); end" : "int",
"begin int i =2; print(i); end" : "int",
"begin int i=2; print(i); end" : "int",
"begin int i=2; print(i); end" : "int",
"begin int i= 2; print(i); end" : "int",
}
for expected_input, expected_output in token_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.typeo, expected_output)
self.assertEqual(out.getvalue(), '2\n')
def test_2_2_float_types(self):
token_suite = {
"begin float i = 2.0; print(i); end" : "float",
"begin float i =2.0; print(i); end" : "float",
"begin float i=2.0; print(i); end" : "float",
"begin float i=2.0; print(i); end" : "float",
"begin float i= 2.0; print(i); end" : "float"
}
for expected_input, expected_output in token_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.typeo, expected_output)
self.assertEqual(out.getvalue(), '2.0\n')
def test_2_3_dynamic_types(self):
token_suite = {
"begin float i = 2.0; print(i); end" : "float",
"begin int i = 2; print(i); end" : "int",
"begin boolean i = True; print(i); end" : "boolean",
"begin boolean i = False; print(i); end" : "boolean"
}
for expected_input, expected_output in token_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.typeo, expected_output)
def test_4_1_assign_statement(self):
token_type_suite = {
"begin int i = 20; print(i); end" : "int",
"begin int i = 2; end" : "int",
"begin int abecadlo = 220; end" : "int",
}
values_suite = {
"begin int i = 2; print(i); end" : "2\n",
"begin float i = 2.0; print(i); end" : "2.0\n",
"begin boolean i = True; print(i); end" : "True\n",
}
for expected_input, expected_output in token_type_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.typeo, expected_output)
for expected_input, expected_output in values_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(out.getvalue(), expected_output)
def test_4_2_assign_statement(self):
token_type_suite = {
"begin int i = 20; i = 21; print(i); end" : "int",
"begin int i = 2; i = 3; end" : "int",
"begin int abecadlo = 220; abecadlo = 221; end" : "int",
}
values_suite = {
"begin int i = 2; i = 3; print(i); end" : "3\n",
"begin float i = 2.0; i = 3.0; print(i); end" : "3.0\n",
"begin boolean i = True; i = False; print(i); end" : "False\n",
}
for expected_input, expected_output in token_type_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(x.block.blocks.block.casual_statement.operation.typeo, expected_output)
for expected_input, expected_output in values_suite.items():
with captured_output() as (out, err):
x, _, _ = calc.parse(expected_input)
self.assertEqual(out.getvalue(), expected_output)
# konwersja typów za pomocą dodatkowego operatora
def test_8_1_intToFloat(self):
assertThose(self, "begin int i = 6; print(intToFloat(i)); end", "6.0\n")
assertThose(self, "begin int i = 6; float j = intToFloat(i); print(j); end", "6.0\n")
assertThose(self, "begin int i = 6; float j = 15 + intToFloat(i); print(j); end", "21.0\n")
assertThose(self, "begin int i = 6; float j = sin 2 + intToFloat(i); print(j); end", "6.909297426825682\n")
def test_8_2_floatToInt(self):
assertThose(self, "begin float i = 6.0; print(floatToInt(i)); end", "6\n")
assertThose(self, "begin float i = 6.0; int j = floatToInt(i); print(j); end", "6\n")
assertThose(self, "begin float i = 6.0; int j = 15 + floatToInt(i); print(j); end", "21\n")
assertThose(self, "begin float i = 6.0; int j = 2 * 2 + floatToInt(i); print(j); end", "10\n")
class test_2_4(unittest.TestCase):
def test_1_1_define(self):
assertThose(self, "begin define fun(int a, int b) = a + b; end", "")
def test_1_2_define(self):
assertThose(self, "begin define fun(float a, string b) = a; end", "")
def test_1_3_define(self):
assertThose(self, "begin define fun(boolean a, boolean b) = b; end", "")
def test_2_1_block(self):
assertThose(self, "begin print(2); end", "2\n")
def test_2_2_block(self):
assertThose(self, "begin print(2); begin print(3); end end", "2\n3\n")
def test_3_1_block(self):
assertThose(self, "begin int i = 0; begin int i = 1; print(i); end end", "1\n")
def test_3_2_block(self):
assertThose(self, "begin int i = 0; begin int i = 1; print(i); end print(i); end", "1\n0\n")
def test_4_1_define(self):
assertThose(self, "begin define fun(int a, int b) = a + b; fun(1, 2); end", "")
def test_4_2_define(self):
assertThose(self, "begin define fun(int a, int b) = a + b; print(fun(1, 2)); end", "3\n")
def test_5_1_conversion(self):
assertThose(self, """begin print(3 * "test"); end""", "testtesttest\n")
def test_5_2_conversion(self):
assertThose(self, """begin print(3 * 4.5); end""", "13.5\n")
def test_5_3_conversion(self):
assertThose(self, """begin print(True * 4); end""", "4\n")
def test_5_4_conversion(self):
assertThose(self, """begin print(True + 1.137); end""", "2.137\n")
class test_2_5(unittest.TestCase):
def test_1_1_inner_functions(self):
assertThose(self, "begin define sum(int a, int b) = a + b; define mul(int a, int b) = a * b; print(sum(1, mul(2, 3))); end", "7\n")
def test_1_2_inner_functions(self):
assertThose(self, "begin define sum(int a, int b) = a + b; define add(int a, int b) = sum(a, b) + b; print(add(2, 3)); end", "8\n")
def test_1_3_fibonacci(self):
assertThose(self, "begin define fib(int n) = if (n < 2) n else fib(n - 1) + fib(n - 2); print(fib(10)); end", "55\n")
def test_2_1_omit_declarations(self):
with captured_output() as (out, err):
x, used_names, _ = calc.parse("begin int thisNameShouldNotExist = 1; print(2); end")
self.assertTrue("thisNameShouldNotExist" not in used_names)
def test_2_2_dont_omit(self):
with captured_output() as (out, err):
x, used_names, _ = calc.parse("begin int thisNameShouldExist = 1; print(thisNameShouldExist); end")
self.assertTrue("thisNameShouldExist" in used_names)
def test_3_1_save_the_evaluation_for_later(self):
with captured_output() as (out, err):
x, _, expressions = calc.parse("begin 2 + 3; end")
self.assertTrue(repr(ast.P_expression_binop(_, ast.P_expression_number(_, 'int', 2), '+', ast.P_expression_number(_, 'int', 3))) in expressions.keys())
if __name__ == '__main__':
unittest.main()