Add integration tests
[fur] / integration_tests.py
1 import os
2 import os.path
3 import subprocess
4 import unittest
5
6 EXAMPLES_PATH = os.path.join(os.path.dirname(__file__), 'examples')
7
8 class OutputTests(unittest.TestCase):
9     pass
10
11 def add_example_output_test(filename):
12     def test(self):
13         compile_fur_to_c_result = subprocess.call([
14             'python',
15             'main.py',
16             os.path.join(EXAMPLES_PATH, filename),
17         ])
18
19         if compile_fur_to_c_result != 0:
20             raise Exception('Example "{}" did not compile'.format(filename))
21
22         compile_c_to_executable_result = subprocess.call([
23             'gcc',
24             os.path.join(EXAMPLES_PATH, filename + '.c'),
25         ])
26
27         if compile_c_to_executable_result != 0:
28             raise Exception('Example output "{}" did not compile'.format(filename + '.c'))
29
30         actual_output = subprocess.check_output(['./a.out'])
31
32         with open(os.path.join(EXAMPLES_PATH, filename + '.output.txt'), 'rb') as f:
33             expected_output = f.read()
34
35         self.assertEqual(expected_output, actual_output)
36
37     setattr(OutputTests, 'test_' + filename, test)
38
39 filenames = (
40     entry.name
41     for entry in os.scandir(EXAMPLES_PATH)
42     if entry.is_file()
43     if entry.name.endswith('.fur')
44 )
45
46 for filename in filenames:
47     add_example_output_test(filename)
48
49 unittest.main()