|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # Copyright 2019 nomennescio |
3 | | -# MIT license |
4 | 3 |
|
5 | | -import glob, os, re |
| 4 | +import glob, os, re, sys |
6 | 5 |
|
7 | | -passed, failed = 0, 0 |
| 6 | +passed, failed, verbose, quiet = 0, 0, 0, 0 |
8 | 7 |
|
9 | 8 | def run (forthfile): |
10 | 9 | return os.popen ("gforth ../ttester-codewars.4th " + forthfile + " -e bye").readlines () |
11 | 10 |
|
12 | 11 | def timeless (lines): |
13 | 12 | return [re.sub(r'\d+', '0', l) if "<COMPLETEDIN::>" in l else l for l in lines] |
14 | 13 |
|
15 | | -def test (base): |
| 14 | +def test (filename): |
16 | 15 | global passed, failed |
17 | | - if (timeless (run (base + ".4th")) == timeless (open (base + ".expected").readlines ())): |
18 | | - print ("Pass: " + base) |
| 16 | + actual = run (filename) |
| 17 | + expected = open (os.path.splitext (filename)[0] + ".expected").readlines () |
| 18 | + if (timeless (actual) == timeless (expected)): |
| 19 | + if verbose: |
| 20 | + print ("Pass: " + filename) |
19 | 21 | passed += 1 |
20 | 22 | else: |
21 | | - print ("Fail: " + base) |
| 23 | + if (not quiet): |
| 24 | + print ("Fail: {0}\n==8<--".format (filename, "")) |
| 25 | + sys.stdout.write (''.join (actual)) |
| 26 | + if (not quiet): |
| 27 | + print ("\n==8<--") |
22 | 28 | failed += 1 |
23 | 29 |
|
| 30 | +# change to script directory |
24 | 31 | prevdir = os.getcwd () |
25 | 32 | os.chdir (os.path.dirname (os.path.abspath (__file__))) |
26 | 33 |
|
27 | | -files = [os.path.splitext (f) for f in glob.glob ("*.4th")] |
28 | | -for f,_ in files: |
| 34 | +# parse flags |
| 35 | +args=sys.argv[1:] |
| 36 | +if (len (args) and args[0]=="-v"): |
| 37 | + verbose=1 |
| 38 | + args.pop (0) |
| 39 | +if (len (args) and args[0]=="-q"): |
| 40 | + quiet=1 |
| 41 | + args.pop (0) |
| 42 | + |
| 43 | +# test named files, or all Forth files in directory |
| 44 | +files = args if len (args) else glob.glob ("*.4th") |
| 45 | +for f in files: |
29 | 46 | test (f) |
30 | 47 |
|
31 | | -if (failed==0): |
32 | | - print ("All {0} tests were successful".format (passed)) |
33 | | -else: |
34 | | - print ("{0} out of {1} tests failed".format (failed, failed+passed)) |
35 | | - |
| 48 | +# show totals |
| 49 | +if (verbose): |
| 50 | + if (failed==0): |
| 51 | + print ("All {0} tests were successful".format (passed)) |
| 52 | + else: |
| 53 | + print ("{0} out of {1} tests failed".format (failed, failed+passed)) |
| 54 | + |
| 55 | +# restore directory |
36 | 56 | os.chdir (prevdir) |
0 commit comments