test_slider.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. from taipy.gui import Gui
  13. def test_slider_md(gui: Gui, test_client, helpers):
  14. gui._bind_var_val("x", 10)
  15. md_string = "<|{x}|slider|>"
  16. expected_list = [
  17. "<Slider",
  18. 'updateVarName="_TpN_tpec_TpExPr_x_TPMDL_0',
  19. "defaultValue={10}",
  20. "value={_TpN_tpec_TpExPr_x_TPMDL_0}",
  21. ]
  22. helpers.test_control_md(gui, md_string, expected_list)
  23. def test_slider_with_min_max(gui: Gui, test_client, helpers):
  24. gui._bind_var_val("x", 0)
  25. md_string = "<|{x}|slider|min=-10|max=10|>"
  26. expected_list = ["<Slider", "min={-10.0}", "max={10.0}", "defaultValue={0}"]
  27. helpers.test_control_md(gui, md_string, expected_list)
  28. def test_slider_with_dict_labels_md(gui: Gui, helpers):
  29. sel = "Item 1" # noqa: F841
  30. labels = {"Item 1": "Label Start", "Item 3": "Label End"} # noqa: F841
  31. gui._set_frame(inspect.currentframe())
  32. md_string = "<|{sel}|slider|lov=Item 1;Item 2;Item 3|labels={labels}|>"
  33. expected_list = [
  34. "<Slider",
  35. 'labels="{&quot;Item 1&quot;: &quot;Label Start&quot;, &quot;Item 3&quot;: &quot;Label End&quot;}"',
  36. ]
  37. helpers.test_control_md(gui, md_string, expected_list)
  38. def test_slider_with_boolean_labels_md(gui: Gui, helpers):
  39. sel = "Item 1" # noqa: F841
  40. gui._set_frame(inspect.currentframe())
  41. md_string = "<|{sel}|slider|lov=Item 1;Item 2;Item 3|labels|>"
  42. expected_list = ["<Slider", "labels={true}"]
  43. helpers.test_control_md(gui, md_string, expected_list)
  44. def test_slider_items_md(gui: Gui, test_client, helpers):
  45. gui._bind_var_val("x", "Item 1")
  46. md_string = "<|{x}|slider|lov=Item 1;Item 2;Item 3|text_anchor=left|>"
  47. expected_list = [
  48. "<Slider",
  49. 'updateVarName="_TpLv_tpec_TpExPr_x_TPMDL_0"',
  50. "value={_TpLv_tpec_TpExPr_x_TPMDL_0}",
  51. 'defaultLov="[&quot;Item 1&quot;, &quot;Item 2&quot;, &quot;Item 3&quot;]"',
  52. 'defaultValue="[&quot;Item 1&quot;]"',
  53. 'textAnchor="left"',
  54. ]
  55. helpers.test_control_md(gui, md_string, expected_list)
  56. def test_slider_text_anchor_md(gui: Gui, test_client, helpers):
  57. gui._bind_var_val("x", "Item 1")
  58. md_string = "<|{x}|slider|text_anchor=NoNe|>"
  59. expected_list = [
  60. "<Slider",
  61. 'updateVarName="_TpN_tpec_TpExPr_x_TPMDL_0"',
  62. "value={_TpN_tpec_TpExPr_x_TPMDL_0}",
  63. 'textAnchor="none"',
  64. ]
  65. helpers.test_control_md(gui, md_string, expected_list)
  66. def test_slider_text_anchor_default_md(gui: Gui, test_client, helpers):
  67. gui._bind_var_val("x", "Item 1")
  68. md_string = "<|{x}|slider|items=Item 1|>"
  69. expected_list = [
  70. "<Slider",
  71. 'updateVarName="_TpN_tpec_TpExPr_x_TPMDL_0"',
  72. "value={_TpN_tpec_TpExPr_x_TPMDL_0}",
  73. 'textAnchor="bottom"',
  74. ]
  75. helpers.test_control_md(gui, md_string, expected_list)
  76. def test_slider_array_md(gui: Gui, test_client, helpers):
  77. gui._bind_var_val("x", [10, 20])
  78. md_string = "<|{x}|slider|>"
  79. expected_list = [
  80. "<Slider",
  81. 'updateVarName="_TpLn_tpec_TpExPr_x_TPMDL_0"',
  82. "value={_TpLn_tpec_TpExPr_x_TPMDL_0}",
  83. ]
  84. helpers.test_control_md(gui, md_string, expected_list)
  85. def test_slider_html_1(gui: Gui, test_client, helpers):
  86. gui._bind_var_val("x", 10)
  87. html_string = '<taipy:slider value="{x}" />'
  88. expected_list = [
  89. "<Slider",
  90. 'updateVarName="_TpN_tpec_TpExPr_x_TPMDL_0"',
  91. "defaultValue={10}",
  92. "value={_TpN_tpec_TpExPr_x_TPMDL_0}",
  93. ]
  94. helpers.test_control_html(gui, html_string, expected_list)
  95. def test_slider_html_2(gui: Gui, test_client, helpers):
  96. gui._bind_var_val("x", 10)
  97. html_string = "<taipy:slider>{x}</taipy:slider>"
  98. expected_list = [
  99. "<Slider",
  100. 'updateVarName="_TpN_tpec_TpExPr_x_TPMDL_0"',
  101. "defaultValue={10}",
  102. "value={_TpN_tpec_TpExPr_x_TPMDL_0}",
  103. ]
  104. helpers.test_control_html(gui, html_string, expected_list)