Skip to content

Commit f1e4a14

Browse files
committed
CC:T final tests migrated
1 parent b719fde commit f1e4a14

12 files changed

Lines changed: 331 additions & 158 deletions

File tree

computercraft/server.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def protocol(send, sess_cls=sess.CCSession, oc=False):
5959
sess.on_task_result(next(msg), next(msg))
6060
elif action == b'C':
6161
sess.throw_keyboard_interrupt()
62+
elif action == b'D': # disconnection
63+
return
6264
else:
6365
send(PROTO_ERROR)
6466
return
@@ -88,6 +90,13 @@ async def ws(self, request):
8890

8991
if mustquit:
9092
break
93+
94+
if not mustquit: # sudden disconnect
95+
try:
96+
pgen.send(b'D')
97+
except StopIteration:
98+
pass
99+
91100
return ws
92101

93102
async def tcp(self, reader, writer):
@@ -195,7 +204,10 @@ def pgen():
195204
while True:
196205
m = yield
197206
write_frame(b'R', m)
198-
p.send(m)
207+
try:
208+
p.send(m)
209+
except StopIteration as e:
210+
return e.value
199211

200212
return pgen()
201213

examples/_lib.py

Lines changed: 0 additions & 139 deletions
This file was deleted.

examples/test_reboot.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/test_shutdown.py

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
assert peripheral.isPresent('back') is False
77

8-
tbl = _lib.get_object_table('pocket')
9-
assert _lib.get_class_table(pocket) == tbl
10-
118
_lib.step('Clean inventory from any pocket upgrades')
129

1310
with _lib.assert_raises(LuaException):
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
_lib = import_file('_lib.py', __file__)
44
step, assert_raises = _lib.step, _lib.assert_raises
55

6-
7-
tbl = _lib.get_object_table('rednet')
8-
del tbl['function']['run']
9-
assert _lib.get_class_table(rednet) == tbl
10-
116
side = 'back'
127

138
step(f'Attach modem to {side} side of computer')

tests/cc_programs/shutdown.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from cc import os
2+
3+
4+
if args[-1:] == [b'reboot']:
5+
assert os.reboot() is None
6+
else:
7+
assert os.shutdown() is None
8+
print('Test finished successfully')
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
R671:0[5]{:[1]<9>pocket.py:[0]<2>py}<9>pocket.py<623>from cc import LuaException, import_file, pocket, peripheral
2+
3+
_lib = import_file('_lib.py', __file__)
4+
5+
6+
assert peripheral.isPresent('back') is False
7+
8+
_lib.step('Clean inventory from any pocket upgrades')
9+
10+
with _lib.assert_raises(LuaException):
11+
pocket.equipBack()
12+
with _lib.assert_raises(LuaException):
13+
pocket.unequipBack()
14+
assert peripheral.isPresent('back') is False
15+
16+
_lib.step('Put any pocket upgrade to inventory')
17+
18+
assert pocket.equipBack() is None
19+
assert peripheral.isPresent('back') is True
20+
21+
assert pocket.unequipBack() is None
22+
assert peripheral.isPresent('back') is False
23+
24+
print('Test finished successfully')
25+
S257:T<1>1<215>local p, rel = ...
26+
if rel ~= nil then
27+
p = fs.combine(fs.getDir(rel), p)
28+
end
29+
if not fs.exists(p) then return nil end
30+
if fs.isDir(p) then return nil end
31+
f = fs.open(p, "r")
32+
local src = f.readAll()
33+
f.close()
34+
return src{:[1]<7>_lib.py:[2]<9>pocket.py}
35+
R1162:T<1>1<1151>{:[1]T:[2]<1134>from contextlib import contextmanager
36+
from cc import os
37+
38+
39+
@contextmanager
40+
def assert_raises(etype, message=None):
41+
try:
42+
yield
43+
except Exception as e:
44+
assert isinstance(e, etype), repr(e)
45+
if message is not None:
46+
assert e.args == (message, )
47+
else:
48+
raise AssertionError(f'Exception of type {etype} was not raised')
49+
50+
51+
@contextmanager
52+
def assert_takes_time(at_least, at_most):
53+
t = os.epoch('utc') / 1000
54+
yield
55+
dt = os.epoch('utc') / 1000 - t
56+
# print(at_least, '<=', dt, '<=', at_most)
57+
assert at_least <= dt <= at_most
58+
59+
60+
class AnyInstanceOf:
61+
def __init__(self, cls):
62+
self.c = cls
63+
64+
def __eq__(self, other):
65+
return isinstance(other, self.c)
66+
67+
68+
def step(text):
69+
input(f'{text} [enter]')
70+
71+
72+
def term_step(text):
73+
from cc import colors, term
74+
75+
for color in colors.iter_colors():
76+
r, g, b = term.nativePaletteColor(color)
77+
term.setPaletteColor(color, r, g, b)
78+
term.setBackgroundColor(colors.black)
79+
term.setTextColor(colors.white)
80+
term.clear()
81+
term.setCursorPos(1, 1)
82+
term.setCursorBlink(True)
83+
step(text)}
84+
S46:T<1>1<24>G:peripheral:M:isPresent{:[1]<4>back}
85+
R21:T<1>1<12>{:[1]T:[2]F}
86+
S80:T<1>1<13>io.write(...){:[1]<48>Clean inventory from any pocket upgrades [enter]}
87+
R15:T<1>1<7>{:[1]T}
88+
S22:T<1>1<11>G:io:M:read{}
89+
R23:T<1>1<14>{:[1]T:[2]<0>}
90+
S31:T<1>1<20>G:pocket:M:equipBack{}
91+
R56:T<1>1<47>{:[1]T:[2]F:[3]<27>Cannot find a valid upgrade}
92+
S33:T<1>1<22>G:pocket:M:unequipBack{}
93+
R47:T<1>1<38>{:[1]T:[2]F:[3]<18>Nothing to unequip}
94+
S46:T<1>1<24>G:peripheral:M:isPresent{:[1]<4>back}
95+
R21:T<1>1<12>{:[1]T:[2]F}
96+
S75:T<1>1<13>io.write(...){:[1]<43>Put any pocket upgrade to inventory [enter]}
97+
R15:T<1>1<7>{:[1]T}
98+
S22:T<1>1<11>G:io:M:read{}
99+
R23:T<1>1<14>{:[1]T:[2]<0>}
100+
S31:T<1>1<20>G:pocket:M:equipBack{}
101+
R21:T<1>1<12>{:[1]T:[2]T}
102+
S46:T<1>1<24>G:peripheral:M:isPresent{:[1]<4>back}
103+
R21:T<1>1<12>{:[1]T:[2]T}
104+
S33:T<1>1<22>G:pocket:M:unequipBack{}
105+
R21:T<1>1<12>{:[1]T:[2]T}
106+
S46:T<1>1<24>G:peripheral:M:isPresent{:[1]<4>back}
107+
R21:T<1>1<12>{:[1]T:[2]F}
108+
S58:T<1>1<13>io.write(...){:[1]<26>Test finished successfully}
109+
R15:T<1>1<7>{:[1]T}
110+
S32:T<1>1<13>io.write(...){:[1]<1>
111+
}
112+
R15:T<1>1<7>{:[1]T}
113+
S2:CN

0 commit comments

Comments
 (0)