test_node_path.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**************************************************************************/
  2. /* test_node_path.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_NODE_PATH_H
  31. #define TEST_NODE_PATH_H
  32. #include "core/string/node_path.h"
  33. #include "tests/test_macros.h"
  34. namespace TestNodePath {
  35. TEST_CASE("[NodePath] Relative path") {
  36. const NodePath node_path_relative = NodePath("Path2D/PathFollow2D/Sprite2D:position:x");
  37. CHECK_MESSAGE(
  38. node_path_relative.get_as_property_path() == NodePath(":Path2D/PathFollow2D/Sprite2D:position:x"),
  39. "The returned property path should match the expected value.");
  40. CHECK_MESSAGE(
  41. node_path_relative.get_concatenated_subnames() == "position:x",
  42. "The returned concatenated subnames should match the expected value.");
  43. CHECK_MESSAGE(
  44. node_path_relative.get_name(0) == "Path2D",
  45. "The returned name at index 0 should match the expected value.");
  46. CHECK_MESSAGE(
  47. node_path_relative.get_name(1) == "PathFollow2D",
  48. "The returned name at index 1 should match the expected value.");
  49. CHECK_MESSAGE(
  50. node_path_relative.get_name(2) == "Sprite2D",
  51. "The returned name at index 2 should match the expected value.");
  52. ERR_PRINT_OFF;
  53. CHECK_MESSAGE(
  54. node_path_relative.get_name(3) == "",
  55. "The returned name at invalid index 3 should match the expected value.");
  56. CHECK_MESSAGE(
  57. node_path_relative.get_name(-1) == "",
  58. "The returned name at invalid index -1 should match the expected value.");
  59. ERR_PRINT_ON;
  60. CHECK_MESSAGE(
  61. node_path_relative.get_name_count() == 3,
  62. "The returned number of names should match the expected value.");
  63. CHECK_MESSAGE(
  64. node_path_relative.get_subname(0) == "position",
  65. "The returned subname at index 0 should match the expected value.");
  66. CHECK_MESSAGE(
  67. node_path_relative.get_subname(1) == "x",
  68. "The returned subname at index 1 should match the expected value.");
  69. ERR_PRINT_OFF;
  70. CHECK_MESSAGE(
  71. node_path_relative.get_subname(2) == "",
  72. "The returned subname at invalid index 2 should match the expected value.");
  73. CHECK_MESSAGE(
  74. node_path_relative.get_subname(-1) == "",
  75. "The returned subname at invalid index -1 should match the expected value.");
  76. ERR_PRINT_ON;
  77. CHECK_MESSAGE(
  78. node_path_relative.get_subname_count() == 2,
  79. "The returned number of subnames should match the expected value.");
  80. CHECK_MESSAGE(
  81. !node_path_relative.is_absolute(),
  82. "The node path should be considered relative.");
  83. CHECK_MESSAGE(
  84. !node_path_relative.is_empty(),
  85. "The node path shouldn't be considered empty.");
  86. }
  87. TEST_CASE("[NodePath] Absolute path") {
  88. const NodePath node_path_aboslute = NodePath("/root/Sprite2D");
  89. CHECK_MESSAGE(
  90. node_path_aboslute.get_as_property_path() == NodePath(":root/Sprite2D"),
  91. "The returned property path should match the expected value.");
  92. CHECK_MESSAGE(
  93. node_path_aboslute.get_concatenated_subnames() == "",
  94. "The returned concatenated subnames should match the expected value.");
  95. CHECK_MESSAGE(
  96. node_path_aboslute.get_name(0) == "root",
  97. "The returned name at index 0 should match the expected value.");
  98. CHECK_MESSAGE(
  99. node_path_aboslute.get_name(1) == "Sprite2D",
  100. "The returned name at index 1 should match the expected value.");
  101. ERR_PRINT_OFF;
  102. CHECK_MESSAGE(
  103. node_path_aboslute.get_name(2) == "",
  104. "The returned name at invalid index 2 should match the expected value.");
  105. CHECK_MESSAGE(
  106. node_path_aboslute.get_name(-1) == "",
  107. "The returned name at invalid index -1 should match the expected value.");
  108. ERR_PRINT_ON;
  109. CHECK_MESSAGE(
  110. node_path_aboslute.get_name_count() == 2,
  111. "The returned number of names should match the expected value.");
  112. CHECK_MESSAGE(
  113. node_path_aboslute.get_subname_count() == 0,
  114. "The returned number of subnames should match the expected value.");
  115. CHECK_MESSAGE(
  116. node_path_aboslute.is_absolute(),
  117. "The node path should be considered absolute.");
  118. CHECK_MESSAGE(
  119. !node_path_aboslute.is_empty(),
  120. "The node path shouldn't be considered empty.");
  121. }
  122. TEST_CASE("[NodePath] Empty path") {
  123. const NodePath node_path_empty = NodePath();
  124. CHECK_MESSAGE(
  125. node_path_empty.get_as_property_path() == NodePath(),
  126. "The returned property path should match the expected value.");
  127. ERR_PRINT_OFF;
  128. CHECK_MESSAGE(
  129. node_path_empty.get_concatenated_subnames() == "",
  130. "The returned concatenated subnames should match the expected value.");
  131. ERR_PRINT_ON;
  132. CHECK_MESSAGE(
  133. node_path_empty.get_name_count() == 0,
  134. "The returned number of names should match the expected value.");
  135. CHECK_MESSAGE(
  136. node_path_empty.get_subname_count() == 0,
  137. "The returned number of subnames should match the expected value.");
  138. CHECK_MESSAGE(
  139. !node_path_empty.is_absolute(),
  140. "The node path shouldn't be considered absolute.");
  141. CHECK_MESSAGE(
  142. node_path_empty.is_empty(),
  143. "The node path should be considered empty.");
  144. }
  145. } // namespace TestNodePath
  146. #endif // TEST_NODE_PATH_H