test_utils.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import pytest
  2. from pynecone import utils
  3. @pytest.mark.parametrize(
  4. "input,output",
  5. [
  6. ("", ""),
  7. ("hello", "hello"),
  8. ("Hello", "hello"),
  9. ("camelCase", "camel_case"),
  10. ("camelTwoHumps", "camel_two_humps"),
  11. ("_start_with_underscore", "_start_with_underscore"),
  12. ("__start_with_double_underscore", "__start_with_double_underscore"),
  13. ],
  14. )
  15. def test_to_snake_case(input: str, output: str):
  16. """Test converting strings to snake case.
  17. Args:
  18. input: The input string.
  19. output: The expected output string.
  20. """
  21. assert utils.to_snake_case(input) == output
  22. @pytest.mark.parametrize(
  23. "input,output",
  24. [
  25. ("", ""),
  26. ("hello", "hello"),
  27. ("Hello", "Hello"),
  28. ("snake_case", "snakeCase"),
  29. ("snake_case_two", "snakeCaseTwo"),
  30. ],
  31. )
  32. def test_to_camel_case(input: str, output: str):
  33. """Test converting strings to camel case.
  34. Args:
  35. input: The input string.
  36. output: The expected output string.
  37. """
  38. assert utils.to_camel_case(input) == output
  39. @pytest.mark.parametrize(
  40. "input,output",
  41. [
  42. ("", ""),
  43. ("hello", "Hello"),
  44. ("Hello", "Hello"),
  45. ("snake_case", "Snake Case"),
  46. ("snake_case_two", "Snake Case Two"),
  47. ],
  48. )
  49. def test_to_title(input: str, output: str):
  50. """Test converting strings to title case.
  51. Args:
  52. input: The input string.
  53. output: The expected output string.
  54. """
  55. assert utils.to_title(input) == output
  56. @pytest.mark.parametrize(
  57. "input,output",
  58. [
  59. ("{", "}"),
  60. ("(", ")"),
  61. ("[", "]"),
  62. ("<", ">"),
  63. ('"', '"'),
  64. ("'", "'"),
  65. ],
  66. )
  67. def test_get_close_char(input: str, output: str):
  68. """Test getting the close character for a given open character.
  69. Args:
  70. input: The open character.
  71. output: The expected close character.
  72. """
  73. assert utils.get_close_char(input) == output
  74. @pytest.mark.parametrize(
  75. "text,open,expected",
  76. [
  77. ("", "{", False),
  78. ("{wrap}", "{", True),
  79. ("{wrap", "{", False),
  80. ("{wrap}", "(", False),
  81. ("(wrap)", "(", True),
  82. ],
  83. )
  84. def test_is_wrapped(text: str, open: str, expected: bool):
  85. """Test checking if a string is wrapped in the given open and close characters.
  86. Args:
  87. text: The text to check.
  88. open: The open character.
  89. expected: Whether the text is wrapped.
  90. """
  91. assert utils.is_wrapped(text, open) == expected
  92. @pytest.mark.parametrize(
  93. "text,open,check_first,num,expected",
  94. [
  95. ("", "{", True, 1, "{}"),
  96. ("wrap", "{", True, 1, "{wrap}"),
  97. ("wrap", "(", True, 1, "(wrap)"),
  98. ("wrap", "(", True, 2, "((wrap))"),
  99. ("(wrap)", "(", True, 1, "(wrap)"),
  100. ("{wrap}", "{", True, 2, "{wrap}"),
  101. ("(wrap)", "{", True, 1, "{(wrap)}"),
  102. ("(wrap)", "(", False, 1, "((wrap))"),
  103. ],
  104. )
  105. def test_wrap(text: str, open: str, expected: str, check_first: bool, num: int):
  106. """Test wrapping a string.
  107. Args:
  108. text: The text to wrap.
  109. open: The open character.
  110. expected: The expected output string.
  111. check_first: Whether to check if the text is already wrapped.
  112. num: The number of times to wrap the text.
  113. """
  114. assert utils.wrap(text, open, check_first=check_first, num=num) == expected
  115. @pytest.mark.parametrize(
  116. "text,indent_level,expected",
  117. [
  118. ("", 2, ""),
  119. ("hello", 2, "hello"),
  120. ("hello\nworld", 2, " hello\n world\n"),
  121. ("hello\nworld", 4, " hello\n world\n"),
  122. (" hello\n world", 2, " hello\n world\n"),
  123. ],
  124. )
  125. def test_indent(text: str, indent_level: int, expected: str):
  126. """Test indenting a string.
  127. Args:
  128. text: The text to indent.
  129. indent_level: The number of spaces to indent by.
  130. expected: The expected output string.
  131. """
  132. assert utils.indent(text, indent_level) == expected
  133. @pytest.mark.parametrize(
  134. "condition,true_value,false_value,expected",
  135. [
  136. ("cond", "<C1>", '""', '{cond ? <C1> : ""}'),
  137. ("cond", "<C1>", "<C2>", "{cond ? <C1> : <C2>}"),
  138. ],
  139. )
  140. def test_format_cond(condition: str, true_value: str, false_value: str, expected: str):
  141. """Test formatting a cond.
  142. Args:
  143. condition: The condition to check.
  144. true_value: The value to return if the condition is true.
  145. false_value: The value to return if the condition is false.
  146. expected: The expected output string.
  147. """
  148. assert utils.format_cond(condition, true_value, false_value) == expected
  149. def test_merge_imports():
  150. """Test that imports are merged correctly."""
  151. d1 = {"react": {"Component"}}
  152. d2 = {"react": {"Component"}, "react-dom": {"render"}}
  153. d = utils.merge_imports(d1, d2)
  154. assert set(d.keys()) == {"react", "react-dom"}
  155. assert set(d["react"]) == {"Component"}
  156. assert set(d["react-dom"]) == {"render"}