test_library.py 5.6 KB

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