|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +import pytest |
| 4 | + |
| 5 | +from code_generator import _safe_filename, save_output |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.parametrize( |
| 9 | + "program_name,language,expected", |
| 10 | + [ |
| 11 | + ("Two Sum", "python", "two_sum_in_python.txt"), |
| 12 | + ("Hello-World!", "JS", "hello-world_in_js.txt"), |
| 13 | + (" Weird Name ", "Go", "weird___name_in_go.txt"), |
| 14 | + ("", "rust", "generated_code_in_rust.txt"), |
| 15 | + ], |
| 16 | +) |
| 17 | +def test_safe_filename(program_name, language, expected): |
| 18 | + assert _safe_filename(program_name, language) == expected |
| 19 | + |
| 20 | + |
| 21 | +def test_save_output_writes_file(tmp_path: Path): |
| 22 | + # Change CWD to temporary directory to avoid polluting repo |
| 23 | + cwd = os.getcwd() |
| 24 | + os.chdir(tmp_path) |
| 25 | + try: |
| 26 | + program = "My App" |
| 27 | + lang = "Python" |
| 28 | + content = "print('hello')\n" |
| 29 | + out_path = save_output(program, lang, content) |
| 30 | + |
| 31 | + # Ensure path is inside output dir and file exists |
| 32 | + assert Path(out_path).exists() |
| 33 | + assert Path(out_path).parent.name == "output" |
| 34 | + assert Path(out_path).read_text(encoding="utf-8") == content |
| 35 | + finally: |
| 36 | + os.chdir(cwd) |
| 37 | + |
| 38 | + |
0 commit comments