test_node_path.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_absolute = NodePath("/root/Sprite2D");
  89. CHECK_MESSAGE(
  90. node_path_absolute.get_as_property_path() == NodePath(":root/Sprite2D"),
  91. "The returned property path should match the expected value.");
  92. CHECK_MESSAGE(
  93. node_path_absolute.get_concatenated_subnames() == "",
  94. "The returned concatenated subnames should match the expected value.");
  95. CHECK_MESSAGE(
  96. node_path_absolute.get_name(0) == "root",
  97. "The returned name at index 0 should match the expected value.");
  98. CHECK_MESSAGE(
  99. node_path_absolute.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_absolute.get_name(2) == "",
  104. "The returned name at invalid index 2 should match the expected value.");
  105. CHECK_MESSAGE(
  106. node_path_absolute.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_absolute.get_name_count() == 2,
  111. "The returned number of names should match the expected value.");
  112. CHECK_MESSAGE(
  113. node_path_absolute.get_subname_count() == 0,
  114. "The returned number of subnames should match the expected value.");
  115. CHECK_MESSAGE(
  116. node_path_absolute.is_absolute(),
  117. "The node path should be considered absolute.");
  118. CHECK_MESSAGE(
  119. !node_path_absolute.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. TEST_CASE("[NodePath] Slice") {
  146. const NodePath node_path_relative = NodePath("Parent/Child:prop");
  147. const NodePath node_path_absolute = NodePath("/root/Parent/Child:prop");
  148. CHECK_MESSAGE(
  149. node_path_relative.slice(0, 2) == NodePath("Parent/Child"),
  150. "The slice lower bound should be inclusive and the slice upper bound should be exclusive.");
  151. CHECK_MESSAGE(
  152. node_path_relative.slice(3) == NodePath(":prop"),
  153. "Slicing on the length of the path should return the last entry.");
  154. CHECK_MESSAGE(
  155. node_path_relative.slice(1, 3) == NodePath("Child:prop"),
  156. "Slicing should include names and subnames.");
  157. CHECK_MESSAGE(
  158. node_path_relative.slice(-1) == NodePath(":prop"),
  159. "Slicing on -1 should return the last entry.");
  160. CHECK_MESSAGE(
  161. node_path_relative.slice(0, -1) == NodePath("Parent/Child"),
  162. "Slicing up to -1 should include the second-to-last entry.");
  163. CHECK_MESSAGE(
  164. node_path_relative.slice(-2, -1) == NodePath("Child"),
  165. "Slicing from negative to negative should treat lower bound as inclusive and upper bound as exclusive.");
  166. CHECK_MESSAGE(
  167. node_path_relative.slice(0, 10) == NodePath("Parent/Child:prop"),
  168. "Slicing past the length of the path should work like slicing up to the last entry.");
  169. CHECK_MESSAGE(
  170. node_path_relative.slice(-10, 2) == NodePath("Parent/Child"),
  171. "Slicing negatively past the length of the path should work like slicing from the first entry.");
  172. CHECK_MESSAGE(
  173. node_path_relative.slice(1, 1) == NodePath(""),
  174. "Slicing with a lower bound equal to upper bound should return empty path.");
  175. CHECK_MESSAGE(
  176. node_path_absolute.slice(0, 2) == NodePath("/root/Parent"),
  177. "Slice from beginning of an absolute path should be an absolute path.");
  178. CHECK_MESSAGE(
  179. node_path_absolute.slice(1, 4) == NodePath("Parent/Child:prop"),
  180. "Slice of an absolute path that does not start at the beginning should be a relative path.");
  181. CHECK_MESSAGE(
  182. node_path_absolute.slice(3, 4) == NodePath(":prop"),
  183. "Slice of an absolute path that does not start at the beginning should be a relative path.");
  184. CHECK_MESSAGE(
  185. NodePath("").slice(0, 1) == NodePath(""),
  186. "Slice of an empty path should be an empty path.");
  187. CHECK_MESSAGE(
  188. NodePath("").slice(-1, 2) == NodePath(""),
  189. "Slice of an empty path should be an empty path.");
  190. CHECK_MESSAGE(
  191. NodePath("/").slice(-1, 2) == NodePath("/"),
  192. "Slice of an empty absolute path should be an empty absolute path.");
  193. }
  194. } // namespace TestNodePath
  195. #endif // TEST_NODE_PATH_H