test_compiler.py 2.2 KB

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