Add integration tests
authorDavid Kerkeslager <kerkeslager@gmail.com>
Fri, 4 Aug 2017 10:15:50 +0000 (06:15 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Fri, 4 Aug 2017 10:15:50 +0000 (06:15 -0400)
examples/01_hello.fur.output.txt [new file with mode: 0644]
integration_tests.py [new file with mode: 0644]
main.py
templates/program.c

diff --git a/examples/01_hello.fur.output.txt b/examples/01_hello.fur.output.txt
new file mode 100644 (file)
index 0000000..dbe9dba
--- /dev/null
@@ -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 (file)
index 0000000..ddf444b
--- /dev/null
@@ -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 (file)
--- 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)
index 3d15076..63b0c56 100644 (file)
@@ -5,7 +5,7 @@
 {% if 'print' in builtins %}
 void builtin$print(const char* output)
 {
-  printf("%s\n", output);
+  printf("%s", output);
 }
 {% endif %}