Files
PROTEL/tests/test_transpiler.py

75 lines
2.3 KiB
Python

"""Transpiler tests correlated to PROTEL 2026 manuals."""
from pathlib import Path
from src.parser import parse_protel
from src.transpiler import transpile_to_c
EXAMPLES = Path(__file__).resolve().parent.parent / "examples"
def _transpile_example(name: str) -> str:
source = (EXAMPLES / name).read_text(encoding="utf-8")
tree = parse_protel(source)
return transpile_to_c(tree, source_name=name)
def test_transpile_factorial_contains_procedure():
c = _transpile_example("intro_1_3_factorial.P")
assert "int16_t factorial(" in c
assert "while (" in c
assert "return result;" in c
def test_transpile_types_emits_typedefs():
c = _transpile_example("intro_2_1_types.P")
assert "typedef int16_t integer;" in c
assert "typedef int16_t digit_register[16];" in c
assert "seconds_per_hour" in c
def test_transpile_assignment_maps_arrow_to_equals():
c = _transpile_example("intro_2_2_calculate_average.P")
assert "average =" in c
assert "return average;" in c
def test_transpile_case_emits_switch():
c = _transpile_example("intro_3_2_4_case.P")
assert "switch (n)" in c
assert "case 0:" in c
assert "default:" not in c
def test_transpile_for_emits_c_for_loop():
c = _transpile_example("intro_3_2_6_sum_table.P")
assert "for (int32_t i = 0;" in c
assert "return sum;" in c
def test_transpile_struct_emits_fields():
c = _transpile_example("ref_8_1_task_block_struct.P")
assert "typedef struct {" in c
assert "uint8_t priority;" in c
assert "uint8_t completed;" in c
assert "task_block * next_task;" in c
def test_transpile_class_type_emits_static_class_var():
c = _transpile_example("ref_oo_class_with_var.P")
assert "typedef struct class_with_var_t" in c
assert "static int16_t class_with_var_class_var" in c
assert "void class_with_var_class_method(int16_t parameter)" in c
assert "class_with_var_class_var = 5" in c
def test_transpile_class_method_invocation():
c = _transpile_example("ref_oo_class_method_invoke.P")
assert "class_with_class_method_class_method(5)" in c
assert "class_with_class_method_class_method(6)" in c
def test_transpile_emits_header():
c = _transpile_example("intro_1_3_factorial.P")
assert "Generated by PROTEL 2026 transpiler" in c
assert "#include <stdint.h>" in c