test_tgb.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import re
  12. import typing as t
  13. from unittest.mock import patch
  14. from taipy.gui import Gui
  15. from taipy.gui.extension import Element, ElementLibrary, ElementProperty, PropertyType
  16. class TgbLibrary(ElementLibrary):
  17. elements = {
  18. "e1": Element(
  19. "s1",
  20. {
  21. "b1": ElementProperty(PropertyType.boolean, doc_string="e1.b1 doc"),
  22. "b2": ElementProperty(PropertyType.dynamic_boolean),
  23. "s1": ElementProperty(PropertyType.string),
  24. "s2": ElementProperty(PropertyType.dynamic_string),
  25. "d1": ElementProperty(PropertyType.dict),
  26. "d2": ElementProperty(PropertyType.dynamic_dict),
  27. },
  28. "E1",
  29. doc_string="e1 doc",
  30. ),
  31. "e2": Element(
  32. "x",
  33. {
  34. "p1": ElementProperty(PropertyType.any),
  35. "p2": ElementProperty(PropertyType.any),
  36. "p3": ElementProperty(PropertyType.any, type_hint="Union[bool,str]"),
  37. },
  38. "E2",
  39. ),
  40. }
  41. def get_name(self) -> str:
  42. return "test_ext_tgb"
  43. def get_elements(self) -> t.Dict[str, Element]:
  44. return TgbLibrary.elements
  45. def test_tgb_generation(gui: Gui, test_client, helpers):
  46. from taipy.gui.extension.__main__ import generate_doc
  47. library = TgbLibrary()
  48. api = generate_doc(library)
  49. assert "def e1(" in api, "Missing element e1"
  50. assert "s1" in api, "Missing property s1"
  51. assert re.search(r"\(\s*s1\s*:", api), "Property s1 should be the default property"
  52. assert re.search(r"b1:\s*t.Optional\[t.Union\[bool", api), "Incorrect property type for b1"
  53. assert re.search(r"b2:\s*t.Optional\[t.Union\[bool", api), "Incorrect property type for b2"
  54. assert re.search(r"s1:\s*t.Optional\[str\]", api), "Incorrect property type for s1"
  55. assert re.search(r"s2:\s*t.Optional\[str\]", api), "Incorrect property type for s2"
  56. assert re.search(r"d1:\s*t.Optional\[t.Union\[dict", api), "Incorrect property type for d1"
  57. assert re.search(r"d2:\s*t.Optional\[t.Union\[dict", api), "Incorrect property type for d2"
  58. assert "e1 doc" in api, "Missing doc for e1"
  59. assert "def e2(" in api, "Missing element e2"
  60. assert re.search(r"\(\s*p1\s*:", api), "Wrong default property in e2"
  61. assert re.search(r"p3:\s*t\.Union", api), "Wrong type hint for property p3 in e2"
  62. def test_tgb_generation_entry_point(gui: Gui, test_client, helpers):
  63. import os
  64. import tempfile
  65. from taipy.gui.extension.__main__ import main
  66. temp_file = tempfile.NamedTemporaryFile(delete=False)
  67. temp_file.close()
  68. with patch("sys.argv", ["main", "generate_tgb", "extlib_test", temp_file.name]):
  69. assert main() == 0
  70. os.remove(temp_file.name)