Skip to content

Commit b180daf

Browse files
committed
Various cleanups and cosmetic changes to testing infrastructure
1 parent 6272628 commit b180daf

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

tests/test-correct.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import argparse
44
import os
5+
import sys
56
import subprocess
67

78
class bcolors:
@@ -12,11 +13,17 @@ class bcolors:
1213
FAIL = '\033[91m'
1314
ENDC = '\033[0m'
1415

16+
def colored(text: str, color: str) -> str:
17+
"""Return colored text if terminal supports it"""
18+
if sys.stdout.isatty():
19+
return f"{color}{text}{bcolors.ENDC}"
20+
return text
21+
1522
def print_ok(format):
16-
print(bcolors.OKGREEN + format + bcolors.ENDC)
23+
print(colored(format, bcolors.OKGREEN))
1724

1825
def print_fail(format):
19-
print(bcolors.FAIL + format + bcolors.ENDC)
26+
print(colored(format, bcolors.FAIL))
2027

2128
def extension(lang):
2229
if lang == "alan":
@@ -27,6 +34,8 @@ def extension(lang):
2734
return '.grc'
2835
elif lang == "llama":
2936
return '.lla'
37+
elif lang == "pcl":
38+
return '.pcl'
3039

3140
def run_test(language, compiler_path, test_dir):
3241
total = 0
@@ -41,16 +50,16 @@ def run_test(language, compiler_path, test_dir):
4150
src_file = os.path.join(test_dir, file)
4251
result_file = os.path.join(test_dir, basename + '.result')
4352

44-
# Compile the .grc file
53+
# Compile the source file
4554
print(f"Compiling {basename}", end=" ... ")
4655
compile_process = subprocess.run([compiler_path, src_file], text=True, capture_output=True)
4756

4857
# Check if the compile process had an error
4958
if compile_process.returncode == 0:
50-
print_ok("OK")
59+
print_ok("OK")
5160
else:
5261
failed += 1
53-
print_fail("FAILED")
62+
print_fail("FAILED")
5463
print(compile_process.stderr)
5564
continue
5665

@@ -66,10 +75,10 @@ def run_test(language, compiler_path, test_dir):
6675

6776
# Check if the run process had an error
6877
if run_process.returncode == 0:
69-
print_ok("OK")
78+
print_ok("OK")
7079
else:
7180
failed += 1
72-
print_fail("FAILED")
81+
print_fail("FAILED")
7382
print(run_process.stderr)
7483
continue
7584

@@ -88,13 +97,13 @@ def run_test(language, compiler_path, test_dir):
8897
ok += 1
8998
print_ok(f"Test passed for {src_file}")
9099

91-
print(bcolors.OKBLUE + f"\nTotal tested: {total} ({ok} correct and {failed} failed)" + bcolors.ENDC)
100+
print(colored(f"\nTotal tested: {total} ({ok} correct and {failed} failed)", bcolors.OKBLUE))
92101

93102

94103
if __name__ == "__main__":
95104
parser = argparse.ArgumentParser(description='Tests a Compiler for a Language by running all the tests residing in a Test Directory.'
96105
' Compiler should be an executable or script that takes as input a program of the language and creates an a.out executable in the directory where this test driver resides.')
97-
parser.add_argument('language', help='Currently one of: \'alan\', \'grace\' or \'llama\'.')
106+
parser.add_argument('language', help='Currently one of: \'alan\', \'dana\', \'grace\', \'llama\' or \'pcl\'.')
98107
parser.add_argument('compiler_path', help='The path to the compiler executable.')
99108
parser.add_argument('test_dir', help='The directory containing the test programs, .result outputs expected for each program and .input files to be used as stdin for each program if needed.')
100109
args = parser.parse_args()

tests/test-erroneous.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import argparse
44
import os
5+
import sys
56
import subprocess
67

78
class bcolors:
@@ -12,11 +13,17 @@ class bcolors:
1213
FAIL = '\033[91m'
1314
ENDC = '\033[0m'
1415

16+
def colored(text: str, color: str) -> str:
17+
"""Return colored text if terminal supports it"""
18+
if sys.stdout.isatty():
19+
return f"{color}{text}{bcolors.ENDC}"
20+
return text
21+
1522
def print_ok(format):
16-
print(bcolors.OKGREEN + format + bcolors.ENDC)
23+
print(colored(format, bcolors.OKGREEN))
1724

1825
def print_fail(format):
19-
print(bcolors.FAIL + format + bcolors.ENDC)
26+
print(colored(format, bcolors.FAIL))
2027

2128
def extension(lang):
2229
if lang == "alan":
@@ -27,6 +34,8 @@ def extension(lang):
2734
return '.grc'
2835
elif lang == "llama":
2936
return '.lla'
37+
elif lang == "pcl":
38+
return '.pcl'
3039

3140
def run_test(language, compiler_path, test_dir):
3241
total = 0
@@ -46,19 +55,19 @@ def run_test(language, compiler_path, test_dir):
4655
# Check if the compilation returned an error
4756
if compile_process.returncode != 0:
4857
ok += 1
49-
print_ok("Error (OK)")
58+
print_ok("Error (OK)")
5059
# print(compile_process.stderr)
5160
else:
5261
failed += 1
53-
print_fail("FAILED to detect the error")
62+
print_fail("FAILED to detect the error")
5463

55-
print(bcolors.OKBLUE + f"\nTotal tested: {total} ({ok} correct and {failed} failed)" + bcolors.ENDC)
64+
print(colored(f"\nTotal tested: {total} ({ok} correct and {failed} failed)", bcolors.OKBLUE))
5665

5766

5867
if __name__ == "__main__":
5968
parser = argparse.ArgumentParser(description='Tests a Compiler for a Language by compiling all test programs in a Directory.'
6069
' Compiler should be an executable that takes as input a program for the language.')
61-
parser.add_argument('language', help='Currently one of: \'alan\', \'grace\' or \'llama\'.')
70+
parser.add_argument('language', help='Currently one of: \'alan\', \'grace\', \'llama\' or \'pcl\'.')
6271
parser.add_argument('compiler_path', help='The path to the compiler executable.')
6372
parser.add_argument('test_dir', help='The directory containing the erroneous programs for the language.')
6473
args = parser.parse_args()

0 commit comments

Comments
 (0)