Skip to content

Commit c1ed54a

Browse files
committed
Expand tests with verbose and quiet flags and passing of files on commandline
1 parent 685ea59 commit c1ed54a

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

test/run-tests.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,56 @@
11
#!/usr/bin/env python
22
# Copyright 2019 nomennescio
3-
# MIT license
43

5-
import glob, os, re
4+
import glob, os, re, sys
65

7-
passed, failed = 0, 0
6+
passed, failed, verbose, quiet = 0, 0, 0, 0
87

98
def run (forthfile):
109
return os.popen ("gforth ../ttester-codewars.4th " + forthfile + " -e bye").readlines ()
1110

1211
def timeless (lines):
1312
return [re.sub(r'\d+', '0', l) if "<COMPLETEDIN::>" in l else l for l in lines]
1413

15-
def test (base):
14+
def test (filename):
1615
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)
1921
passed += 1
2022
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<--")
2228
failed += 1
2329

30+
# change to script directory
2431
prevdir = os.getcwd ()
2532
os.chdir (os.path.dirname (os.path.abspath (__file__)))
2633

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:
2946
test (f)
3047

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
3656
os.chdir (prevdir)

0 commit comments

Comments
 (0)