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 OutputTests(unittest.TestCase):
12 def add_example_output_test(filename):
14 compile_fur_to_c_result = subprocess.call([
17 os.path.join('examples', filename),
20 if compile_fur_to_c_result != 0:
21 raise Exception('Example "{}" did not compile'.format(filename))
23 compile_c_to_executable_result = subprocess.call([
25 os.path.join('examples', filename + '.c'),
28 if compile_c_to_executable_result != 0:
29 raise Exception('Example output "{}" did not compile'.format(filename + '.c'))
32 p = subprocess.Popen('./a.out', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
33 actual_stdout, actual_stderr = p.communicate()
35 expected_stdout_path = os.path.join('examples', filename + '.stdout.txt')
37 if os.path.isfile(expected_stdout_path):
38 with open(expected_stdout_path, 'rb') as f:
39 expected_stdout = f.read()
43 expected_stderr_path = os.path.join('examples', filename + '.stderr.txt')
45 if os.path.isfile(expected_stderr_path):
46 with open(expected_stderr_path, 'rb') as f:
47 expected_stderr = f.read()
51 self.assertEqual(expected_stderr, actual_stderr)
53 # We don't clean up the C file in the finally clause because it can be useful to have in case of errors
54 os.remove(os.path.join('examples', filename + '.c'))
62 setattr(OutputTests, 'test_' + filename, test)
64 class MemoryLeakTest(unittest.TestCase):
67 def add_example_memory_leak_test(filename):
69 compile_fur_to_c_result = subprocess.call([
72 os.path.join('examples', filename),
75 if compile_fur_to_c_result != 0:
76 raise Exception('Example "{}" did not compile'.format(filename))
78 compile_c_to_executable_result = subprocess.call([
81 os.path.join('examples', filename + '.c'),
84 if compile_c_to_executable_result != 0:
85 raise Exception('Example output "{}" did not compile'.format(filename + '.c'))
88 with open(os.devnull, 'w') as devnull:
90 actual_return = subprocess.call(
95 '--show-reachable=yes',
98 '--error-exitcode=666',
106 self.assertEqual(expected_return, actual_return)
108 # We don't clean up the C file in the finally clause because it can be useful to have in case of errors
109 os.remove(os.path.join('examples', filename + '.c'))
117 setattr(MemoryLeakTest, 'test_' + filename, test)
121 for entry in os.scandir('examples')
123 if entry.name.endswith('.fur')
126 for filename in filenames:
127 add_example_output_test(filename)
128 add_example_memory_leak_test(filename)