-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_minidc.py
467 lines (425 loc) · 15.1 KB
/
test_minidc.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
import unittest
import sys
import StringIO
from minidc import minidc
class test_minidc(unittest.TestCase):
#test push_number with valid domain logic
def test_push_number_domain_correct(self):
#create a calculator
calc = minidc()
#push number 5 on the stack
calc.push_number(5)
#check to make sure the stack has length 1
self.assertEqual(len(calc.nums), 1)
#check to make sure the (only) number on the stack is 5
self.assertEqual(calc.nums.pop(), 5)
#test add with valid domain logic
def test_add_domain_correct(self):
#create a calculator
calc = minidc()
#push a 3 onto the stack
calc.push_number(3)
#push a 5 onto the stack
calc.push_number(5)
#add the two numbers together
calc.add()
#check to make sure the stack has length 1
self.assertEqual(len(calc.nums), 1)
#check to make sure the value on the stack is 8
self.assertEqual(calc.nums.pop(), 8)
#test subtract with valid domain logic
def test_subtract_domain_correct(self):
#create a calculator
calc = minidc()
#push a 5 onto the stack
calc.push_number(5)
#push a 3 onto the stack
calc.push_number(3)
#subtract the second number from the other
calc.subtract()
#check to make sure the stack has length 1
self.assertEqual(len(calc.nums), 1)
#check to make sure the value on the stack is 2
self.assertEqual(calc.nums.pop(), 2)
#test multiply with valid domain logic
def test_multiply_domain_correct(self):
#create a calculator
calc = minidc()
#push a 5 onto the stack
calc.push_number(5)
#push a 3 onto the stack
calc.push_number(3)
#multiply the numbers together
calc.multiply()
#check to make sure the stack has length 1
self.assertEqual(len(calc.nums), 1)
#check to make sure the value on the stack is 15
self.assertEqual(calc.nums.pop(), 15)
#test divide with valid domain logic
def test_divide_domain_correct(self):
#create a calculator
calc = minidc()
#push a 15 onto the stack
calc.push_number(15)
#push a 3 onto the stack
calc.push_number(3)
#divide the numbers
calc.divide()
#make sure the stack has length 1
self.assertEqual(len(calc.nums), 1)
#make sure value on stack is 5
self.assertEqual(calc.nums.pop(), 5)
#test the p command with valid domain logic
def test_command_p_domain_correct(self):
#create a calculator
calc = minidc()
#push a 4 onto the stack
calc.push_number(4)
#redirect stdout to something we can read
stdout_temp = sys.stdout
out = StringIO.StringIO()
sys.stdout = out
try:
#run the p command
calc.command_p()
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stdout = stdout_temp
#assert that the stack length hasn't changed
self.assertEqual(len(calc.nums), 1)
#assert that the value printed is the values pushed
self.assertEqual(printed, "4\n")
#test the n command with valid domain logic
def test_command_n_domain_correct(self):
#create a calculator
calc = minidc()
#push a 10 onto the stack
calc.push_number(10)
#redirect stdout to something we can read
stdout_temp = sys.stdout
out = StringIO.StringIO()
sys.stdout = out
try:
#run the n command
calc.command_n()
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stdout = stdout_temp
#assert that the stack length has decremented
self.assertEqual(len(calc.nums), 0)
#assert that the value printed is the values pushed
self.assertEqual(printed, "10\n")
#test the f command with valid domain logic
def test_command_f_domain_correct(self):
#create a calculator
calc = minidc()
#push a bunch of numbers onto the stack
calc.push_number(10)
calc.push_number(5)
calc.push_number(3)
#redirect stdout to something we can read
stdout_temp = sys.stdout
out = StringIO.StringIO()
sys.stdout = out
try:
#run the f command
calc.command_f()
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stdout = stdout_temp
#assert that the stack length hasn't changed
self.assertEqual(len(calc.nums), 3)
#assert that the values printed are the values pushed
self.assertEqual(printed, "3 5 10 \n")
#test the add function with just 1 value on the stack
def test_add_domain_incorrect(self):
#create a calculator
calc = minidc()
#push 4 onto the stack
calc.push_number(4)
#redirect stderr to something we can read
stderr_temp = sys.stdout
out = StringIO.StringIO()
sys.stderr = out
try:
#run the add command (with only 1 value)
try:
calc.add()
except ArithmeticError:
pass
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stderr = stderr_temp
self.assertEqual(len(calc.nums), 1)
self.assertEqual(printed, 'Error: not enough values to add\n')
#test the subtract function with just 1 value on the stack
def test_subtract_domain_incorrect(self):
#create a calculator
calc = minidc()
#push 4 onto the stack
calc.push_number(4)
#redirect stderr to something we can read
stderr_temp = sys.stdout
out = StringIO.StringIO()
sys.stderr = out
try:
#run the subtract command (with only 1 value)
try:
calc.subtract()
except ArithmeticError:
pass
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stderr = stderr_temp
self.assertEqual(len(calc.nums), 1)
self.assertEqual(printed, 'Error: not enough values to subtract\n')
#test the multiply function with just 1 value on the stack
def test_multiply_domain_incorrect(self):
#create a calculator
calc = minidc()
#push 4 onto the stack
calc.push_number(4)
#redirect stderr to something we can read
stderr_temp = sys.stdout
out = StringIO.StringIO()
sys.stderr = out
try:
#run the multiply command (with only 1 value)
try:
calc.multiply()
except ArithmeticError:
pass
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stderr = stderr_temp
self.assertEqual(len(calc.nums), 1)
self.assertEqual(printed, 'Error: not enough values to multiply\n')
#test the divide function with just 1 value on the stack
def test_divide_domain_incorrect_1(self):
#create a calculator
calc = minidc()
#push 4 onto the stack
calc.push_number(4)
#redirect stderr to something we can read
stderr_temp = sys.stdout
out = StringIO.StringIO()
sys.stderr = out
try:
#run the divide command (with only 1 value)
try:
calc.divide()
except ArithmeticError:
pass
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stderr = stderr_temp
self.assertEqual(len(calc.nums), 1)
self.assertEqual(printed, 'Error: not enough values to divide\n')
#test the divide function dividing by zero
def test_divide_domain_incorrect_2(self):
#create a calculator
calc = minidc()
#push 4 onto the stack
calc.push_number(4)
#push 0 onto the stack
calc.push_number(0)
#redirect stderr to something we can read
stderr_temp = sys.stdout
out = StringIO.StringIO()
sys.stderr = out
try:
#run the divide command (with only 1 value)
try:
calc.divide()
except ArithmeticError:
pass
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stderr = stderr_temp
self.assertEqual(printed, 'Error: divide by 0\n')
#test the command_p function empty stack
def test_command_p_domain_incorrect(self):
#create a calculator
calc = minidc()
#redirect stderr to something we can read
stderr_temp = sys.stdout
out = StringIO.StringIO()
sys.stderr = out
try:
#run the divide command (with only 1 value)
try:
calc.command_p()
except Exception:
pass
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stderr = stderr_temp
self.assertEqual(printed, 'Error: empty stack\n')
#test the command_n function empty stack
def test_command_n_domain_incorrect(self):
#create a calculator
calc = minidc()
#redirect stderr to something we can read
stderr_temp = sys.stdout
out = StringIO.StringIO()
sys.stderr = out
try:
#run the divide command (with only 1 value)
try:
calc.command_n()
except Exception:
pass
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stderr = stderr_temp
self.assertEqual(printed, 'Error: empty stack\n')
#test the command_f function empty stack
def test_command_f_domain_incorrect(self):
#create a calculator
calc = minidc()
#redirect stderr to something we can read
stderr_temp = sys.stdout
out = StringIO.StringIO()
sys.stderr = out
try:
#run the divide command (with only 1 value)
try:
calc.command_f()
except Exception:
pass
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stderr = stderr_temp
self.assertEqual(printed, 'Error: empty stack\n')
#test the execute_line command with valid input logic
#numbers, decimal numbers and arithmetic operations
def test_execute_line_input_correct_1(self):
#create a calculator
calc = minidc()
#execute a line
calc.execute_line('5.2 3+4 6 .5*-/')
#check to make sure there is only one number on the stack
self.assertEqual(len(calc.nums), 1)
#check to make sure the value is 8.2
self.assertEqual(calc.nums.pop(), 8.2)
#tests to make sure negatives, command_f, command_p and command_n are processed properly
def test_execute_line_input_correct_2(self):
#create a calculator
calc = minidc()
#redirect stdout to something we can read
stdout_temp = sys.stdout
out = StringIO.StringIO()
sys.stdout = out
try:
#run a command (including negatives, command_f, command_p and command_n)
calc.execute_line('5.2_3.1fnp')
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stdout = stdout_temp
self.assertEqual(printed, '-3.1 5.2 \n-3.1\n5.2\n')
self.assertEqual(len(calc.nums), 1)
#tests to make sure negative special cases are processed properly
def test_execute_line_input_correct_3(self):
#create a calculator
calc = minidc()
#redirect stdout to something we can read
stdout_temp = sys.stdout
out = StringIO.StringIO()
sys.stdout = out
try:
#run a command (including negatives, command_f, command_p and command_n)
calc.execute_line('__.f')
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stdout = stdout_temp
self.assertEqual(printed, '0 0 \n')
self.assertEqual(len(calc.nums), 2)
#test to make sure improper commands are caught
def test_execute_line_input_incorrect_1(self):
#create a calculator
calc = minidc()
#redirect stderr to something we can read
stderr_temp = sys.stdout
out = StringIO.StringIO()
sys.stderr = out
try:
#run a command (including command_f, command_p and command_n
try:
calc.execute_line('3 4phy')
except Exception:
pass
#get the value printed
printed = out.getvalue()
finally:
#restore stderr
sys.stderr = stderr_temp
self.assertEqual(printed, 'Error: \'h\': Not a valid operation\n')
#Tests the run command with valid parameters and numbers. Also makes it quit
def test_run_parameters_numbers_correct_1(self):
#create a calculator
calc = minidc()
#redirect stdout to something we can read
stdout_temp = sys.stdout
out = StringIO.StringIO()
sys.stdout = out
try:
#run a bunch of valid commands
commands = StringIO.StringIO('3 4+p\nq\n')
calc.run(commands=commands.readline)
#get the value printed
printed = out.getvalue()
finally:
#restore stdout
sys.stdout = stdout_temp
self.assertEqual(printed, "$ 7\n$ ")
#Tests the run command with invalid parameters and numbers.
def test_run_parameters_numbers_incorrect_1(self):
#create a calculator
calc = minidc()
#redirect stdout to something we can read
stdout_temp = sys.stdout
out = StringIO.StringIO()
sys.stdout = out
#redirect stderr to something we can read
stderr_temp = sys.stderr
err = StringIO.StringIO()
sys.stderr = err
try:
#run a bunch of valid commands
commands = StringIO.StringIO('p\n3+\nq\n')
calc.run(commands=commands.readline)
#get the value printed
outprinted = out.getvalue()
errprinted = err.getvalue()
finally:
#restore stdout
sys.stdout = stdout_temp
self.assertEqual(outprinted, "$ \n$ \n$ ")
self.assertEqual(errprinted, "Error: empty stack\nError: not enough values to add\n")
unittest.TextTestRunner(verbosity=2).run(unittest.TestLoader().loadTestsFromTestCase(test_minidc))