X-Git-Url: https://code.kerkeslager.com/?p=fur;a=blobdiff_plain;f=integration_tests.py;h=b5ad3219492c6013561d4c1c2dc7880cffff129a;hp=25550f61f5f4b2c63c078d32fe8056677ef0866b;hb=732b4ed7f693a328509b94d7137f3210c9c4042f;hpb=c24be69b26aedc427c7831f150e86ca00182d8e0 diff --git a/integration_tests.py b/integration_tests.py index 25550f6..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), ]) @@ -59,7 +105,7 @@ def add_example_output_test(filename): except OSError: pass - setattr(OutputTests, 'test_' + filename, test) + setattr(CompilerOutputTests, 'test_' + filename, test) class MemoryLeakTests(unittest.TestCase): pass @@ -124,7 +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()