/
test_Terminal.py
executable file
·271 lines (176 loc) · 7.66 KB
/
test_Terminal.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
#!/usr/bin/env python2
import unittest
from Terminal import *
#
class test_Terminal(unittest.TestCase):
def test_setup(self):
t = Terminal(3,5)
self.assertFalse(t.insert_mode,"Should be in overstrike mode at create time")
self.assertEqual(t.row,0,"Should be in row zero at create time")
self.assertEqual(t.col,0,"Should be in column zero at create time")
self.assertEqual(t.str(), " \n \n ", "Should be three newline-terminated rows of spaces")
def test_move_to(self):
t = Terminal()
t.move_to(5,5)
self.assertEqual(t.row,5,"Should be in row five after move")
self.assertEqual(t.col,5,"Should be in column five after move")
t.move_to(10,1) # exceeds the maximum rows
self.assertEqual(t.row,9,"Should be in row nine after move")
self.assertEqual(t.col,6,"Should be in column six after move")
t.move_to(0,10) # exceeds the maximum columns
self.assertEqual(t.row,9,"Should be in row nine after move")
self.assertEqual(t.col,9,"Should be in column nine after move")
t.move_to(-10,0) # below the minimum rows
self.assertEqual(t.row,0,"Should be in row zero after move")
self.assertEqual(t.col,9,"Should still be in column nine after move")
t.move_to(0,-10) # below the minimum rows
self.assertEqual(t.row,0,"Should still be in row zero after move")
self.assertEqual(t.col,0,"Should be in column zero after move")
# normal moves - rows
t.move_to(1,0)
self.assertEqual(t.row,1,"Should be in row one after move")
self.assertEqual(t.col,0,"Should be in column zero after move")
t.move_to(1,0)
self.assertEqual(t.row,2,"Should be in row two after move")
self.assertEqual(t.col,0,"Should be in column zero after move")
t.move_to(-1,0)
self.assertEqual(t.row,1,"Should be in row one after move")
self.assertEqual(t.col,0,"Should be in column zero after move")
# normal moves - columns
t.move_to(0,1)
self.assertEqual(t.row,1,"Should be in row one after move")
self.assertEqual(t.col,1,"Should be in column one after move")
t.move_to(0,-1)
self.assertEqual(t.row,1,"Should be in row two after move")
self.assertEqual(t.col,0,"Should be in column zero after move")
t.move_to(0,1)
self.assertEqual(t.row,1,"Should be in row one after move")
self.assertEqual(t.col,1,"Should be in column one after move")
def test_go_to(self):
t = Terminal()
t.go_to(5,5)
self.assertEqual(t.row,5,"Should be in row five after go")
self.assertEqual(t.col,5,"Should be in column five after go")
t.go_to(10,1) # exceeds the maximum rows
self.assertEqual(t.row,9,"Should be in row nine after go")
self.assertEqual(t.col,1,"Should be in column one after go")
t.go_to(0,10) # exceeds the maximum columns
self.assertEqual(t.row,0,"Should be in row zero after go")
self.assertEqual(t.col,9,"Should be in column nine after go")
t.go_to(-10,-10) # below the minimums
self.assertEqual(t.row,0,"Should be in row zero after go")
self.assertEqual(t.col,0,"Should be in column zero after go")
t.go_to(10,10) # above the maximums
self.assertEqual(t.row,9,"Should be in row nine after go")
self.assertEqual(t.col,9,"Should be in column nine after go")
def test_store_single_row(self):
# Basic checking insert vs. overwrite mode
t = Terminal(1,10)
t.store("The cat sat down")
self.assertEqual(t.str(),"The cat sa","String should be truncated")
t.go_to(0,4)
t.store("CAR")
self.assertEqual(t.str(),"The CAR sa","String should have overwritten data")
t.insert_mode = True
t.go_to(0,0)
t.store("*Now*")
self.assertEqual(t.str(),"*Now*The C","String should should show inserted data")
t.go_to(0,0)
t.store("1234567890")
self.assertEqual(t.str(),"1234567890","String should have been replaced by insert: " + t.str())
t.go_to(0,5)
t.store("abc")
self.assertEqual(t.str(),"12345abc67","String should have insert in the middle: " + t.str())
t.insert_mode = False
t.go_to(0,2)
t.store("XYZ")
self.assertEqual(t.str(),"12XYZabc67","String should have overwrite near the start: " + t.str())
def test_interpret_relative_moves(self):
# Up, Down, Right, and Left, leaving a character behind after the moves
#
# Note that the cursor moves to the right after each character, so the
# additional ^l sequences back it up into the expected position for
# the next write
#
t = Terminal()
t.go_to(4,4)
t.interpret("^uU^d^d^lD^l^u^rR^l^l^lL")
self.assertEqual(t.str(),"""
U
L R
D
""","Result does not match")
def test_interpret_row_column_moves(self):
# Go to X,Y and Home, Beginning of Row, Erase from cursor, and Clear all
t = Terminal()
t.interpret("^i^90abcdefghijkklmnop")
self.assertEqual(t.str(),
"""
abcdefghip""","Result does not match after last-row insert")
t.interpret("^i^00==========")
t.interpret("^i^40++++++++++")
t.interpret("^i^90^e")
t.interpret("^i^30----------")
t.interpret("^i^10^^^^^^^^^^^^^^^^^^^^")
t.interpret("^i^20**********")
self.assertEqual(t.str(),
"""==========
^^^^^^^^^^
**********
----------
++++++++++
""","Result does not match after inserts and clearing last row")
t.interpret("^h^e^11^e^22^e^33^e^44^e")
self.assertEqual(t.str(),
"""
^
**
---
++++
""","Result does not match after making triangle of deletions")
t.interpret("^b^e")
self.assertEqual(t.str(),
"""
^
**
---
""","Result does not match after beginning of row/erase sequence")
r_before = t.row
c_before = t.col
t.interpret("^c")
self.assertEqual(t.str(),
"""
""","Result does not match after clear-all")
self.assertEqual(t.row, r_before)
self.assertEqual(t.col, c_before)
def test_challenge_data(self):
"""Run the command sequence presented in the challenge"""
# Note: the trailing backslashes in the sample data caused Python to ignore them:
# appending a space after the \ tells Python's """ intepreter to leave the \ alone
data = """^h^c
^04^^
^13/ \^d^b / \
^u^d^d^l^l^l^l^l^l^l^l^l
^r^r^l^l^d<CodeEval >^l^l^d/^b \
^d^r^r^66/^b \
^b^d \ /
^d^l^lv^d^b===========^i^94O123456
789^94A=======^u^u^u^u^u^u^l^l\^o^b^r/
"""
t = Terminal()
for s in data.splitlines():
t.interpret(s)
self.assertEqual(t.str(),
""" ^
/ \
/ \
/ \
<CodeEval>
\ /
\ /
\ /
v
====A=====""","Result does not match sample output")
#
if __name__ == '__main__':
unittest.main()