Skip to content

Commit

Permalink
Extend the test infrastructure
Browse files Browse the repository at this point in the history
* Add `test-erroneous.py` script.
* Extend the `test-correct.py` script to be language agnostic.
  • Loading branch information
kostis committed Mar 26, 2024
1 parent a1e9b3c commit 7413404
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 11 deletions.
33 changes: 22 additions & 11 deletions tests/test-correct.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python3

import argparse
import subprocess
import os
import subprocess

class bcolors:
HEADER = '\033[95m'
Expand All @@ -18,21 +18,30 @@ def print_ok(format):
def print_fail(format):
print(bcolors.FAIL + format + bcolors.ENDC)

def extension(lang):
if lang == "alan":
return '.alan'
elif lang == "grace":
return '.grc'
elif lang == "llama":
return '.lla'

def run_test(language, compiler_path, test_dir):
total = 0
ok = 0
failed = 0
# Iterate over each .grc file in the test directory
ext = extension(language)
# Iterate over each file in the test directory
for file in os.listdir(test_dir):
if file.endswith('.grc'):
if file.endswith(ext):
total += 1
basename = file[:-4]
grc_file = os.path.join(test_dir, file)
basename = file[:-(len(ext))]
src_file = os.path.join(test_dir, file)
result_file = os.path.join(test_dir, basename + '.result')

# Compile the .grc file
print(f"Compiling {basename}", end=" ... ")
compile_process = subprocess.run([compiler_path, grc_file], text=True, capture_output=True)
compile_process = subprocess.run([compiler_path, src_file], text=True, capture_output=True)

# Check if the compile process had an error
if compile_process.returncode == 0:
Expand Down Expand Up @@ -68,22 +77,24 @@ def run_test(language, compiler_path, test_dir):

if run_process.stdout != expected_output:
failed += 1
print_fail(f"Test failed for {grc_file}")
print_fail(f"Test failed for {src_file}")
print("Expected:")
print(expected_output)
print("Got:")
print(run_process.stdout)
else:
ok += 1
print_ok(f"Test passed for {grc_file}")
print_ok(f"Test passed for {src_file}")

print(bcolors.OKBLUE + f"\nTotal tested: {total} ({ok} correct and {failed} failed)" + bcolors.ENDC)


if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Test a compiler. Compiler should be an executable that takes as an input the Grace program and outputs an a.out executable')
parser = argparse.ArgumentParser(description='Tests a Compiler for a Language by running all the tests residing in a Test Directory.'
' 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.')
parser.add_argument('language', help='Currently one of: \'alan\', \'grace\' or \'llama\'.')
parser.add_argument('compiler_path', help='The path to the compiler executable.')
parser.add_argument('test_dir', help='The directory containing the test files. The directory contains .grc programs, .result outputs expected for each program and .input files to be used as stdin for each program if needed.')
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.')
args = parser.parse_args()

run_test('grace', args.compiler_path, args.test_dir)
run_test(args.language, args.compiler_path, args.test_dir)
64 changes: 64 additions & 0 deletions tests/test-erroneous.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3

import argparse
import os
import subprocess

class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'

def print_ok(format):
print(bcolors.OKGREEN + format + bcolors.ENDC)

def print_fail(format):
print(bcolors.FAIL + format + bcolors.ENDC)

def extension(lang):
if lang == "alan":
return '.alan'
elif lang == "grace":
return '.grc'
elif lang == "llama":
return '.lla'

def run_test(language, compiler_path, test_dir):
total = 0
ok = 0
failed = 0
ext = extension(language)
# Iterate over each file in the test directory
for file in os.listdir(test_dir):
if file.endswith(ext):
total += 1
basename = file[:-(len(ext))]
src_file = os.path.join(test_dir, file)

print(f"Compiling {basename}", end=" ... ")
compile_process = subprocess.run([compiler_path, src_file], text=True, capture_output=True)

# Check if the compilation returned an error
if compile_process.returncode != 0:
ok += 1
print_ok("Error (OK)")
# print(compile_process.stderr)
else:
failed += 1
print_fail("FAILED to detect the error")

print(bcolors.OKBLUE + f"\nTotal tested: {total} ({ok} correct and {failed} failed)" + bcolors.ENDC)


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

run_test(args.language, args.compiler_path, args.test_dir)

0 comments on commit 7413404

Please sign in to comment.