"""Tests for simple lambda support - starting with valid Python syntax.""" import pytest from ritz.parser import parse_component from ritz.llvm_codegen_signals import generate_llvm_ir def test_parse_lambda_calling_function(): """Test lambda that calls a function - valid Python syntax.""" source = """ from ritz.syntax import * @component def App(): count = signal(0) def increment(): count += 1 with Button(onclick=lambda: increment()): yield "Click" """ ir = parse_component(source) # Parser should recognize lambda in onclick and convert to synthetic function with_stmt = [s for s in ir["body"] if s["type"] == "with"][0] assert "onclick" in with_stmt["element"]["props"] # After lambda processing, onclick should reference a synthetic function onclick_ref = with_stmt["element"]["props"]["onclick"] assert onclick_ref["type"] == "name" assert onclick_ref["id"].startswith("_lambda_") def test_codegen_lambda_calling_function(): """Test LLVM codegen for lambda calling a function.""" source = """ from ritz.syntax import * @component def App(): count = signal(0) def increment(): count += 1 with Button(onclick=lambda: increment()): yield "+" """ ir = parse_component(source) llvm_ir = generate_llvm_ir(ir) # Should generate a lambda handler that calls increment assert "define void @_lambda_0_App()" in llvm_ir assert "call void @increment()" in llvm_ir assert "js_bind_event" in llvm_ir def test_lambda_inline_for_simple_setter(): """Test using lambda for simple signal setter calls.""" source = """ from ritz.syntax import * @component def Tabs(): tab = signal(0) def set_tab(value): tab = value with Button(onclick=lambda: set_tab(0)): yield "Tab 0" with Button(onclick=lambda: set_tab(1)): yield "Tab 1" """ ir = parse_component(source) # Should have two with statements with lambda onclick handlers (converted to functions) with_stmts = [s for s in ir["body"] if s["type"] == "with"] assert len([w for w in with_stmts if "onclick" in w["element"]["props"]]) == 2 # Check that both have synthetic lambda functions for with_stmt in with_stmts: if "onclick" in with_stmt["element"]["props"]: onclick_ref = with_stmt["element"]["props"]["onclick"] assert onclick_ref["type"] == "name" assert onclick_ref["id"].startswith("_lambda_")