"""Tests for ternary expression support in props.""" import pytest from ritz.parser import parse_component from ritz.llvm_codegen_signals import generate_llvm_ir def test_parse_ternary_in_prop(): """Test parsing ternary expression in element prop.""" source = """ from ritz.syntax import * @component def Toggle(): active = signal(1) with Button(class_='btn active' if active else 'btn'): yield "Click" """ ir = parse_component(source) # Find the with statement with_stmt = [s for s in ir["body"] if s["type"] == "with"][0] # Check that class_ prop contains a ternary (ifexp) class_prop = with_stmt["element"]["props"]["class_"] assert class_prop["type"] == "ifexp" assert class_prop["test"]["id"] == "active" assert class_prop["body"]["value"] == "btn active" assert class_prop["orelse"]["value"] == "btn" def test_codegen_ternary_in_prop(): """Test LLVM codegen for ternary expression in prop.""" source = """ from ritz.syntax import * @component def Toggle(): active = signal(1) with Button(class_='btn active' if active else 'btn'): yield "Click" """ ir = parse_component(source) llvm_ir = generate_llvm_ir(ir) # Should generate code to evaluate the condition assert "icmp" in llvm_ir # Comparison for active signal # Should select between the two string values # This will likely use select instruction or branching assert "btn active" in llvm_ir assert "btn" in llvm_ir def test_ternary_with_comparison(): """Test ternary with comparison expression.""" source = """ from ritz.syntax import * @component def Counter(): count = signal(0) with Div(class_='high' if count > 5 else 'low'): yield "Value" """ ir = parse_component(source) # Find the with statement with_stmt = [s for s in ir["body"] if s["type"] == "with"][0] # Check ternary structure class_prop = with_stmt["element"]["props"]["class_"] assert class_prop["type"] == "ifexp" assert class_prop["test"]["type"] == "compare" assert class_prop["test"]["op"] == "Gt"