test_on_action.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 taipy.gui.builder as tgb
  12. from taipy.gui import Gui, notify
  13. def test_builder_on_function(gui: Gui, test_client, helpers):
  14. def on_slider(state):
  15. notify(state, "success", f"Value: {state.value}")
  16. gui._bind_var_val("on_slider", on_slider)
  17. with tgb.Page(frame=None) as page:
  18. tgb.slider(value="{value}", on_change=on_slider) # type: ignore[attr-defined] # noqa: B023
  19. expected_list = ['<Slider','onChange="__lambda_'] # inner function is treated as lambda
  20. helpers.test_control_builder(gui, page, expected_list)
  21. def test_builder_on_lambda(gui: Gui, test_client, helpers):
  22. with tgb.Page(frame=None) as page:
  23. tgb.slider(value="{value}", on_change=lambda s: notify(s, "success", f"Lambda Value: {s.value}")) # type: ignore[attr-defined] # noqa: B023
  24. expected_list = ['<Slider','onChange="__lambda_']
  25. helpers.test_control_builder(gui, page, expected_list)
  26. def test_builder_callable_lambda(gui: Gui, test_client, helpers):
  27. with tgb.Page(frame=None) as page:
  28. table = tgb.table("{value}", row_class_name=lambda s: f"Lambda Value: {s.value}") # type: ignore[attr-defined] # noqa: B023
  29. assert table._is_callable("row_class_name")
  30. expected_list = ['<Table','rowClassName="__lambda_']
  31. helpers.test_control_builder(gui, page, expected_list)
  32. def test_builder_indexed_callable_lambda(gui: Gui, test_client, helpers):
  33. with tgb.Page(frame=None):
  34. table = tgb.table("{value}", cell_class_name__A=lambda s: f"Lambda Value: {s.value}") # type: ignore[attr-defined] # noqa: B023
  35. assert table._is_callable("cell_class_name__A")