test_compiler.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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(
  38. import_dict: utils.ImportDict, output: str, windows_platform: bool
  39. ):
  40. """Test the compile_imports function.
  41. Args:
  42. import_dict: The import dictionary.
  43. output: The expected output.
  44. windows_platform: whether system is windows.
  45. """
  46. assert utils.compile_imports(import_dict) == (
  47. output.replace("\n", "\r\n") if windows_platform else output
  48. )
  49. @pytest.mark.parametrize(
  50. "name,value,output",
  51. [
  52. ("foo", "bar", 'const foo = "bar"'),
  53. ("num", 1, "const num = 1"),
  54. ("check", False, "const check = false"),
  55. ("arr", [1, 2, 3], "const arr = [1, 2, 3]"),
  56. ("obj", {"foo": "bar"}, 'const obj = {"foo": "bar"}'),
  57. ],
  58. )
  59. def test_compile_constant_declaration(name: str, value: str, output: str):
  60. """Test the compile_constant_declaration function.
  61. Args:
  62. name: The name of the constant.
  63. value: The value of the constant.
  64. output: The expected output.
  65. """
  66. assert utils.compile_constant_declaration(name, value) == output