From 6c2894f00c8daca3d85c7ce850da711b9f7effc5 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Fri, 4 Aug 2017 06:15:50 -0400 Subject: [PATCH] Add integration tests --- examples/01_hello.fur.output.txt | 1 + integration_tests.py | 49 ++++++++++++++++++++++++++++++++ main.py | 2 +- templates/program.c | 2 +- 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 examples/01_hello.fur.output.txt create mode 100644 integration_tests.py diff --git a/examples/01_hello.fur.output.txt b/examples/01_hello.fur.output.txt new file mode 100644 index 0000000..dbe9dba --- /dev/null +++ b/examples/01_hello.fur.output.txt @@ -0,0 +1 @@ +Hello, world \ No newline at end of file diff --git a/integration_tests.py b/integration_tests.py new file mode 100644 index 0000000..ddf444b --- /dev/null +++ b/integration_tests.py @@ -0,0 +1,49 @@ +import os +import os.path +import subprocess +import unittest + +EXAMPLES_PATH = os.path.join(os.path.dirname(__file__), 'examples') + +class OutputTests(unittest.TestCase): + pass + +def add_example_output_test(filename): + def test(self): + compile_fur_to_c_result = subprocess.call([ + 'python', + 'main.py', + os.path.join(EXAMPLES_PATH, filename), + ]) + + if compile_fur_to_c_result != 0: + raise Exception('Example "{}" did not compile'.format(filename)) + + compile_c_to_executable_result = subprocess.call([ + 'gcc', + os.path.join(EXAMPLES_PATH, filename + '.c'), + ]) + + if compile_c_to_executable_result != 0: + raise Exception('Example output "{}" did not compile'.format(filename + '.c')) + + actual_output = subprocess.check_output(['./a.out']) + + with open(os.path.join(EXAMPLES_PATH, filename + '.output.txt'), 'rb') as f: + expected_output = f.read() + + self.assertEqual(expected_output, actual_output) + + setattr(OutputTests, 'test_' + filename, test) + +filenames = ( + entry.name + for entry in os.scandir(EXAMPLES_PATH) + if entry.is_file() + if entry.name.endswith('.fur') +) + +for filename in filenames: + add_example_output_test(filename) + +unittest.main() diff --git a/main.py b/main.py index 5d9f9de..c2592c6 100644 --- a/main.py +++ b/main.py @@ -16,7 +16,7 @@ transformed = transformation.transform(parsed) generated = generation.generate(transformed) assert source_path.endswith('.fur') -destination_path = source_path[:-4] + '.c' +destination_path = source_path + '.c' with open(destination_path, 'w') as f: f.write(generated) diff --git a/templates/program.c b/templates/program.c index 3d15076..63b0c56 100644 --- a/templates/program.c +++ b/templates/program.c @@ -5,7 +5,7 @@ {% if 'print' in builtins %} void builtin$print(const char* output) { - printf("%s\n", output); + printf("%s", output); } {% endif %} -- 2.20.1