test_compiler.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from typing import Set
  2. import pytest
  3. from pynecone.compiler import utils
  4. from pynecone.utils import imports
  5. @pytest.mark.parametrize(
  6. "lib,fields,output",
  7. [
  8. ("axios", {"axios"}, 'import axios from "axios"'),
  9. ("axios", {"foo", "bar"}, 'import {bar, foo} from "axios"'),
  10. ("axios", {"axios", "foo", "bar"}, 'import axios, {bar, foo} from "axios"'),
  11. ],
  12. )
  13. def test_compile_import_statement(lib: str, fields: Set[str], output: str):
  14. """Test the compile_import_statement function.
  15. Args:
  16. lib: The library name.
  17. fields: The fields to import.
  18. output: The expected output.
  19. """
  20. assert utils.compile_import_statement(lib, fields) == output
  21. @pytest.mark.parametrize(
  22. "import_dict,output",
  23. [
  24. ({}, ""),
  25. ({"axios": {"axios"}}, 'import axios from "axios"'),
  26. ({"axios": {"foo", "bar"}}, 'import {bar, foo} from "axios"'),
  27. (
  28. {"axios": {"axios", "foo", "bar"}, "react": {"react"}},
  29. 'import axios, {bar, foo} from "axios"\nimport react from "react"',
  30. ),
  31. ({"": {"lib1.js", "lib2.js"}}, 'import "lib1.js"\nimport "lib2.js"'),
  32. (
  33. {"": {"lib1.js", "lib2.js"}, "axios": {"axios"}},
  34. 'import "lib1.js"\nimport "lib2.js"\nimport axios from "axios"',
  35. ),
  36. ],
  37. )
  38. def test_compile_imports(
  39. import_dict: imports.ImportDict, output: str, windows_platform: bool
  40. ):
  41. """Test the compile_imports function.
  42. Args:
  43. import_dict: The import dictionary.
  44. output: The expected output.
  45. windows_platform: whether system is windows.
  46. """
  47. assert utils.compile_imports(import_dict) == (
  48. output.replace("\n", "\r\n") if windows_platform else output
  49. )
  50. @pytest.mark.parametrize(
  51. "name,value,output",
  52. [
  53. ("foo", "bar", 'const foo = "bar"'),
  54. ("num", 1, "const num = 1"),
  55. ("check", False, "const check = false"),
  56. ("arr", [1, 2, 3], "const arr = [1, 2, 3]"),
  57. ("obj", {"foo": "bar"}, 'const obj = {"foo": "bar"}'),
  58. ],
  59. )
  60. def test_compile_constant_declaration(name: str, value: str, output: str):
  61. """Test the compile_constant_declaration function.
  62. Args:
  63. name: The name of the constant.
  64. value: The value of the constant.
  65. output: The expected output.
  66. """
  67. assert utils.compile_constant_declaration(name, value) == output