|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import shutil |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from code2logic import analyze_project |
| 7 | +from code2logic.generators import YAMLGenerator |
| 8 | +from logic2code.generator import CodeGenerator as Logic2CodeGenerator |
| 9 | +from logic2code.generator import GeneratorConfig as Logic2CodeGeneratorConfig |
| 10 | +from logic2test.generator import GeneratorConfig as Logic2TestGeneratorConfig |
| 11 | +from logic2test.generator import TestGenerator as Logic2TestGenerator |
| 12 | + |
| 13 | + |
| 14 | +def _repo_root() -> Path: |
| 15 | + return Path(__file__).resolve().parents[1] |
| 16 | + |
| 17 | + |
| 18 | +def _copy_example_sample_project(tmp_path: Path) -> Path: |
| 19 | + src = _repo_root() / "examples" / "code2logic" / "sample_project" |
| 20 | + dst = tmp_path / "sample_project" |
| 21 | + shutil.copytree(src, dst) |
| 22 | + return dst |
| 23 | + |
| 24 | + |
| 25 | +def _write_code2logic_compact_yaml(project_dir: Path, out_file: Path) -> None: |
| 26 | + project = analyze_project(str(project_dir), use_treesitter=False, verbose=False) |
| 27 | + yaml_str = YAMLGenerator().generate(project, compact=True, detail="standard") |
| 28 | + out_file.write_text(yaml_str, encoding="utf-8") |
| 29 | + |
| 30 | + |
| 31 | +def test_e2e_pipeline_code2logic_logic2test_logic2code(tmp_path: Path) -> None: |
| 32 | + project_dir = _copy_example_sample_project(tmp_path) |
| 33 | + logic_file = tmp_path / "project.c2l.yaml" |
| 34 | + _write_code2logic_compact_yaml(project_dir, logic_file) |
| 35 | + assert logic_file.exists() |
| 36 | + assert logic_file.stat().st_size > 0 |
| 37 | + |
| 38 | + tests_out = tmp_path / "generated_tests" |
| 39 | + test_gen = Logic2TestGenerator(logic_file, config=Logic2TestGeneratorConfig(framework="pytest")) |
| 40 | + |
| 41 | + unit_result = test_gen.generate_unit_tests(tests_out / "unit") |
| 42 | + assert not unit_result.errors |
| 43 | + assert unit_result.files_generated > 0 |
| 44 | + assert unit_result.tests_generated > 0 |
| 45 | + |
| 46 | + integration_result = test_gen.generate_integration_tests(tests_out / "integration") |
| 47 | + assert not integration_result.errors |
| 48 | + assert (tests_out / "integration" / "test_integration.py").exists() |
| 49 | + |
| 50 | + property_result = test_gen.generate_property_tests(tests_out / "property") |
| 51 | + assert not property_result.errors |
| 52 | + assert (tests_out / "property" / "test_properties.py").exists() |
| 53 | + |
| 54 | + code_out = tmp_path / "generated_code" |
| 55 | + code_gen = Logic2CodeGenerator( |
| 56 | + logic_file, |
| 57 | + config=Logic2CodeGeneratorConfig( |
| 58 | + language="python", |
| 59 | + stubs_only=True, |
| 60 | + include_docstrings=True, |
| 61 | + include_type_hints=True, |
| 62 | + generate_init=True, |
| 63 | + preserve_structure=True, |
| 64 | + ), |
| 65 | + ) |
| 66 | + code_result = code_gen.generate(code_out) |
| 67 | + assert not code_result.errors |
| 68 | + assert code_result.files_generated > 0 |
| 69 | + assert (code_out / "calculator.py").exists() |
| 70 | + |
| 71 | + |
| 72 | +def test_e2e_logic2test_on_examples_input(tmp_path: Path) -> None: |
| 73 | + input_file = _repo_root() / "examples" / "logic2test" / "input" / "sample_project.c2l.yaml" |
| 74 | + assert input_file.exists() |
| 75 | + |
| 76 | + out_dir = tmp_path / "tests" |
| 77 | + gen = Logic2TestGenerator(input_file, config=Logic2TestGeneratorConfig(framework="pytest")) |
| 78 | + result = gen.generate_unit_tests(out_dir) |
| 79 | + |
| 80 | + assert not result.errors |
| 81 | + assert result.files_generated > 0 |
| 82 | + assert any(Path(p).suffix == ".py" for p in result.output_files) |
| 83 | + |
| 84 | + |
| 85 | +def test_e2e_logic2code_on_examples_input(tmp_path: Path) -> None: |
| 86 | + input_file = _repo_root() / "examples" / "logic2code" / "input" / "sample_project.c2l.yaml" |
| 87 | + assert input_file.exists() |
| 88 | + |
| 89 | + out_dir = tmp_path / "generated_code" |
| 90 | + gen = Logic2CodeGenerator( |
| 91 | + input_file, |
| 92 | + config=Logic2CodeGeneratorConfig( |
| 93 | + language="python", |
| 94 | + stubs_only=True, |
| 95 | + include_docstrings=True, |
| 96 | + include_type_hints=True, |
| 97 | + generate_init=True, |
| 98 | + preserve_structure=True, |
| 99 | + ), |
| 100 | + ) |
| 101 | + result = gen.generate(out_dir) |
| 102 | + |
| 103 | + assert not result.errors |
| 104 | + assert result.files_generated > 0 |
| 105 | + assert (out_dir / "calculator.py").exists() |
0 commit comments