test_callable.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. from taipy.gui.utils.callable import _function_name, _is_function, _is_unnamed_function
  12. def my_function():
  13. pass
  14. class my_class:
  15. pass
  16. class my_callable_class:
  17. def __call__(self):
  18. pass
  19. def test__is_unnamed_function():
  20. assert _is_unnamed_function(my_function) is False
  21. assert _is_unnamed_function(lambda x: x) is True
  22. assert _is_unnamed_function("a") is False
  23. def test__is_function():
  24. assert _is_function(my_function) is True
  25. assert _is_function(lambda x: x) is True
  26. assert _is_function("a") is False
  27. def test__function_name():
  28. assert _function_name(my_function) == "my_function"
  29. assert _function_name(lambda x: x) == "<lambda>"
  30. assert _function_name("a") == "a"
  31. assert _function_name(1) == "1"
  32. assert _function_name(1.0) == "1.0"
  33. assert _function_name(True) == "True"
  34. assert _function_name(False) == "False"
  35. assert _function_name(None) == "None"
  36. assert _function_name([]) == "[]"
  37. assert _function_name({}) == "{}"
  38. assert _function_name(set()) == "set()"
  39. assert _function_name(tuple()) == "()" # noqa C408
  40. assert _function_name(object) == "object"
  41. assert _function_name(object()).startswith("<object ")
  42. assert _function_name(my_class) == "my_class"
  43. assert _function_name(my_class()).startswith("<tests.gui.gui_specific.test_callable.my_class ")
  44. assert _function_name(my_callable_class) == "my_callable_class"
  45. assert _function_name(my_callable_class()) == "<instance of my_callable_class>"