test_tag.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import platform
  2. from typing import Dict
  3. import pytest
  4. from pynecone.components import Box
  5. from pynecone.components.tags import CondTag, IterTag, Tag
  6. from pynecone.event import EventChain, EventHandler, EventSpec
  7. from pynecone.var import BaseVar, Var
  8. from pynecone.propcond import PropCond
  9. def mock_event(arg):
  10. pass
  11. @pytest.mark.parametrize(
  12. "prop,formatted",
  13. [
  14. ("string", '"string"'),
  15. ("{wrapped_string}", "{wrapped_string}"),
  16. (True, "{true}"),
  17. (False, "{false}"),
  18. (123, "{123}"),
  19. (3.14, "{3.14}"),
  20. ([1, 2, 3], "{[1, 2, 3]}"),
  21. (["a", "b", "c"], '{["a", "b", "c"]}'),
  22. ({"a": 1, "b": 2, "c": 3}, '{{"a": 1, "b": 2, "c": 3}}'),
  23. (
  24. EventChain(events=[EventSpec(handler=EventHandler(fn=mock_event))]),
  25. '{() => Event([E("mock_event", {})])}',
  26. ),
  27. (
  28. EventChain(
  29. events=[
  30. EventSpec(
  31. handler=EventHandler(fn=mock_event),
  32. local_args=("e",),
  33. args=(("arg", "e.target.value"),),
  34. )
  35. ]
  36. ),
  37. '{(e) => Event([E("mock_event", {arg:e.target.value})])}',
  38. ),
  39. (
  40. PropCond.create(
  41. cond=BaseVar(name="random_var", type_=str),
  42. prop1="true_value",
  43. prop2="false_value",
  44. ),
  45. "{random_var ? 'true_value' : 'false_value'}",
  46. ),
  47. ],
  48. )
  49. def test_format_value(prop: Var, formatted: str):
  50. """Test that the formatted value of an prop is correct.
  51. Args:
  52. prop: The prop to test.
  53. formatted: The expected formatted value.
  54. """
  55. assert Tag.format_prop(prop) == formatted
  56. @pytest.mark.parametrize(
  57. "props,formatted",
  58. [
  59. ({}, ""),
  60. ({"key": 1}, "key={1}"),
  61. ({"key": "value"}, 'key="value"'),
  62. ({"key": True, "key2": "value2"}, 'key={true}\nkey2="value2"'),
  63. ],
  64. )
  65. def test_format_props(props: Dict[str, Var], formatted: str, windows_platform: bool):
  66. """Test that the formatted props are correct.
  67. Args:
  68. props: The props to test.
  69. formatted: The expected formatted props.
  70. windows_platform: Whether the system is windows.
  71. """
  72. assert Tag(props=props).format_props() == (
  73. formatted.replace("\n", "\r\n") if windows_platform else formatted
  74. )
  75. @pytest.mark.parametrize(
  76. "prop,valid",
  77. [
  78. (1, True),
  79. (3.14, True),
  80. ("string", True),
  81. (False, True),
  82. ([], True),
  83. ({}, False),
  84. (None, False),
  85. ],
  86. )
  87. def test_is_valid_prop(prop: Var, valid: bool):
  88. """Test that the prop is valid.
  89. Args:
  90. prop: The prop to test.
  91. valid: The expected validity of the prop.
  92. """
  93. assert Tag.is_valid_prop(prop) == valid
  94. def test_add_props():
  95. """Test that the props are added."""
  96. tag = Tag().add_props(key="value", key2=42, invalid=None, invalid2={})
  97. assert tag.props["key"] == Var.create("value")
  98. assert tag.props["key2"] == Var.create(42)
  99. assert "invalid" not in tag.props
  100. assert "invalid2" not in tag.props
  101. @pytest.mark.parametrize(
  102. "tag,expected",
  103. [
  104. (Tag(), "</>"),
  105. (Tag(name="br"), "<br/>"),
  106. (Tag(contents="hello"), "<>hello</>"),
  107. (Tag(name="h1", contents="hello"), "<h1>hello</h1>"),
  108. (
  109. Tag(name="box", props={"color": "red", "textAlign": "center"}),
  110. '<box color="red"\ntextAlign="center"/>',
  111. ),
  112. (
  113. Tag(
  114. name="box",
  115. props={"color": "red", "textAlign": "center"},
  116. contents="text",
  117. ),
  118. '<box color="red"\ntextAlign="center">text</box>',
  119. ),
  120. ],
  121. )
  122. def test_format_tag(tag: Tag, expected: str, windows_platform: bool):
  123. """Test that the formatted tag is correct.
  124. Args:
  125. tag: The tag to test.
  126. expected: The expected formatted tag.
  127. windows_platform: Whether the system is windows.
  128. """
  129. expected = expected.replace("\n", "\r\n") if windows_platform else expected
  130. assert str(tag) == expected
  131. def test_format_cond_tag():
  132. """Test that the formatted cond tag is correct."""
  133. tag = CondTag(
  134. true_value=str(Tag(name="h1", contents="True content")),
  135. false_value=str(Tag(name="h2", contents="False content")),
  136. cond=BaseVar(name="logged_in", type_=bool),
  137. )
  138. assert str(tag) == "{logged_in ? <h1>True content</h1> : <h2>False content</h2>}"