test_match.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import re
  2. from collections.abc import Mapping, Sequence
  3. import pytest
  4. import reflex as rx
  5. from reflex.components.component import Component
  6. from reflex.components.core.match import Match
  7. from reflex.state import BaseState
  8. from reflex.utils.exceptions import MatchTypeError
  9. from reflex.vars.base import Var
  10. class MatchState(BaseState):
  11. """A test state."""
  12. value: int = 0
  13. num: int = 5
  14. string: str = "random string"
  15. def test_match_components():
  16. """Test matching cases with return values as components."""
  17. match_case_tuples = (
  18. (1, rx.text("first value")),
  19. (2, 3, rx.text("second value")),
  20. ([1, 2], rx.text("third value")),
  21. ("random", rx.text("fourth value")),
  22. ({"foo": "bar"}, rx.text("fifth value")),
  23. (MatchState.num + 1, rx.text("sixth value")),
  24. rx.text("default value"),
  25. )
  26. match_comp = Match.create(MatchState.value, *match_case_tuples)
  27. assert isinstance(match_comp, Component)
  28. match_dict = match_comp.render()
  29. assert match_dict["name"] == "Fragment"
  30. [match_child] = match_dict["children"]
  31. assert match_child["name"] == "match"
  32. assert str(match_child["cond"]) == f"{MatchState.get_name()}.value_rx_state_"
  33. match_cases = match_child["match_cases"]
  34. assert len(match_cases) == 6
  35. assert match_cases[0][0]._js_expr == "1"
  36. assert match_cases[0][0]._var_type is int
  37. first_return_value_render = match_cases[0][1]
  38. assert first_return_value_render["name"] == "RadixThemesText"
  39. assert first_return_value_render["children"][0]["contents"] == '"first value"'
  40. assert match_cases[1][0]._js_expr == "2"
  41. assert match_cases[1][0]._var_type is int
  42. assert match_cases[1][1]._js_expr == "3"
  43. assert match_cases[1][1]._var_type is int
  44. second_return_value_render = match_cases[1][2]
  45. assert second_return_value_render["name"] == "RadixThemesText"
  46. assert second_return_value_render["children"][0]["contents"] == '"second value"'
  47. assert match_cases[2][0]._js_expr == "[1, 2]"
  48. assert match_cases[2][0]._var_type == Sequence[int]
  49. third_return_value_render = match_cases[2][1]
  50. assert third_return_value_render["name"] == "RadixThemesText"
  51. assert third_return_value_render["children"][0]["contents"] == '"third value"'
  52. assert match_cases[3][0]._js_expr == '"random"'
  53. assert match_cases[3][0]._var_type is str
  54. fourth_return_value_render = match_cases[3][1]
  55. assert fourth_return_value_render["name"] == "RadixThemesText"
  56. assert fourth_return_value_render["children"][0]["contents"] == '"fourth value"'
  57. assert match_cases[4][0]._js_expr == '({ ["foo"] : "bar" })'
  58. assert match_cases[4][0]._var_type == Mapping[str, str]
  59. fifth_return_value_render = match_cases[4][1]
  60. assert fifth_return_value_render["name"] == "RadixThemesText"
  61. assert fifth_return_value_render["children"][0]["contents"] == '"fifth value"'
  62. assert match_cases[5][0]._js_expr == f"({MatchState.get_name()}.num_rx_state_ + 1)"
  63. assert match_cases[5][0]._var_type is int
  64. fifth_return_value_render = match_cases[5][1]
  65. assert fifth_return_value_render["name"] == "RadixThemesText"
  66. assert fifth_return_value_render["children"][0]["contents"] == '"sixth value"'
  67. default = match_child["default"]
  68. assert default["name"] == "RadixThemesText"
  69. assert default["children"][0]["contents"] == '"default value"'
  70. @pytest.mark.parametrize(
  71. "cases, expected",
  72. [
  73. (
  74. (
  75. (1, "first"),
  76. (2, 3, "second value"),
  77. ([1, 2], "third-value"),
  78. ("random", "fourth_value"),
  79. ({"foo": "bar"}, "fifth value"),
  80. (MatchState.num + 1, "sixth value"),
  81. (f"{MatchState.value} - string", MatchState.string),
  82. (MatchState.string, f"{MatchState.value} - string"),
  83. "default value",
  84. ),
  85. f'(() => {{ switch (JSON.stringify({MatchState.get_name()}.value_rx_state_)) {{case JSON.stringify(1): return ("first"); break;case JSON.stringify(2): case JSON.stringify(3): return '
  86. '("second value"); break;case JSON.stringify([1, 2]): return ("third-value"); break;case JSON.stringify("random"): '
  87. 'return ("fourth_value"); break;case JSON.stringify(({ ["foo"] : "bar" })): return ("fifth value"); '
  88. f'break;case JSON.stringify(({MatchState.get_name()}.num_rx_state_ + 1)): return ("sixth value"); break;case JSON.stringify(({MatchState.get_name()}.value_rx_state_+" - string")): '
  89. f'return ({MatchState.get_name()}.string_rx_state_); break;case JSON.stringify({MatchState.get_name()}.string_rx_state_): return (({MatchState.get_name()}.value_rx_state_+" - string")); break;default: '
  90. 'return ("default value"); break;};})()',
  91. ),
  92. (
  93. (
  94. (1, "first"),
  95. (2, 3, "second value"),
  96. ([1, 2], "third-value"),
  97. ("random", "fourth_value"),
  98. ({"foo": "bar"}, "fifth value"),
  99. (MatchState.num + 1, "sixth value"),
  100. (f"{MatchState.value} - string", MatchState.string),
  101. (MatchState.string, f"{MatchState.value} - string"),
  102. MatchState.string,
  103. ),
  104. f'(() => {{ switch (JSON.stringify({MatchState.get_name()}.value_rx_state_)) {{case JSON.stringify(1): return ("first"); break;case JSON.stringify(2): case JSON.stringify(3): return '
  105. '("second value"); break;case JSON.stringify([1, 2]): return ("third-value"); break;case JSON.stringify("random"): '
  106. 'return ("fourth_value"); break;case JSON.stringify(({ ["foo"] : "bar" })): return ("fifth value"); '
  107. f'break;case JSON.stringify(({MatchState.get_name()}.num_rx_state_ + 1)): return ("sixth value"); break;case JSON.stringify(({MatchState.get_name()}.value_rx_state_+" - string")): '
  108. f'return ({MatchState.get_name()}.string_rx_state_); break;case JSON.stringify({MatchState.get_name()}.string_rx_state_): return (({MatchState.get_name()}.value_rx_state_+" - string")); break;default: '
  109. f"return ({MatchState.get_name()}.string_rx_state_); break;}};}})()",
  110. ),
  111. ],
  112. )
  113. def test_match_vars(cases, expected):
  114. """Test matching cases with return values as Vars.
  115. Args:
  116. cases: The match cases.
  117. expected: The expected var full name.
  118. """
  119. match_comp = Match.create(MatchState.value, *cases)
  120. assert isinstance(match_comp, Var)
  121. assert str(match_comp) == expected
  122. def test_match_on_component_without_default():
  123. """Test that matching cases with return values as components returns a Fragment
  124. as the default case if not provided.
  125. """
  126. from reflex.components.base.fragment import Fragment
  127. match_case_tuples = (
  128. (1, rx.text("first value")),
  129. (2, 3, rx.text("second value")),
  130. )
  131. match_comp = Match.create(MatchState.value, *match_case_tuples)
  132. assert isinstance(match_comp, Component)
  133. default = match_comp.render()["children"][0]["default"]
  134. assert isinstance(default, dict) and default["name"] == Fragment.__name__
  135. def test_match_on_var_no_default():
  136. """Test that an error is thrown when cases with return Values as Var do not have a default case."""
  137. match_case_tuples = (
  138. (1, "red"),
  139. (2, 3, "blue"),
  140. ([1, 2], "green"),
  141. )
  142. with pytest.raises(
  143. ValueError,
  144. match="For cases with return types as Vars, a default case must be provided",
  145. ):
  146. Match.create(MatchState.value, *match_case_tuples)
  147. @pytest.mark.parametrize(
  148. "match_case",
  149. [
  150. (
  151. (1, "red"),
  152. (2, 3, "blue"),
  153. "black",
  154. ([1, 2], "green"),
  155. ),
  156. (
  157. (1, rx.text("first value")),
  158. (2, 3, rx.text("second value")),
  159. ([1, 2], rx.text("third value")),
  160. rx.text("default value"),
  161. ("random", rx.text("fourth value")),
  162. ({"foo": "bar"}, rx.text("fifth value")),
  163. (MatchState.num + 1, rx.text("sixth value")),
  164. ),
  165. ],
  166. )
  167. def test_match_default_not_last_arg(match_case):
  168. """Test that an error is thrown when the default case is not the last arg.
  169. Args:
  170. match_case: The cases to match.
  171. """
  172. with pytest.raises(
  173. ValueError,
  174. match="rx.match should have tuples of cases and a default case as the last argument.",
  175. ):
  176. Match.create(MatchState.value, *match_case)
  177. @pytest.mark.parametrize(
  178. "match_case",
  179. [
  180. (
  181. (1, "red"),
  182. (2, 3, "blue"),
  183. ("green",),
  184. "black",
  185. ),
  186. (
  187. (1, rx.text("first value")),
  188. (2, 3, rx.text("second value")),
  189. ([1, 2],),
  190. rx.text("default value"),
  191. ),
  192. ],
  193. )
  194. def test_match_case_tuple_elements(match_case):
  195. """Test that a match has at least 2 elements(a condition and a return value).
  196. Args:
  197. match_case: The cases to match.
  198. """
  199. with pytest.raises(
  200. ValueError,
  201. match="A case tuple should have at least a match case element and a return value.",
  202. ):
  203. Match.create(MatchState.value, *match_case)
  204. @pytest.mark.parametrize(
  205. "cases, error_msg",
  206. [
  207. (
  208. (
  209. (1, rx.text("first value")),
  210. (2, 3, rx.text("second value")),
  211. ([1, 2], rx.text("third value")),
  212. ("random", "red"),
  213. ({"foo": "bar"}, "green"),
  214. (MatchState.num + 1, "black"),
  215. rx.text("default value"),
  216. ),
  217. 'Match cases should have the same return types. Case 3 with return value `"red"` of type '
  218. "<class 'reflex.vars.sequence.LiteralStringVar'> is not <class 'reflex.components.component.BaseComponent'>",
  219. ),
  220. (
  221. (
  222. ("random", "red"),
  223. ({"foo": "bar"}, "green"),
  224. (MatchState.num + 1, "black"),
  225. (1, rx.text("first value")),
  226. (2, 3, rx.text("second value")),
  227. ([1, 2], rx.text("third value")),
  228. rx.text("default value"),
  229. ),
  230. 'Match cases should have the same return types. Case 3 with return value `jsx( RadixThemesText, {as:"p"}, "first value" ,)` '
  231. "of type <class 'reflex.components.radix.themes.typography.text.Text'> is not <class 'reflex.vars.base.Var'>",
  232. ),
  233. ],
  234. )
  235. def test_match_different_return_types(cases: tuple, error_msg: str):
  236. """Test that an error is thrown when the return values are of different types.
  237. Args:
  238. cases: The match cases.
  239. error_msg: Expected error message.
  240. """
  241. with pytest.raises(MatchTypeError, match=re.escape(error_msg)):
  242. Match.create(MatchState.value, *cases)
  243. @pytest.mark.parametrize(
  244. "match_case",
  245. [
  246. (
  247. (1, "red"),
  248. (2, 3, "blue"),
  249. ([1, 2], "green"),
  250. "black",
  251. "white",
  252. ),
  253. (
  254. (1, rx.text("first value")),
  255. (2, 3, rx.text("second value")),
  256. ([1, 2], rx.text("third value")),
  257. ("random", rx.text("fourth value")),
  258. ({"foo": "bar"}, rx.text("fifth value")),
  259. (MatchState.num + 1, rx.text("sixth value")),
  260. rx.text("default value"),
  261. rx.text("another default value"),
  262. ),
  263. ],
  264. )
  265. def test_match_multiple_default_cases(match_case):
  266. """Test that there is only one default case.
  267. Args:
  268. match_case: the cases to match.
  269. """
  270. with pytest.raises(ValueError, match="rx.match can only have one default case."):
  271. Match.create(MatchState.value, *match_case)
  272. def test_match_no_cond():
  273. with pytest.raises(ValueError):
  274. _ = Match.create(None)