|
| 1 | +"""Unit test for all language interfaces.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | + |
| 8 | +from ..constants import OUTPUT_RESULT_PREFIX |
| 9 | +from .file import LANGUAGE_INTERFACES |
| 10 | +from .web import get_code_snippets |
| 11 | + |
| 12 | +TEST_CASES = [ |
| 13 | + "add-binary", |
| 14 | + "add-two-numbers", |
| 15 | + "add-two-promises", |
| 16 | + "build-array-from-permutation", |
| 17 | + "climbing-stairs", |
| 18 | + "concatenation-of-array", |
| 19 | + "defanging-an-ip-address", |
| 20 | + "display-the-first-three-rows", |
| 21 | + "divide-two-integers", |
| 22 | + "final-value-of-variable-after-performing-operations", |
| 23 | + "find-the-index-of-the-first-occurrence-in-a-string", |
| 24 | + "hand-of-straights", |
| 25 | + "integer-to-roman", |
| 26 | + "length-of-last-word", |
| 27 | + "longest-palindromic-substring", |
| 28 | + "longest-substring-without-repeating-characters", |
| 29 | + "merge-sorted-array", |
| 30 | + "merge-two-sorted-lists", |
| 31 | + "n-queens", |
| 32 | + "minimize-maximum-pair-sum-in-array", |
| 33 | + "modify-columns", |
| 34 | + "number-of-good-pairs", |
| 35 | + "plus-one", |
| 36 | + "remove-duplicates-from-sorted-list", |
| 37 | + "return-length-of-arguments-passed", |
| 38 | + "reverse-string", |
| 39 | + "score-of-a-string", |
| 40 | + "search-insert-position", |
| 41 | + "string-to-integer-atoi", |
| 42 | + "two-sum", |
| 43 | +] |
| 44 | + |
| 45 | + |
| 46 | +class TestProjectGeneration(unittest.TestCase): |
| 47 | + """Test case for the project generation process.""" |
| 48 | + |
| 49 | + def conduct_tests(self, code_snippets: list[dict[str, str]]): |
| 50 | + """Generates, compiles and runs the solution code for each supported language.""" |
| 51 | + for template_data in code_snippets: |
| 52 | + language = template_data["langSlug"] |
| 53 | + interface = LANGUAGE_INTERFACES.get(language) |
| 54 | + if interface is None: |
| 55 | + continue |
| 56 | + |
| 57 | + # print(f"Testing {language} interface...") |
| 58 | + interface.create_project(template_data["code"]) |
| 59 | + if interface.compile_command is not None: |
| 60 | + subprocess.run(interface.compile_command, check=True) |
| 61 | + result = subprocess.run( |
| 62 | + interface.test_command, check=True, capture_output=True |
| 63 | + ) |
| 64 | + self.assertMultiLineEqual( |
| 65 | + result.stdout.decode().strip(), |
| 66 | + f"{OUTPUT_RESULT_PREFIX} {interface.default_output}", |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def generate_test(test_case: str): |
| 71 | + """Generates a test function for a specific LeetCode problem.""" |
| 72 | + |
| 73 | + def test(self: TestProjectGeneration): |
| 74 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 75 | + # print(f"\nGenerating {test_case} in directory {temp_dir}...") |
| 76 | + os.chdir(temp_dir) |
| 77 | + self.conduct_tests(get_code_snippets(test_case)) |
| 78 | + |
| 79 | + return test |
| 80 | + |
| 81 | + |
| 82 | +def register_tests(): |
| 83 | + """Registers all LeetCode problems as separate test cases.""" |
| 84 | + for test_case in TEST_CASES: |
| 85 | + test_name = f"test_{test_case.replace('-', '_')}" |
| 86 | + setattr(TestProjectGeneration, test_name, generate_test(test_case)) |
| 87 | + |
| 88 | + |
| 89 | +register_tests() |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + unittest.main() |
0 commit comments