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):
19 os.path.join('examples', filename),
21 stdout=subprocess.PIPE,
22 stderr=subprocess.PIPE,
25 actual_stdout, actual_stderr = p.communicate()
27 expected_stdout_path = os.path.join('examples', filename + '.stdout.txt')
29 if os.path.isfile(expected_stdout_path):
30 with open(expected_stdout_path, 'rb') as f:
31 expected_stdout = f.read()
35 expected_stderr_path = os.path.join('examples', filename + '.stderr.txt')
37 if os.path.isfile(expected_stderr_path):
38 with open(expected_stderr_path, 'rb') as f:
39 expected_stderr = f.read()
43 self.assertEqual(expected_stderr, actual_stderr)
45 setattr(InterpreterOutputTests, 'test_' + filename[:-4], test)
47 class CompilerOutputTests(unittest.TestCase):
50 def add_example_compiler_output_test(filename):
52 compile_fur_to_c_result = subprocess.call([
56 os.path.join('examples', filename),
59 if compile_fur_to_c_result != 0:
60 raise Exception('Example "{}" did not compile'.format(filename))
62 compile_c_to_executable_result = subprocess.call([
64 os.path.join('examples', filename + '.c'),
67 if compile_c_to_executable_result != 0:
68 raise Exception('Example output "{}" did not compile'.format(filename + '.c'))
71 p = subprocess.Popen('./a.out', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
72 actual_stdout, actual_stderr = p.communicate()
74 expected_stdout_path = os.path.join('examples', filename + '.stdout.txt')
76 if os.path.isfile(expected_stdout_path):
77 with open(expected_stdout_path, 'rb') as f:
78 expected_stdout = f.read()
82 expected_stderr_path = os.path.join('examples', filename + '.stderr.txt')
84 if os.path.isfile(expected_stderr_path):
85 with open(expected_stderr_path, 'rb') as f:
86 expected_stderr = f.read()
90 self.assertEqual(expected_stderr, actual_stderr)
92 # We don't clean up the C file in the finally clause because it can be useful to have in case of errors
93 os.remove(os.path.join('examples', filename + '.c'))
101 setattr(CompilerOutputTests, 'test_' + filename[:-4], test)
103 class MemoryLeakTests(unittest.TestCase):
106 def add_example_memory_leak_test(filename):
108 compile_fur_to_c_result = subprocess.call([
111 os.path.join('examples', filename),
114 if compile_fur_to_c_result != 0:
115 raise Exception('Example "{}" did not compile'.format(filename))
117 compile_c_to_executable_result = subprocess.call([
120 os.path.join('examples', filename + '.c'),
123 if compile_c_to_executable_result != 0:
124 raise Exception('Example output "{}" did not compile'.format(filename + '.c'))
127 with open(os.devnull, 'w') as devnull:
129 actual_return = subprocess.call(
134 '--show-reachable=yes',
137 '--error-exitcode=42',
145 self.assertEqual(expected_return, actual_return)
147 # We don't clean up the C file in the finally clause because it can be useful to have in case of errors
148 os.remove(os.path.join('examples', filename + '.c'))
156 setattr(MemoryLeakTests, 'test_' + filename[:-4], test)
160 for entry in os.scandir('examples')
162 if entry.name.endswith('.fur')
165 for filename in filenames:
166 add_example_compiler_output_test(filename)
167 add_example_interpreter_output_test(filename)
168 add_example_memory_leak_test(filename)