test_library.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 inspect
  12. import typing as t
  13. from pathlib import Path
  14. import pytest
  15. from taipy.gui import Gui
  16. from taipy.gui.extension import Element, ElementLibrary, ElementProperty, PropertyType
  17. from taipy.gui.extension.library import _ElementWithInnerProps
  18. def render_xhtml_4_my_library(properties: t.Dict[str, t.Any]) -> str:
  19. return f"<h1>{properties.get('value', '')}</h1>"
  20. def render_xhtml_4_my_library_fail(properties: t.Dict[str, t.Any]) -> str:
  21. return f"<h1>{properties.get('value', '')}</h1"
  22. class MyLibrary(ElementLibrary):
  23. elts = {
  24. "testinput": Element(
  25. "value",
  26. {
  27. "value": ElementProperty(PropertyType.dynamic_string, "Fred"),
  28. "multiline": ElementProperty(PropertyType.boolean, False),
  29. "broadcast": ElementProperty(PropertyType.broadcast, "broadcast"),
  30. },
  31. "Input",
  32. ),
  33. "title": Element(
  34. "value",
  35. {
  36. "value": ElementProperty(PropertyType.string, ""),
  37. },
  38. "h1",
  39. render_xhtml=render_xhtml_4_my_library,
  40. ),
  41. "title_fail": Element(
  42. "value",
  43. {
  44. "value": ElementProperty(PropertyType.string, ""),
  45. },
  46. "h1",
  47. render_xhtml=render_xhtml_4_my_library_fail,
  48. ),
  49. "inner": _ElementWithInnerProps(
  50. "value",
  51. {"value": ElementProperty(PropertyType.string, "")},
  52. inner_properties={
  53. "with_property": ElementProperty(
  54. PropertyType.react,
  55. "{<tp:prop:value>}",
  56. ),
  57. },
  58. ),
  59. }
  60. def get_name(self) -> str:
  61. return "test_lib"
  62. def get_elements(self) -> t.Dict[str, Element]:
  63. return MyLibrary.elts
  64. def get_resource(self, name: str) -> Path:
  65. return Path(name)
  66. class MyBadLibrary(ElementLibrary):
  67. def get_name(self) -> str:
  68. return "bad name"
  69. def get_elements(self) -> t.Dict[str, Element]:
  70. return {}
  71. class MyGoodLibrary(ElementLibrary):
  72. def get_name(self) -> str:
  73. return "test_lib"
  74. def get_elements(self) -> t.Dict[str, Element]:
  75. return {}
  76. Gui.add_library(MyLibrary())
  77. def test_lib_input_md(gui: Gui, test_client, helpers):
  78. val = "" # noqa: F841
  79. gui._set_frame(inspect.currentframe())
  80. md_string = "<|{val}|test_lib.testinput|multiline|>"
  81. expected_list = [
  82. "<TestLib_Input",
  83. 'libClassName="test_lib-testinput"',
  84. "multiline={true}",
  85. 'defaultValue=""',
  86. "broadcast={_bc_broadcast}",
  87. "value={tpec_TpExPr_val_TPMDL_0}",
  88. ]
  89. helpers.test_control_md(gui, md_string, expected_list)
  90. def test_lib_xhtml_md(gui: Gui, test_client, helpers):
  91. val = "title" # noqa: F841
  92. gui._set_frame(inspect.currentframe())
  93. md_string = "<|{val}|test_lib.title|>"
  94. expected = [f"<h1>{val}</h1>"]
  95. helpers.test_control_md(gui, md_string, expected)
  96. def test_lib_xhtml_fail_md(gui: Gui, test_client, helpers):
  97. val = "title" # noqa: F841
  98. gui._set_frame(inspect.currentframe())
  99. md_string = "<|{val}|test_lib.title_fail|>"
  100. expected = ["title_fail.render_xhtml() did not return a valid XHTML string. unclosed token: line 1, column 9"]
  101. helpers.test_control_md(gui, md_string, expected)
  102. def test_lib_input_html_1(gui: Gui, test_client, helpers):
  103. val = "" # noqa: F841
  104. gui._set_frame(inspect.currentframe())
  105. html_string = '<test_lib:testinput value="{val}" multiline="true" />'
  106. expected_list = [
  107. "<TestLib_Input",
  108. "multiline={true}",
  109. 'defaultValue=""',
  110. "broadcast={_bc_broadcast}",
  111. "value={tpec_TpExPr_val_TPMDL_0}",
  112. "/>",
  113. ]
  114. helpers.test_control_html(gui, html_string, expected_list)
  115. def test_lib_input_html_2(gui: Gui, test_client, helpers):
  116. val = "" # noqa: F841
  117. gui._set_frame(inspect.currentframe())
  118. html_string = '<test_lib:testinput multiline="true">{val}</test_lib:testinput>'
  119. expected_list = [
  120. "<TestLib_Input",
  121. "multiline={true}",
  122. 'defaultValue=""',
  123. "broadcast={_bc_broadcast}",
  124. "value={tpec_TpExPr_val_TPMDL_0}",
  125. "/>",
  126. ]
  127. helpers.test_control_html(gui, html_string, expected_list)
  128. def test_lib_inner_md(gui: Gui, test_client, helpers):
  129. val = "title" # noqa: F841
  130. gui._set_frame(inspect.currentframe())
  131. md_string = "<|{val}|test_lib.inner|>"
  132. expected = [
  133. "<TestLib_Inner",
  134. "value={tpec_TpExPr_val_TPMDL_0}",
  135. "withProperty={tpec_TpExPr_val_TPMDL_0}",
  136. ]
  137. helpers.test_control_md(gui, md_string, expected)
  138. def test_lib_inner_no_value_md(gui: Gui, test_client, helpers):
  139. gui._set_frame(inspect.currentframe())
  140. md_string = "<|test_lib.inner|>"
  141. expected = ["<TestLib_Inner", "withProperty={tpec_TpExPr_None_TPMDL_0}"]
  142. helpers.test_control_md(gui, md_string, expected)
  143. def test_lib_bad_name():
  144. with pytest.raises(NameError):
  145. Gui.add_library(MyBadLibrary())
  146. def test_lib_good_name():
  147. Gui.add_library(MyGoodLibrary())
  148. def test_add_lib():
  149. Gui(libraries=[MyGoodLibrary()])