Changes: - Component render functions return i32 (root element) instead of void - Nested components (yield SomeComponent()) now work correctly - Added main() entry point that calls top-level component and sets root - Updated runtime to call main() instead of render_ComponentName() Note: Active state in ternary expressions still needs reactive updates This commit fixes the nested component rendering issue All tests passing (51 passed, 2 skipped) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
83 lines
2.3 KiB
Python
83 lines
2.3 KiB
Python
"""Tests for wrapper component pattern to reduce repetition."""
|
|
|
|
import pytest
|
|
from ritz.parser import parse_component
|
|
from ritz.llvm_codegen_signals import generate_llvm_ir
|
|
|
|
|
|
def test_tab_button_wrapper_component():
|
|
"""Test wrapper component pattern for reducing tab button repetition."""
|
|
source = """
|
|
from ritz.syntax import *
|
|
|
|
@component
|
|
def TabButton(label, index, active, onclick):
|
|
\"\"\"Wrapper component for tab buttons with active state.\"\"\"
|
|
if active:
|
|
with Button(class_="tab active", onclick=onclick):
|
|
yield label
|
|
else:
|
|
with Button(class_="tab", onclick=onclick):
|
|
yield label
|
|
|
|
@component
|
|
def SimpleTabs():
|
|
tab_index = signal(0)
|
|
|
|
def set_tab(value):
|
|
tab_index = value
|
|
|
|
with Div(class_="tab-menu"):
|
|
yield TabButton("Home", 0, tab_index == 0, lambda: set_tab(0))
|
|
yield TabButton("Profile", 1, tab_index == 1, lambda: set_tab(1))
|
|
yield TabButton("Settings", 2, tab_index == 2, lambda: set_tab(2))
|
|
"""
|
|
ir = parse_component(source)
|
|
|
|
# Should parse both components
|
|
assert isinstance(ir, list)
|
|
assert len(ir) == 2
|
|
assert ir[0]["name"] == "TabButton"
|
|
assert ir[1]["name"] == "SimpleTabs"
|
|
|
|
# TabButton should have parameters
|
|
assert len(ir[0]["params"]) == 4
|
|
assert ir[0]["params"][0]["name"] == "label"
|
|
assert ir[0]["params"][1]["name"] == "index"
|
|
assert ir[0]["params"][2]["name"] == "active"
|
|
assert ir[0]["params"][3]["name"] == "onclick"
|
|
|
|
|
|
def test_tab_button_wrapper_codegen():
|
|
"""Test LLVM codegen for wrapper component pattern."""
|
|
source = """
|
|
from ritz.syntax import *
|
|
|
|
@component
|
|
def TabButton(label, active, onclick):
|
|
if active:
|
|
with Button(class_="tab active", onclick=onclick):
|
|
yield label
|
|
else:
|
|
with Button(class_="tab", onclick=onclick):
|
|
yield label
|
|
|
|
@component
|
|
def SimpleTabs():
|
|
tab_index = signal(0)
|
|
|
|
def set_tab(value):
|
|
tab_index = value
|
|
|
|
with Div():
|
|
yield TabButton("Home", tab_index == 0, lambda: set_tab(0))
|
|
"""
|
|
ir = parse_component(source)
|
|
llvm_ir = generate_llvm_ir(ir)
|
|
|
|
# Should generate render function for TabButton with parameters
|
|
assert "define i32 @render_TabButton(" in llvm_ir
|
|
|
|
# Should call TabButton from SimpleTabs with arguments
|
|
assert "call i32 @render_TabButton(" in llvm_ir
|