69 lines
1.5 KiB
Python
69 lines
1.5 KiB
Python
"""Tests for match/case statement parsing."""
|
|
|
|
import pytest
|
|
from ritz.parser import parse_component
|
|
|
|
|
|
def test_match_statement():
|
|
"""Test parsing match statement."""
|
|
source = """
|
|
from ritz.syntax import *
|
|
|
|
@component
|
|
def Tabs():
|
|
tab = signal(0)
|
|
|
|
match tab:
|
|
case 0:
|
|
with P():
|
|
yield "First"
|
|
case 1:
|
|
with P():
|
|
yield "Second"
|
|
"""
|
|
ir = parse_component(source)
|
|
|
|
# Find the match statement
|
|
match_stmt = None
|
|
for stmt in ir["body"]:
|
|
if stmt["type"] == "match":
|
|
match_stmt = stmt
|
|
break
|
|
|
|
assert match_stmt is not None
|
|
assert match_stmt["subject"]["type"] == "name"
|
|
assert match_stmt["subject"]["id"] == "tab"
|
|
assert len(match_stmt["cases"]) == 2
|
|
|
|
|
|
def test_match_with_wildcard():
|
|
"""Test match with wildcard case."""
|
|
source = """
|
|
from ritz.syntax import *
|
|
|
|
@component
|
|
def Tabs():
|
|
tab = signal(0)
|
|
|
|
match tab:
|
|
case 0:
|
|
with P():
|
|
yield "Home"
|
|
case _:
|
|
with P():
|
|
yield "Other"
|
|
"""
|
|
ir = parse_component(source)
|
|
|
|
# Find the match statement
|
|
match_stmt = None
|
|
for stmt in ir["body"]:
|
|
if stmt["type"] == "match":
|
|
match_stmt = stmt
|
|
break
|
|
|
|
assert match_stmt is not None
|
|
assert len(match_stmt["cases"]) == 2
|
|
# Wildcard should be the last case
|
|
assert match_stmt["cases"][1]["pattern"]["type"] == "wildcard"
|