forked from beeware/voc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_while.py
148 lines (134 loc) · 3.98 KB
/
test_while.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
from unittest import expectedFailure
from ..utils import TranspileTestCase
class WhileLoopTests(TranspileTestCase):
def test_while(self):
self.assertCodeExecution("""
i = 0
total = 0
while i < 10:
i += 1
total += i
print(i, total)
print('Done.')
""")
def test_break(self):
self.assertCodeExecution(
code="""
i = 0
while i < 10:
i = i + 1
print(i, i % 5)
if i % 5 == 0:
break
print("after")
print("Done")
""")
def test_continue(self):
self.assertCodeExecution(
code="""
i = 0
while i < 10:
i = i + 1
print(i, i % 5)
if i % 5 == 0:
continue
print("after")
print("Done")
""")
def test_nested(self):
self.assertCodeExecution(
code="""
i = 1
j = 10
while i < j:
k = 0
while k < i:
print(i, j)
k = k + 1
print("While done")
i = i + 1
print("Done")
""")
def test_while_forever(self):
self.assertCodeExecution(
code="""
i = 0
while 1:
print("Loop", i)
i = i + 1
if i == 10:
break
print("Done")
""")
@expectedFailure
def test_while_forever_with_if_not(self):
self.assertCodeExecution(
code="""
i = 0
while 1:
print("Loop", i)
i = i + 1
if not i < 10:
break
print("Done")
""")
@expectedFailure
def test_while_not_forever(self):
self.assertCodeExecution(
code="""
while not 0:
print("Loop")
break
print("Done")
""")
@expectedFailure
def test_while_else(self):
self.assertCodeExecution(
code="""
i = 1
j = 4
while i < j:
print(i)
i = i + 1
else:
print("Else")
print("Done")
""")
@expectedFailure
def test_while_else_break(self):
self.assertCodeExecution(
code="""
i = 1
j = 4
while i < j:
print(i)
i = i + 1
break
else:
print("Else")
print("Done")
""")
@expectedFailure
def test_while_forever_inside_try(self):
"""Test ``while True`` inside try/finally block.
Currently raises::
Traceback (most recent call last):
...
File ".../env/src/voc/voc/python/opcodes.py", line 962, in convert
self.convert_opcode(context, arguments)
File ".../env/src/voc/voc/python/opcodes.py", line 1394, in convert_opcode
jump(JavaOpcodes.GOTO(0), context, current_loop, Opcode.NEXT)
UnboundLocalError: local variable 'current_loop' referenced before assignment
"""
self.assertCodeExecution(
code="""
i = 0
try:
while 1:
print("Loop", i)
i = i + 1
if i == 10:
break
finally:
print("Done")
""")