-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTests.py
619 lines (518 loc) · 16.1 KB
/
Tests.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#-------------------------------------------------------------------------
# Python 3.7
# Date: 30 June 2019
# Author: c stacey
#-------------------------------------------------------------------------
# the library we are testing
from ReadifyToyRobot import *
import io
import sys
# for copying function __name__ etc from wrapped func to wrapper func
from functools import wraps
# for parsing command line arguments
import argparse
class Tester:
'''A class for performing basic testing of functions return values.
Note: This class will not work as expected in IDLE, because the
stdout stream can be captured ok, but not reset.
Thus, after calling start_logging_stdout, no further stdout
output will display in the IDLE console, even after
stop_logging_stdout is called.
"By default, IDLE runs user code in a separate OS process rather than
in the user interface process that runs the shell and editor. In the
execution process, it replaces sys.stdin, sys.stdout, and
sys.stderr with objects that get input from and send output to the
Shell window. The original values stored in sys.__stdin__,
sys.__stdout__, and sys.__stderr__ are not touched, but may be None."
https://docs.python.org/3/library/idle.html#running-user-code'''
def __init__(s):
s.capturedOutput = None
def start_logging_stdout(s) -> None:
'''divert stdout stream so it can be recorded by Tester object'''
if not s.capturedOutput:
s.capturedOutput = io.StringIO()
sys.stdout = s.capturedOutput
def stop_logging_stdout(s) -> None:
'''reset stdout stream, back to it's normal file alias'''
sys.stdout = sys.__stdout__
def get_log(s) -> str:
'''returns the captured output thus far (a string)
(does not consume the stream, thus this method is idempotent)'''
return s.capturedOutput.getvalue()
# technically this is a static method
def capture_calls_and_returns(s, func, func_name):
'''decorator function used to print names of functions called,
and returns from those functions.
Note: arguments passed in are captured in their repr form.
(ie. calls will not be identical to that made in code)'''
@wraps(func)
def wrapped(*args, **kwargs):
s1 = ', '.join( [f'{v!r}' for v in args] )
s2 = ', '.join( [f'{k}={v!r}' for k, v in kwargs.items()] )
sep = ', ' if s1 != '' and s2 != '' else ''
call = f"{func_name}({s1}{sep}{s2})"
print(call)
r = func(*args, **kwargs)
print(' returned:\n',r)
return wrapped
def testA():
t = Tester()
# create table and robot that will be used in testing
table = FlatSurface()
robot = Character()
# Wrap methods of THIS INSTANCE of Character (to print calls to console)
# Second arg is used because passing in the variable name 'robot'
# makes the output easier to read (no better way to get variable name).
robot.report = t.capture_calls_and_returns(robot.report, 'robot.report')
robot.right = t.capture_calls_and_returns(robot.right, 'robot.right')
robot.left = t.capture_calls_and_returns(robot.left, 'robot.left')
robot.place = t.capture_calls_and_returns(robot.place, 'robot.place')
robot.move = t.capture_calls_and_returns(robot.move, 'robot.move')
t.start_logging_stdout()
print(f"table = {table}")
robot.report(); print()
robot.place(table, 0, 0, 'north')
robot.move()
robot.report()
t.stop_logging_stdout()
if t.get_log() == \
"\n".join(
['''table = FlatSurface(width=5, depth=5)'''
,'''robot.report()'''
,'''None, None, None'''
,''' returned:'''
,''' None, None, None'''
,''''''
,'''robot.place(FlatSurface(width=5, depth=5), 0, 0, 'north')'''
,''' returned:'''
,''' True'''
,'''robot.move()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''0,1,NORTH'''
,''' returned:'''
,''' 0,1,NORTH'''
,''''''
]):
print("testA PASSED")
return True
else:
print("testA *FAILED*")
return False
def testB():
t = Tester()
# create table and robot that will be used in testing
table = FlatSurface()
robot = Character()
# Wrap methods of THIS INSTANCE of Character (to print calls to console)
# Second arg is used because passing in the variable name 'robot'
# makes the output easier to read (no better way to get variable name).
robot.report = t.capture_calls_and_returns(robot.report, 'robot.report')
robot.right = t.capture_calls_and_returns(robot.right, 'robot.right')
robot.left = t.capture_calls_and_returns(robot.left, 'robot.left')
robot.place = t.capture_calls_and_returns(robot.place, 'robot.place')
robot.move = t.capture_calls_and_returns(robot.move, 'robot.move')
t.start_logging_stdout()
print(f"table = {table}")
robot.report(); print()
robot.place(table, 0, 0, 'north')
robot.left()
robot.report()
t.stop_logging_stdout()
if t.get_log() == \
"\n".join(
['''table = FlatSurface(width=5, depth=5)'''
,'''robot.report()'''
,'''None, None, None'''
,''' returned:'''
,''' None, None, None'''
,''''''
,'''robot.place(FlatSurface(width=5, depth=5), 0, 0, 'north')'''
,''' returned:'''
,''' True'''
,'''robot.left()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''0,0,WEST'''
,''' returned:'''
,''' 0,0,WEST'''
,''''''
]):
print("testB PASSED")
return True
else:
print("testB *FAILED*")
return False
def testC():
t = Tester()
# create table and robot that will be used in testing
table = FlatSurface()
robot = Character()
# Wrap methods of THIS INSTANCE of Character (to print calls to console)
# Second arg is used because passing in the variable name 'robot'
# makes the output easier to read (no better way to get variable name).
robot.report = t.capture_calls_and_returns(robot.report, 'robot.report')
robot.right = t.capture_calls_and_returns(robot.right, 'robot.right')
robot.left = t.capture_calls_and_returns(robot.left, 'robot.left')
robot.place = t.capture_calls_and_returns(robot.place, 'robot.place')
robot.move = t.capture_calls_and_returns(robot.move, 'robot.move')
t.start_logging_stdout()
print(f"table = {table}")
robot.report(); print()
robot.place(table, 1, 2, 'east')
robot.move()
robot.move()
robot.left()
robot.move()
robot.report()
t.stop_logging_stdout()
if t.get_log() == \
"\n".join(
['''table = FlatSurface(width=5, depth=5)'''
,'''robot.report()'''
,'''None, None, None'''
,''' returned:'''
,''' None, None, None'''
,''''''
,'''robot.place(FlatSurface(width=5, depth=5), 1, 2, 'east')'''
,''' returned:'''
,''' True'''
,'''robot.move()'''
,''' returned:'''
,''' True'''
,'''robot.move()'''
,''' returned:'''
,''' True'''
,'''robot.left()'''
,''' returned:'''
,''' True'''
,'''robot.move()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''3,3,NORTH'''
,''' returned:'''
,''' 3,3,NORTH'''
,''''''
]):
print("testC PASSED")
return True
else:
print("testC *FAILED*")
return False
def testD():
t = Tester()
# create table and robot that will be used in testing
table = FlatSurface(width=3, depth=3)
robot = Character()
# Wrap methods of THIS INSTANCE of Character (to print calls to console)
# Second arg is used because passing in the variable name 'robot'
# makes the output easier to read (no better way to get variable name).
robot.report = t.capture_calls_and_returns(robot.report, 'robot.report')
robot.right = t.capture_calls_and_returns(robot.right, 'robot.right')
robot.left = t.capture_calls_and_returns(robot.left, 'robot.left')
robot.place = t.capture_calls_and_returns(robot.place, 'robot.place')
robot.move = t.capture_calls_and_returns(robot.move, 'robot.move')
t.start_logging_stdout()
print(f"table = {table}")
robot.report(); print()
# test ignoring commands before valid placement
robot.right()
robot.report(); print()
robot.left()
robot.report(); print()
robot.move()
robot.report(); print()
# test ignoring invalid place commands
robot.place(table, 10, 10, 'NORTH')
robot.report(); print()
robot.place(table, 2, 2, 'JUNK')
robot.report(); print()
# valid place
robot.place(table, 2, 2, 'north')
robot.report(); print()
# test not falling off table
robot.move()
robot.report(); print()
robot.right()
robot.report(); print()
robot.move()
robot.report(); print()
robot.right()
robot.report(); print()
robot.move()
robot.report(); print()
robot.right()
robot.report(); print()
robot.move()
robot.report(); print()
robot.move()
robot.report(); print()
robot.move()
robot.report(); print()
robot.left()
robot.report(); print()
robot.move()
robot.report(); print()
robot.move()
robot.report(); print()
# test invalid and valid place cmd (after valid place cmd)
robot.place(table, 10, 10, 'north')
robot.report(); print()
robot.place(table, 1, 1, 'west')
robot.report(); print()
# test turning left/right past north point
robot.right()
robot.report(); print()
robot.right()
robot.report(); print()
robot.left()
robot.report(); print()
robot.left()
robot.report(); print()
t.stop_logging_stdout()
if t.get_log() == \
"\n".join(
['''table = FlatSurface(width=3, depth=3)'''
,'''robot.report()'''
,'''None, None, None'''
,''' returned:'''
,''' None, None, None'''
,''''''
,'''robot.right()'''
,''' returned:'''
,''' False'''
,'''robot.report()'''
,'''None, None, None'''
,''' returned:'''
,''' None, None, None'''
,''''''
,'''robot.left()'''
,''' returned:'''
,''' False'''
,'''robot.report()'''
,'''None, None, None'''
,''' returned:'''
,''' None, None, None'''
,''''''
,'''robot.move()'''
,''' returned:'''
,''' False'''
,'''robot.report()'''
,'''None, None, None'''
,''' returned:'''
,''' None, None, None'''
,''''''
,'''robot.place(FlatSurface(width=3, depth=3), 10, 10, 'NORTH')'''
,''' returned:'''
,''' False'''
,'''robot.report()'''
,'''None, None, None'''
,''' returned:'''
,''' None, None, None'''
,''''''
,'''robot.place(FlatSurface(width=3, depth=3), 2, 2, 'JUNK')'''
,''' returned:'''
,''' False'''
,'''robot.report()'''
,'''None, None, None'''
,''' returned:'''
,''' None, None, None'''
,''''''
,'''robot.place(FlatSurface(width=3, depth=3), 2, 2, 'north')'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''2,2,NORTH'''
,''' returned:'''
,''' 2,2,NORTH'''
,''''''
,'''robot.move()'''
,''' returned:'''
,''' False'''
,'''robot.report()'''
,'''2,2,NORTH'''
,''' returned:'''
,''' 2,2,NORTH'''
,''''''
,'''robot.right()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''2,2,EAST'''
,''' returned:'''
,''' 2,2,EAST'''
,''''''
,'''robot.move()'''
,''' returned:'''
,''' False'''
,'''robot.report()'''
,'''2,2,EAST'''
,''' returned:'''
,''' 2,2,EAST'''
,''''''
,'''robot.right()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''2,2,SOUTH'''
,''' returned:'''
,''' 2,2,SOUTH'''
,''''''
,'''robot.move()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''2,1,SOUTH'''
,''' returned:'''
,''' 2,1,SOUTH'''
,''''''
,'''robot.right()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''2,1,WEST'''
,''' returned:'''
,''' 2,1,WEST'''
,''''''
,'''robot.move()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''1,1,WEST'''
,''' returned:'''
,''' 1,1,WEST'''
,''''''
,'''robot.move()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''0,1,WEST'''
,''' returned:'''
,''' 0,1,WEST'''
,''''''
,'''robot.move()'''
,''' returned:'''
,''' False'''
,'''robot.report()'''
,'''0,1,WEST'''
,''' returned:'''
,''' 0,1,WEST'''
,''''''
,'''robot.left()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''0,1,SOUTH'''
,''' returned:'''
,''' 0,1,SOUTH'''
,''''''
,'''robot.move()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''0,0,SOUTH'''
,''' returned:'''
,''' 0,0,SOUTH'''
,''''''
,'''robot.move()'''
,''' returned:'''
,''' False'''
,'''robot.report()'''
,'''0,0,SOUTH'''
,''' returned:'''
,''' 0,0,SOUTH'''
,''''''
,'''robot.place(FlatSurface(width=3, depth=3), 10, 10, 'north')'''
,''' returned:'''
,''' False'''
,'''robot.report()'''
,'''0,0,SOUTH'''
,''' returned:'''
,''' 0,0,SOUTH'''
,''''''
,'''robot.place(FlatSurface(width=3, depth=3), 1, 1, 'west')'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''1,1,WEST'''
,''' returned:'''
,''' 1,1,WEST'''
,''''''
,'''robot.right()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''1,1,NORTH'''
,''' returned:'''
,''' 1,1,NORTH'''
,''''''
,'''robot.right()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''1,1,EAST'''
,''' returned:'''
,''' 1,1,EAST'''
,''''''
,'''robot.left()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''1,1,NORTH'''
,''' returned:'''
,''' 1,1,NORTH'''
,''''''
,'''robot.left()'''
,''' returned:'''
,''' True'''
,'''robot.report()'''
,'''1,1,WEST'''
,''' returned:'''
,''' 1,1,WEST'''
,''''''
,''''''
]):
print("testD PASSED")
return True
else:
print("testD *FAILED*")
return False
def parse_args_using_argparse() -> argparse.Namespace:
'''Parse cmd line args using rules defined by
argparse.ArgumentParser().add_argument() calls.
(argparse makes --help txt, outputs error msgs for invalid args, etc)
Returns object (argparse.Namespace) containing member variables whose
values have been set via the parsing process.
Use method operator to access args via returned object (eg args.arg1)
Names of methods follow "long" cmd line option name,
or "dest" parameter of .add_argument() call if supplied.
https://docs.python.org/3/howto/argparse.htm
https://docs.python.org/3/library/argparse.html#the-add-
argument-method'''
parser = argparse.ArgumentParser()
# only adding one argument
parser.add_argument('-t' # short
,'--test_all' # long (test_all is default
# var name in progrma)
,action='store_true' # True if flag found else False
# help str shown when -h used
,help='run all tests in the module. output to stdout'
)
# Can add extra cmd line args here (as if you had passed them into main)
# example:
# parser.parse_args(['--date_range','05/05/2005','06/06/2006'])
args = parser.parse_args() # haven't set any extra args
return args
if __name__ == "__main__":
args = parse_args_using_argparse()
if args.test_all:
testA()
testB()
testC()
testD()
#-------------------------------------------------------------------------
# END OF FILE
#-------------------------------------------------------------------------