X-Git-Url: https://code.kerkeslager.com/?p=fur;a=blobdiff_plain;f=integration_tests.py;h=b5ad3219492c6013561d4c1c2dc7880cffff129a;hp=ed019bd5ea9d9a291eb688b52b861d9d2141b2d0;hb=732b4ed7f693a328509b94d7137f3210c9c4042f;hpb=6aad5a204967e0bd9f148872f98efe20bb785fcf diff --git a/integration_tests.py b/integration_tests.py index ed019bd..b5ad321 100644 --- a/integration_tests.py +++ b/integration_tests.py @@ -6,14 +6,60 @@ import unittest # Go to the directory of the current file so we know where we are in the filesystem os.chdir(os.path.dirname(os.path.abspath(__file__))) -class OutputTests(unittest.TestCase): +class InterpreterOutputTests(unittest.TestCase): pass -def add_example_output_test(filename): +def add_example_interpreter_output_test(filename): + def test(self): + try: + p = subprocess.Popen( + 'python main.py interpret {}'.format( + os.path.join('examples', filename), + ), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + + actual_stdout, actual_stderr = p.communicate() + + expected_stdout_path = os.path.join('examples', filename + '.stdout.txt') + + if os.path.isfile(expected_stdout_path): + with open(expected_stdout_path, 'rb') as f: + expected_stdout = f.read() + else: + expected_stdout = b'' + + expected_stderr_path = os.path.join('examples', filename + '.stderr.txt') + + if os.path.isfile(expected_stderr_path): + with open(expected_stderr_path, 'rb') as f: + expected_stderr = f.read() + else: + expected_stderr = b'' + + self.assertEqual(expected_stderr, actual_stderr) + + # We don't clean up the C file in the finally clause because it can be useful to have in case of errors + os.remove(os.path.join('examples', filename + '.c')) + + finally: + try: + os.remove('a.out') + except OSError: + pass + + setattr(InterpreterOutputTests, 'test_' + filename, test) + +class CompilerOutputTests(unittest.TestCase): + pass + +def add_example_compiler_output_test(filename): def test(self): compile_fur_to_c_result = subprocess.call([ 'python', 'main.py', + 'compile', os.path.join('examples', filename), ]) @@ -29,12 +75,26 @@ def add_example_output_test(filename): raise Exception('Example output "{}" did not compile'.format(filename + '.c')) try: - actual_output = subprocess.check_output(['./a.out']) + p = subprocess.Popen('./a.out', stdout=subprocess.PIPE, stderr=subprocess.PIPE) + actual_stdout, actual_stderr = p.communicate() - with open(os.path.join('examples', filename + '.output.txt'), 'rb') as f: - expected_output = f.read() + expected_stdout_path = os.path.join('examples', filename + '.stdout.txt') - self.assertEqual(expected_output, actual_output) + if os.path.isfile(expected_stdout_path): + with open(expected_stdout_path, 'rb') as f: + expected_stdout = f.read() + else: + expected_stdout = b'' + + expected_stderr_path = os.path.join('examples', filename + '.stderr.txt') + + if os.path.isfile(expected_stderr_path): + with open(expected_stderr_path, 'rb') as f: + expected_stderr = f.read() + else: + expected_stderr = b'' + + self.assertEqual(expected_stderr, actual_stderr) # We don't clean up the C file in the finally clause because it can be useful to have in case of errors os.remove(os.path.join('examples', filename + '.c')) @@ -45,7 +105,62 @@ def add_example_output_test(filename): except OSError: pass - setattr(OutputTests, 'test_' + filename, test) + setattr(CompilerOutputTests, 'test_' + filename, test) + +class MemoryLeakTests(unittest.TestCase): + pass + +def add_example_memory_leak_test(filename): + def test(self): + compile_fur_to_c_result = subprocess.call([ + 'python', + 'main.py', + os.path.join('examples', filename), + ]) + + if compile_fur_to_c_result != 0: + raise Exception('Example "{}" did not compile'.format(filename)) + + compile_c_to_executable_result = subprocess.call([ + 'gcc', + '-ggdb3', + os.path.join('examples', filename + '.c'), + ]) + + if compile_c_to_executable_result != 0: + raise Exception('Example output "{}" did not compile'.format(filename + '.c')) + + try: + with open(os.devnull, 'w') as devnull: + expected_return = 0 + actual_return = subprocess.call( + [ + 'valgrind', + '--tool=memcheck', + '--leak-check=yes', + '--show-reachable=yes', + '--num-callers=20', + '--track-fds=yes', + '--error-exitcode=42', + '-q', + './a.out', + ], + stdout=devnull, + stderr=devnull, + ) + + self.assertEqual(expected_return, actual_return) + + # We don't clean up the C file in the finally clause because it can be useful to have in case of errors + os.remove(os.path.join('examples', filename + '.c')) + + finally: + try: + os.remove('a.out') + except OSError: + pass + + setattr(MemoryLeakTests, 'test_' + filename, test) filenames = ( entry.name @@ -55,6 +170,8 @@ filenames = ( ) for filename in filenames: - add_example_output_test(filename) + add_example_compiler_output_test(filename) + add_example_interpreter_output_test(filename) + add_example_memory_leak_test(filename) unittest.main()