Fixes: - Lambda functions now correctly pass arguments to handlers - Function parameters properly emitted in LLVM IR - Event handlers defined with parameter lists New feature - Ternary expressions: - Support Python ternary syntax (x if cond else y) in props - Eliminates repetitive if/else blocks for active states - Before: 6 lines per tab button - After: 1 line with ternary expression Example: class_='tab active' if active else 'tab' All tests passing (51 passed, 2 skipped) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
79 lines
2.1 KiB
Python
79 lines
2.1 KiB
Python
"""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"
|