Files
ritz/tests/test_llvm_codegen_conditional.py
Aaron SinclairandClaude e686903170 Fix reactive conditional rendering with proper branch swapping
Implements efficient reactive conditionals that pre-render branches once
and swap DOM nodes without recreation, enabling fast switching with
thousands of elements.

Key Fixes:
- Runtime now calls reevaluate_conditional_*() on signal changes
- Match/case swap logic uses phi nodes to load correct old case
- Previously hardcoded case_0 as old value, causing accumulation
- Branch render functions now wrap all content in container div

Implementation:
- Pre-render both if/else branches at init
- Store roots in globals (@cond_N_then_root, @cond_N_else_root)
- On signal change, swap DOM nodes via js_replace_at_marker()
- Match statements use switch/phi pattern to select old case

Tests:
- Added test_reactive_conditionals.py (5 new tests)
- Updated test_llvm_codegen_conditional.py for reactive infrastructure
- All 35 tests passing

Performance: No re-rendering on switches, perfect for dynamic content.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-15 10:32:40 -08:00

86 lines
2.1 KiB
Python

"""Tests for LLVM IR code generation with conditional rendering."""
import pytest
from ritz.parser import parse_component
from ritz.llvm_codegen_signals import generate_llvm_ir
def test_if_statement_codegen():
"""Test LLVM IR generation for if statement with reactive rendering."""
source = """
from ritz.syntax import *
@component
def Toggle():
visible = signal(1)
if visible:
with P():
yield "I am visible!"
"""
ir = parse_component(source)
llvm_ir = generate_llvm_ir(ir)
# Should have signal global (scoped to component)
assert "@signal_Toggle_visible = global i32 1" in llvm_ir
# Should have conditional branch
assert "br i1" in llvm_ir
# Should have reactive conditional infrastructure
assert "@cond_0_marker" in llvm_ir
assert "_init_conditional_0" in llvm_ir
assert "_render_cond_0_then" in llvm_ir
assert "_render_cond_0_else" in llvm_ir
def test_if_else_codegen():
"""Test LLVM IR generation for if/else with reactive rendering."""
source = """
from ritz.syntax import *
@component
def Toggle():
visible = signal(1)
if visible:
with P():
yield "Shown"
else:
with P():
yield "Hidden"
"""
ir = parse_component(source)
llvm_ir = generate_llvm_ir(ir)
# Should have conditional branch
assert "br i1" in llvm_ir
# Should have reactive infrastructure with both branches
assert "_render_cond_0_then" in llvm_ir
assert "_render_cond_0_else" in llvm_ir
assert "reevaluate_conditional_0" in llvm_ir
def test_if_comparison_codegen():
"""Test LLVM IR generation for if with comparison."""
source = """
from ritz.syntax import *
@component
def Counter():
count = signal(0)
if count > 5:
with P():
yield "High count!"
"""
ir = parse_component(source)
llvm_ir = generate_llvm_ir(ir)
# Should have icmp instruction for comparison
assert "icmp" in llvm_ir
# Should have greater-than comparison (sgt = signed greater than)
assert "sgt" in llvm_ir or ">" in llvm_ir