gdscript-transpiler/tests/test_transforms.py
Claude Code 27396acf47 release(forgejo-.forgejo/): 🔖 Publish new release with updated metadata, README, and source code + tests
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
2026-03-25 22:43:42 -07:00

150 lines
4.9 KiB
Python

"""Tests for GDScript to TypeScript line-level transforms."""
from lilith_gdscript_transpiler.transforms import (
apply_simple_transforms,
convert_math,
convert_operators,
convert_string_ops,
strip_gd_types,
to_camel,
)
class TestToCamel:
def test_simple(self) -> None:
assert to_camel("snake_case") == "snakeCase"
def test_single_word(self) -> None:
assert to_camel("foo") == "foo"
def test_multi_segment(self) -> None:
assert to_camel("update_moisture_wind") == "updateMoistureWind"
def test_leading_underscore_stripped(self) -> None:
assert to_camel("_update_temps") == "updateTemps"
def test_already_single(self) -> None:
assert to_camel("x") == "x"
class TestConvertMath:
def test_clamp(self) -> None:
assert convert_math("clamp(x, 0.0, 1.0)") == "Math.min(1.0, Math.max(0.0, x))"
def test_clampi(self) -> None:
assert convert_math("clampi(n, 0, 10)") == "Math.min(10, Math.max(0, n))"
def test_clampf(self) -> None:
assert convert_math("clampf(v, 0.0, 1.0)") == "Math.min(1.0, Math.max(0.0, v))"
def test_clampf_nested_parens(self) -> None:
# Critical regression: first arg wrapped in parens from optional-field transform
result = convert_math("clampf((tile.fish_stock ?? 0), 0.0, 1.0)")
assert result == "Math.min(1.0, Math.max(0.0, (tile.fish_stock ?? 0)))"
def test_clampf_complex_first_arg(self) -> None:
result = convert_math("clampf((nb.corruption_resistance_pct ?? 0), 0.0, 1.0)")
assert result == "Math.min(1.0, Math.max(0.0, (nb.corruption_resistance_pct ?? 0)))"
def test_minf(self) -> None:
assert convert_math("minf(a, b)") == "Math.min(a, b)"
def test_maxf(self) -> None:
assert convert_math("maxf(a, b)") == "Math.max(a, b)"
def test_absf(self) -> None:
assert convert_math("absf(x)") == "Math.abs(x)"
def test_signf(self) -> None:
assert convert_math("signf(x)") == "Math.sign(x)"
def test_float_cast_removed(self) -> None:
assert convert_math("float(n)") == "n"
def test_int_cast(self) -> None:
assert convert_math("int(x)") == "Math.floor(x)"
def test_inf(self) -> None:
assert convert_math("x > INF") == "x > Infinity"
def test_neg_inf(self) -> None:
assert convert_math("(-INF + x)") == "(-Infinity + x)"
class TestConvertOperators:
def test_and(self) -> None:
assert convert_operators("a and b") == "a && b"
def test_or(self) -> None:
assert convert_operators("a or b") == "a || b"
def test_not(self) -> None:
assert convert_operators("not x") == "!x"
def test_eq(self) -> None:
assert convert_operators("x == y") == "x === y"
def test_neq(self) -> None:
assert convert_operators("x != y") == "x !== y"
def test_compound(self) -> None:
result = convert_operators("a == b and c != d")
assert "===" in result
assert "&&" in result
assert "!==" in result
class TestStripGdTypes:
def test_typed_float_var(self) -> None:
assert strip_gd_types("var x: float = 1.0") == "let x = 1.0"
def test_typed_int_var(self) -> None:
assert strip_gd_types("var n: int = 0") == "let n = 0"
def test_typed_bool_var(self) -> None:
assert strip_gd_types("var flag: bool = false") == "let flag = false"
def test_untyped_var(self) -> None:
assert strip_gd_types("var foo = bar") == "let foo = bar"
def test_typed_const(self) -> None:
result = strip_gd_types("const X: Array = []")
assert result == "const X = []"
def test_typed_dict_var(self) -> None:
result = strip_gd_types("var d: Dictionary = {}")
assert result == "let d = {}"
class TestConvertStringOps:
def test_is_empty(self) -> None:
assert convert_string_ops("s.is_empty()") == "s.length === 0"
def test_size(self) -> None:
assert convert_string_ops("arr.size()") == "arr.length"
def test_strip_edges(self) -> None:
assert convert_string_ops("s.strip_edges()") == "s.trim()"
def test_append(self) -> None:
assert convert_string_ops("arr.append(x)") == "arr.push(x)"
class TestApplySimpleTransforms:
def test_typed_var_with_clamp(self) -> None:
result = apply_simple_transforms("var x: float = clampf(v, 0.0, 1.0)")
assert result == "let x = Math.min(1.0, Math.max(0.0, v))"
def test_eq_and_combined(self) -> None:
result = apply_simple_transforms("if a == b and c != d:")
assert "===" in result
assert "&&" in result
assert "!==" in result
def test_ternary(self) -> None:
result = apply_simple_transforms("var x: float = a if cond else b")
assert "(cond ? a : b)" in result
def test_passthrough_plain_line(self) -> None:
result = apply_simple_transforms("return total")
assert result == "return total"