test_compiler.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from typing import List, Set
  2. import pytest
  3. from pynecone.compiler import utils
  4. from pynecone.utils import imports
  5. from pynecone.var import ImportVar
  6. @pytest.mark.parametrize(
  7. "fields,test_default,test_rest",
  8. [
  9. (
  10. {ImportVar(tag="axios", is_default=True)},
  11. "axios",
  12. set(),
  13. ),
  14. (
  15. {ImportVar(tag="foo"), ImportVar(tag="bar")},
  16. "",
  17. {"foo", "bar"},
  18. ),
  19. (
  20. {
  21. ImportVar(tag="axios", is_default=True),
  22. ImportVar(tag="foo"),
  23. ImportVar(tag="bar"),
  24. },
  25. "axios",
  26. {"foo", "bar"},
  27. ),
  28. ],
  29. )
  30. def test_compile_import_statement(
  31. fields: Set[ImportVar], test_default: str, test_rest: str
  32. ):
  33. """Test the compile_import_statement function.
  34. Args:
  35. fields: The fields to import.
  36. test_default: The expected output of default library.
  37. test_rest: The expected output rest libraries.
  38. """
  39. default, rest = utils.compile_import_statement(fields)
  40. assert default == test_default
  41. assert rest == test_rest
  42. @pytest.mark.parametrize(
  43. "import_dict,test_dicts",
  44. [
  45. ({}, []),
  46. (
  47. {"axios": {ImportVar(tag="axios", is_default=True)}},
  48. [{"lib": "axios", "default": "axios", "rest": set()}],
  49. ),
  50. (
  51. {"axios": {ImportVar(tag="foo"), ImportVar(tag="bar")}},
  52. [{"lib": "axios", "default": "", "rest": {"foo", "bar"}}],
  53. ),
  54. (
  55. {
  56. "axios": {
  57. ImportVar(tag="axios", is_default=True),
  58. ImportVar(tag="foo"),
  59. ImportVar(tag="bar"),
  60. },
  61. "react": {ImportVar(tag="react", is_default=True)},
  62. },
  63. [
  64. {"lib": "axios", "default": "axios", "rest": {"foo", "bar"}},
  65. {"lib": "react", "default": "react", "rest": set()},
  66. ],
  67. ),
  68. (
  69. {"": {ImportVar(tag="lib1.js"), ImportVar(tag="lib2.js")}},
  70. [
  71. {"lib": "lib1.js", "default": "", "rest": set()},
  72. {"lib": "lib2.js", "default": "", "rest": set()},
  73. ],
  74. ),
  75. (
  76. {
  77. "": {ImportVar(tag="lib1.js"), ImportVar(tag="lib2.js")},
  78. "axios": {ImportVar(tag="axios", is_default=True)},
  79. },
  80. [
  81. {"lib": "lib1.js", "default": "", "rest": set()},
  82. {"lib": "lib2.js", "default": "", "rest": set()},
  83. {"lib": "axios", "default": "axios", "rest": set()},
  84. ],
  85. ),
  86. ],
  87. )
  88. def test_compile_imports(import_dict: imports.ImportDict, test_dicts: List[dict]):
  89. """Test the compile_imports function.
  90. Args:
  91. import_dict: The import dictionary.
  92. test_dicts: The expected output.
  93. """
  94. imports = utils.compile_imports(import_dict)
  95. for import_dict, test_dict in zip(imports, test_dicts):
  96. assert import_dict["lib"] == test_dict["lib"]
  97. assert import_dict["default"] == test_dict["default"]
  98. assert import_dict["rest"] == test_dict["rest"]
  99. # @pytest.mark.parametrize(
  100. # "name,value,output",
  101. # [
  102. # ("foo", "bar", 'const foo = "bar"'),
  103. # ("num", 1, "const num = 1"),
  104. # ("check", False, "const check = false"),
  105. # ("arr", [1, 2, 3], "const arr = [1, 2, 3]"),
  106. # ("obj", {"foo": "bar"}, 'const obj = {"foo": "bar"}'),
  107. # ],
  108. # )
  109. # def test_compile_constant_declaration(name: str, value: str, output: str):
  110. # """Test the compile_constant_declaration function.
  111. # Args:
  112. # name: The name of the constant.
  113. # value: The value of the constant.
  114. # output: The expected output.
  115. # """
  116. # assert utils.compile_constant_declaration(name, value) == output