from typing import Any, Dict
import pytest
from pynecone.components.tags import CondTag, Tag
from pynecone.event import EventChain, EventHandler, EventSpec
from pynecone.var import BaseVar, Var
def mock_event(arg):
pass
@pytest.mark.parametrize(
"prop,formatted",
[
("string", '"string"'),
("{wrapped_string}", "{wrapped_string}"),
(True, "{true}"),
(False, "{false}"),
(123, "{123}"),
(3.14, "{3.14}"),
([1, 2, 3], "{[1, 2, 3]}"),
(["a", "b", "c"], '{["a", "b", "c"]}'),
({"a": 1, "b": 2, "c": 3}, '{{"a": 1, "b": 2, "c": 3}}'),
(
EventChain(events=[EventSpec(handler=EventHandler(fn=mock_event))]),
'{() => Event([E("mock_event", {})])}',
),
(
EventChain(
events=[
EventSpec(
handler=EventHandler(fn=mock_event),
local_args=("e",),
args=(("arg", "e.target.value"),),
)
]
),
'{(e) => Event([E("mock_event", {arg:e.target.value})])}',
),
({"a": "red", "b": "blue"}, '{{"a": "red", "b": "blue"}}'),
(BaseVar(name="var", type_="int"), "{var}"),
(
BaseVar(
name="_",
type_=Any,
state="",
is_local=True,
is_string=False,
),
"{_}",
),
(BaseVar(name='state.colors["a"]', type_="str"), '{state.colors["a"]}'),
({"a": BaseVar(name="val", type_="str")}, '{{"a": val}}'),
({"a": BaseVar(name='"val"', type_="str")}, '{{"a": "val"}}'),
(
{"a": BaseVar(name='state.colors["val"]', type_="str")},
'{{"a": state.colors["val"]}}',
),
],
)
def test_format_prop(prop: Var, formatted: str):
"""Test that the formatted value of an prop is correct.
Args:
prop: The prop to test.
formatted: The expected formatted value.
"""
assert Tag.format_prop(prop) == formatted
@pytest.mark.parametrize(
"props,formatted",
[
({}, ""),
({"key": 1}, "key={1}"),
({"key": "value"}, 'key="value"'),
({"key": True, "key2": "value2"}, 'key={true}\nkey2="value2"'),
],
)
def test_format_props(props: Dict[str, Var], formatted: str, windows_platform: bool):
"""Test that the formatted props are correct.
Args:
props: The props to test.
formatted: The expected formatted props.
windows_platform: Whether the system is windows.
"""
assert Tag(props=props).format_props() == (
formatted.replace("\n", "\r\n") if windows_platform else formatted
)
@pytest.mark.parametrize(
"prop,valid",
[
(1, True),
(3.14, True),
("string", True),
(False, True),
([], True),
({}, False),
(None, False),
],
)
def test_is_valid_prop(prop: Var, valid: bool):
"""Test that the prop is valid.
Args:
prop: The prop to test.
valid: The expected validity of the prop.
"""
assert Tag.is_valid_prop(prop) == valid
def test_add_props():
"""Test that the props are added."""
tag = Tag().add_props(key="value", key2=42, invalid=None, invalid2={})
assert tag.props["key"] == Var.create("value")
assert tag.props["key2"] == Var.create(42)
assert "invalid" not in tag.props
assert "invalid2" not in tag.props
@pytest.mark.parametrize(
"tag,expected",
[
(Tag(), ">"),
(Tag(name="br"), "
"),
(Tag(contents="hello"), "<>hello>"),
(Tag(name="h1", contents="hello"), "