-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
106 lines (97 loc) · 1.73 KB
/
test.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
import unittest
import sol
valid_sources = []
valid_sources.append("var a = 1;")
valid_sources.append("""
var a = 1;
a = 2;
{
var b = 3;
a = b;
{
var c = 4;
a = c;
}
}
""")
invalid_sources = []
invalid_sources.append("var ;")
invalid_sources.append("var a")
invalid_sources.append("""
var a = 1;
{
var b = 2;
}
b = 3;
""")
invalid_sources.append("""
var a = 1;
{
var a = 2;
}
""")
valid_sources.append("""
var a1b2 = 1 + 2;
var a = 1;
a = 2;
{
var b = 3;
a = b;
{
var c = 4;
a = c;
}
}
""")
valid_sources.append("var a = 1; if (a == 1) a = 2;")
valid_sources.append("""
var a = 1;
if (a == 1)
{
a = a + 1;
}
""")
valid_sources.append("""
var a = 2;
if (a == 1)
{
a = a + 1;
}
else
{
a = 0;
}
""")
valid_sources.append("var a = true; if (a) a = false;")
valid_sources.append("""
var a = 20;
while (a > 0)
{
a = a - 1;
}
""")
valid_sources.append("for (var a = 0; a < 10; a = a + 1) a = a + 1;")
valid_sources.append("var word = \"hello world\"; word = word + \"!\";")
def create_test(source):
def f():
sol.interpret(source)
return f
def create_invalid_test(source):
def f():
exception_thrown = False
try:
sol.interpret(source)
except Exception:
exception_thrown = True
assert exception_thrown, "No exception was thrown"
return f
def suite():
suite = unittest.TestSuite()
for source in valid_sources:
suite.addTest(unittest.FunctionTestCase(create_test(source)))
for source in invalid_sources:
suite.addTest(unittest.FunctionTestCase(create_invalid_test(source)))
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suite())