Parser: Support multiple components in one file, change args to params with defaults
This commit is contained in:
1 parent
635d466874
commit
81d51d5617
38 files changed
+2151
-455
No files matched your search
@@ -0,0 +1,120 @@
|
||||
"""Tests for conditional rendering (if statements)."""
|
||||
|
||||
import pytest
|
||||
from ritz.parser import parse_component
|
||||
|
||||
|
||||
def test_if_statement():
|
||||
"""Test parsing if statement for conditional rendering."""
|
||||
source = """
|
||||
from ritz.syntax import *
|
||||
|
||||
@component
|
||||
def Toggle():
|
||||
visible = signal(True)
|
||||
|
||||
if visible:
|
||||
with P():
|
||||
yield "I am visible!"
|
||||
"""
|
||||
ir = parse_component(source)
|
||||
|
||||
# Find the if statement
|
||||
if_stmt = None
|
||||
for stmt in ir["body"]:
|
||||
if stmt["type"] == "if":
|
||||
if_stmt = stmt
|
||||
break
|
||||
|
||||
assert if_stmt is not None
|
||||
assert if_stmt["test"]["type"] == "name"
|
||||
assert if_stmt["test"]["id"] == "visible"
|
||||
assert len(if_stmt["body"]) == 1
|
||||
assert if_stmt["body"][0]["type"] == "with"
|
||||
|
||||
|
||||
def test_if_else_statement():
|
||||
"""Test parsing if/else for conditional rendering."""
|
||||
source = """
|
||||
from ritz.syntax import *
|
||||
|
||||
@component
|
||||
def Toggle():
|
||||
visible = signal(True)
|
||||
|
||||
if visible:
|
||||
with P():
|
||||
yield "Shown"
|
||||
else:
|
||||
with P():
|
||||
yield "Hidden"
|
||||
"""
|
||||
ir = parse_component(source)
|
||||
|
||||
# Find the if statement
|
||||
if_stmt = None
|
||||
for stmt in ir["body"]:
|
||||
if stmt["type"] == "if":
|
||||
if_stmt = stmt
|
||||
break
|
||||
|
||||
assert if_stmt is not None
|
||||
assert len(if_stmt["body"]) == 1
|
||||
assert len(if_stmt["orelse"]) == 1
|
||||
assert if_stmt["body"][0]["element"]["tag"] == "P"
|
||||
assert if_stmt["orelse"][0]["element"]["tag"] == "P"
|
||||
|
||||
|
||||
def test_if_comparison():
|
||||
"""Test parsing if statement 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)
|
||||
|
||||
# Find the if statement
|
||||
if_stmt = None
|
||||
for stmt in ir["body"]:
|
||||
if stmt["type"] == "if":
|
||||
if_stmt = stmt
|
||||
break
|
||||
|
||||
assert if_stmt is not None
|
||||
assert if_stmt["test"]["type"] == "compare"
|
||||
assert if_stmt["test"]["op"] == "Gt"
|
||||
|
||||
|
||||
def test_nested_if():
|
||||
"""Test parsing nested if statements."""
|
||||
source = """
|
||||
from ritz.syntax import *
|
||||
|
||||
@component
|
||||
def Nested():
|
||||
a = signal(True)
|
||||
b = signal(False)
|
||||
|
||||
if a:
|
||||
if b:
|
||||
with P():
|
||||
yield "Both true"
|
||||
"""
|
||||
ir = parse_component(source)
|
||||
|
||||
# Find outer if
|
||||
outer_if = None
|
||||
for stmt in ir["body"]:
|
||||
if stmt["type"] == "if":
|
||||
outer_if = stmt
|
||||
break
|
||||
|
||||
assert outer_if is not None
|
||||
assert outer_if["body"][0]["type"] == "if"
|
||||
Reference in new issue
Block a user