test_lambda.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 taipy.gui.builder as tgb
  12. from taipy.gui import Gui
  13. def test_builder_lambda(gui: Gui, test_client, helpers):
  14. message = {"a": "value A", "b": "value B"}
  15. gui._bind_var_val("message", message)
  16. with tgb.Page(frame=None) as page:
  17. for key in message:
  18. tgb.text(lambda message: str(message.get(key))) # type: ignore[attr-defined] # noqa: B023
  19. expected_list = ['defaultValue="value A"', 'defaultValue="value B"']
  20. helpers.test_control_builder(gui, page, expected_list)
  21. def test_builder_lambda_2(gui: Gui, test_client, helpers):
  22. message = {"a": "value A", "b": "value B"}
  23. gui._bind_var_val("message", message)
  24. with tgb.Page(frame=None) as page:
  25. for key in message:
  26. tgb.text(lambda message: str(message.get(key)), mode=lambda message: "mode " + str(message.get(key))) # type: ignore[attr-defined] # noqa: B023
  27. expected_list = ['defaultValue="value A"', 'defaultValue="value B"', 'mode="mode value A"', 'mode="mode value B"']
  28. helpers.test_control_builder(gui, page, expected_list)
  29. def test_builder_lambda_f_string(gui: Gui, test_client, helpers):
  30. value = 10
  31. gui._bind_var_val("value", value)
  32. with tgb.Page(frame=None) as page:
  33. tgb.text(lambda value: f"value is {value}") # type: ignore[attr-defined] # noqa: B023
  34. expected_list = ['defaultValue="value is 10"']
  35. helpers.test_control_builder(gui, page, expected_list)
  36. def test_builder_simple_lambda(gui: Gui, test_client, helpers):
  37. value = 10
  38. gui._bind_var_val("value", value)
  39. with tgb.Page(frame=None) as page:
  40. tgb.text(lambda value: value) # type: ignore[attr-defined] # noqa: B023
  41. expected_list = ['defaultValue="10"']
  42. helpers.test_control_builder(gui, page, expected_list)
  43. def test_builder_multiline_lambda(gui: Gui, test_client, helpers):
  44. value = 10
  45. gui._bind_var_val("value", value)
  46. with tgb.Page(frame=None) as page:
  47. tgb.text( # type: ignore[attr-defined] # noqa: B023
  48. lambda value: value)
  49. expected_list = ['defaultValue="10"']
  50. helpers.test_control_builder(gui, page, expected_list)