|
| 1 | +# As of right now all tests can be run by either one of the following commands in the terminal: |
| 2 | +# python -m unittest discover -s ./tests -p test_*.py |
| 3 | +# python -m unittest discover -s tests |
| 4 | +from unittest import TestCase |
| 5 | +import subprocess |
| 6 | +import os |
| 7 | +import shutil |
| 8 | + |
| 9 | +class TestCompilerRun(TestCase): |
| 10 | + def setUp(self): |
| 11 | + self.test_working_directory = os.path.dirname(os.path.abspath(__file__)) |
| 12 | + self.output_directory = os.path.join(self.test_working_directory, 'output') |
| 13 | + self.input_directory = os.path.join(self.test_working_directory, 'input') |
| 14 | + self.compiler_path = os.path.join(self.test_working_directory, '../compiler.py') |
| 15 | + # if the output directory exists, delete it and if it doesn't exist, create it |
| 16 | + if os.path.exists(self.output_directory): |
| 17 | + shutil.rmtree(self.output_directory) |
| 18 | + os.makedirs(self.output_directory) |
| 19 | + |
| 20 | + def tearDown(self): |
| 21 | + shutil.rmtree(self.output_directory) |
| 22 | + |
| 23 | + def test_complete_compiler_run_should_be_successful(self): |
| 24 | + source_file = os.path.join(self.input_directory, 'successful.txt') |
| 25 | + output_file = os.path.join(self.output_directory, 'output_successful.py') |
| 26 | + subprocess.run(["poetry", "run", "python", self.compiler_path, source_file, "-o", output_file], check=True) |
| 27 | + self.assertTrue(os.path.exists(output_file)) |
| 28 | + with open(output_file, 'r') as file: |
| 29 | + content = file.read() |
| 30 | + self.assertIn("result = (1 + 2)", content) |
| 31 | + |
| 32 | + def test_compiler_run_with_invalid_syntax_should_fail(self): |
| 33 | + source_file = os.path.join(self.input_directory, 'invalid_syntax.txt') |
| 34 | + output_file = os.path.join(self.output_directory, 'output_invalid_syntax.py') |
| 35 | + with self.assertRaises(subprocess.CalledProcessError): |
| 36 | + subprocess.run(["poetry", "run", "python", self.compiler_path, source_file, "-o", output_file], check=True) |
0 commit comments