6 # Go to the directory of the current file so we know where we are in the filesystem
7 os.chdir(os.path.dirname(os.path.abspath(__file__)))
9 class InterpreterOutputTests(unittest.TestCase):
12 def add_example_interpreter_output_test(filename):
16 'python main.py interpret {}'.format(
17 os.path.join('examples', filename),
19 stdout=subprocess.PIPE,
20 stderr=subprocess.PIPE,
23 actual_stdout, actual_stderr = p.communicate()
25 expected_stdout_path = os.path.join('examples', filename + '.stdout.txt')
27 if os.path.isfile(expected_stdout_path):
28 with open(expected_stdout_path, 'rb') as f:
29 expected_stdout = f.read()
33 expected_stderr_path = os.path.join('examples', filename + '.stderr.txt')
35 if os.path.isfile(expected_stderr_path):
36 with open(expected_stderr_path, 'rb') as f:
37 expected_stderr = f.read()
41 self.assertEqual(expected_stderr, actual_stderr)
43 # We don't clean up the C file in the finally clause because it can be useful to have in case of errors
44 os.remove(os.path.join('examples', filename + '.c'))
52 setattr(InterpreterOutputTests, 'test_' + filename, test)
54 class CompilerOutputTests(unittest.TestCase):
57 def add_example_compiler_output_test(filename):
59 compile_fur_to_c_result = subprocess.call([
63 os.path.join('examples', filename),
66 if compile_fur_to_c_result != 0:
67 raise Exception('Example "{}" did not compile'.format(filename))
69 compile_c_to_executable_result = subprocess.call([
71 os.path.join('examples', filename + '.c'),
74 if compile_c_to_executable_result != 0:
75 raise Exception('Example output "{}" did not compile'.format(filename + '.c'))
78 p = subprocess.Popen('./a.out', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
79 actual_stdout, actual_stderr = p.communicate()
81 expected_stdout_path = os.path.join('examples', filename + '.stdout.txt')
83 if os.path.isfile(expected_stdout_path):
84 with open(expected_stdout_path, 'rb') as f:
85 expected_stdout = f.read()
89 expected_stderr_path = os.path.join('examples', filename + '.stderr.txt')
91 if os.path.isfile(expected_stderr_path):
92 with open(expected_stderr_path, 'rb') as f:
93 expected_stderr = f.read()
97 self.assertEqual(expected_stderr, actual_stderr)
99 # We don't clean up the C file in the finally clause because it can be useful to have in case of errors
100 os.remove(os.path.join('examples', filename + '.c'))
108 setattr(CompilerOutputTests, 'test_' + filename, test)
110 class MemoryLeakTests(unittest.TestCase):
113 def add_example_memory_leak_test(filename):
115 compile_fur_to_c_result = subprocess.call([
118 os.path.join('examples', filename),
121 if compile_fur_to_c_result != 0:
122 raise Exception('Example "{}" did not compile'.format(filename))
124 compile_c_to_executable_result = subprocess.call([
127 os.path.join('examples', filename + '.c'),
130 if compile_c_to_executable_result != 0:
131 raise Exception('Example output "{}" did not compile'.format(filename + '.c'))
134 with open(os.devnull, 'w') as devnull:
136 actual_return = subprocess.call(
141 '--show-reachable=yes',
144 '--error-exitcode=42',
152 self.assertEqual(expected_return, actual_return)
154 # We don't clean up the C file in the finally clause because it can be useful to have in case of errors
155 os.remove(os.path.join('examples', filename + '.c'))
163 setattr(MemoryLeakTests, 'test_' + filename, test)
167 for entry in os.scandir('examples')
169 if entry.name.endswith('.fur')
172 for filename in filenames:
173 add_example_compiler_output_test(filename)
174 add_example_interpreter_output_test(filename)
175 add_example_memory_leak_test(filename)