test_node_path.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #pragma once
  31. #include "core/string/node_path.h"
  32. #include "tests/test_macros.h"
  33. namespace TestNodePath {
  34. TEST_CASE("[NodePath] Relative path") {
  35. const NodePath node_path_relative = NodePath("Path2D/PathFollow2D/Sprite2D:position:x");
  36. CHECK_MESSAGE(
  37. node_path_relative.get_as_property_path() == NodePath(":Path2D/PathFollow2D/Sprite2D:position:x"),
  38. "The returned property path should match the expected value.");
  39. CHECK_MESSAGE(
  40. node_path_relative.get_concatenated_subnames() == "position:x",
  41. "The returned concatenated subnames should match the expected value.");
  42. CHECK_MESSAGE(
  43. node_path_relative.get_name(0) == "Path2D",
  44. "The returned name at index 0 should match the expected value.");
  45. CHECK_MESSAGE(
  46. node_path_relative.get_name(1) == "PathFollow2D",
  47. "The returned name at index 1 should match the expected value.");
  48. CHECK_MESSAGE(
  49. node_path_relative.get_name(2) == "Sprite2D",
  50. "The returned name at index 2 should match the expected value.");
  51. ERR_PRINT_OFF;
  52. CHECK_MESSAGE(
  53. node_path_relative.get_name(3) == "",
  54. "The returned name at invalid index 3 should match the expected value.");
  55. CHECK_MESSAGE(
  56. node_path_relative.get_name(-1) == "",
  57. "The returned name at invalid index -1 should match the expected value.");
  58. ERR_PRINT_ON;
  59. CHECK_MESSAGE(
  60. node_path_relative.get_name_count() == 3,
  61. "The returned number of names should match the expected value.");
  62. CHECK_MESSAGE(
  63. node_path_relative.get_subname(0) == "position",
  64. "The returned subname at index 0 should match the expected value.");
  65. CHECK_MESSAGE(
  66. node_path_relative.get_subname(1) == "x",
  67. "The returned subname at index 1 should match the expected value.");
  68. ERR_PRINT_OFF;
  69. CHECK_MESSAGE(
  70. node_path_relative.get_subname(2) == "",
  71. "The returned subname at invalid index 2 should match the expected value.");
  72. CHECK_MESSAGE(
  73. node_path_relative.get_subname(-1) == "",
  74. "The returned subname at invalid index -1 should match the expected value.");
  75. ERR_PRINT_ON;
  76. CHECK_MESSAGE(
  77. node_path_relative.get_subname_count() == 2,
  78. "The returned number of subnames should match the expected value.");
  79. CHECK_MESSAGE(
  80. !node_path_relative.is_absolute(),
  81. "The node path should be considered relative.");
  82. CHECK_MESSAGE(
  83. !node_path_relative.is_empty(),
  84. "The node path shouldn't be considered empty.");
  85. }
  86. TEST_CASE("[NodePath] Absolute path") {
  87. const NodePath node_path_absolute = NodePath("/root/Sprite2D");
  88. CHECK_MESSAGE(
  89. node_path_absolute.get_as_property_path() == NodePath(":root/Sprite2D"),
  90. "The returned property path should match the expected value.");
  91. CHECK_MESSAGE(
  92. node_path_absolute.get_concatenated_subnames() == "",
  93. "The returned concatenated subnames should match the expected value.");
  94. CHECK_MESSAGE(
  95. node_path_absolute.get_name(0) == "root",
  96. "The returned name at index 0 should match the expected value.");
  97. CHECK_MESSAGE(
  98. node_path_absolute.get_name(1) == "Sprite2D",
  99. "The returned name at index 1 should match the expected value.");
  100. ERR_PRINT_OFF;
  101. CHECK_MESSAGE(
  102. node_path_absolute.get_name(2) == "",
  103. "The returned name at invalid index 2 should match the expected value.");
  104. CHECK_MESSAGE(
  105. node_path_absolute.get_name(-1) == "",
  106. "The returned name at invalid index -1 should match the expected value.");
  107. ERR_PRINT_ON;
  108. CHECK_MESSAGE(
  109. node_path_absolute.get_name_count() == 2,
  110. "The returned number of names should match the expected value.");
  111. CHECK_MESSAGE(
  112. node_path_absolute.get_subname_count() == 0,
  113. "The returned number of subnames should match the expected value.");
  114. CHECK_MESSAGE(
  115. node_path_absolute.is_absolute(),
  116. "The node path should be considered absolute.");
  117. CHECK_MESSAGE(
  118. !node_path_absolute.is_empty(),
  119. "The node path shouldn't be considered empty.");
  120. }
  121. TEST_CASE("[NodePath] Empty path") {
  122. const NodePath node_path_empty = NodePath();
  123. CHECK_MESSAGE(
  124. node_path_empty.get_as_property_path() == NodePath(),
  125. "The returned property path should match the expected value.");
  126. ERR_PRINT_OFF;
  127. CHECK_MESSAGE(
  128. node_path_empty.get_concatenated_subnames() == "",
  129. "The returned concatenated subnames should match the expected value.");
  130. ERR_PRINT_ON;
  131. CHECK_MESSAGE(
  132. node_path_empty.get_name_count() == 0,
  133. "The returned number of names should match the expected value.");
  134. CHECK_MESSAGE(
  135. node_path_empty.get_subname_count() == 0,
  136. "The returned number of subnames should match the expected value.");
  137. CHECK_MESSAGE(
  138. !node_path_empty.is_absolute(),
  139. "The node path shouldn't be considered absolute.");
  140. CHECK_MESSAGE(
  141. node_path_empty.is_empty(),
  142. "The node path should be considered empty.");
  143. }
  144. TEST_CASE("[NodePath] Slice") {
  145. const NodePath node_path_relative = NodePath("Parent/Child:prop:subprop");
  146. const NodePath node_path_absolute = NodePath("/root/Parent/Child:prop");
  147. CHECK_MESSAGE(
  148. node_path_relative.slice(0, 2) == NodePath("Parent/Child"),
  149. "The slice lower bound should be inclusive and the slice upper bound should be exclusive.");
  150. CHECK_MESSAGE(
  151. node_path_relative.slice(3) == NodePath(":subprop"),
  152. "Slicing on the last index (length - 1) should return the last entry.");
  153. CHECK_MESSAGE(
  154. node_path_relative.slice(1) == NodePath("Child:prop:subprop"),
  155. "Slicing without upper bound should return remaining entries after index.");
  156. CHECK_MESSAGE(
  157. node_path_relative.slice(1, 3) == NodePath("Child:prop"),
  158. "Slicing should include names and subnames.");
  159. CHECK_MESSAGE(
  160. node_path_relative.slice(-1) == NodePath(":subprop"),
  161. "Slicing on -1 should return the last entry.");
  162. CHECK_MESSAGE(
  163. node_path_relative.slice(0, -1) == NodePath("Parent/Child:prop"),
  164. "Slicing up to -1 should include the second-to-last entry.");
  165. CHECK_MESSAGE(
  166. node_path_relative.slice(-2, -1) == NodePath(":prop"),
  167. "Slicing from negative to negative should treat lower bound as inclusive and upper bound as exclusive.");
  168. CHECK_MESSAGE(
  169. node_path_relative.slice(0, 10) == NodePath("Parent/Child:prop:subprop"),
  170. "Slicing past the length of the path should work like slicing up to the last entry.");
  171. CHECK_MESSAGE(
  172. node_path_relative.slice(-10, 2) == NodePath("Parent/Child"),
  173. "Slicing negatively past the length of the path should work like slicing from the first entry.");
  174. CHECK_MESSAGE(
  175. node_path_relative.slice(1, 1) == NodePath(""),
  176. "Slicing with a lower bound equal to upper bound should return empty path.");
  177. CHECK_MESSAGE(
  178. node_path_absolute.slice(0, 2) == NodePath("/root/Parent"),
  179. "Slice from beginning of an absolute path should be an absolute path.");
  180. CHECK_MESSAGE(
  181. node_path_absolute.slice(1, 4) == NodePath("Parent/Child:prop"),
  182. "Slice of an absolute path that does not start at the beginning should be a relative path.");
  183. CHECK_MESSAGE(
  184. node_path_absolute.slice(3, 4) == NodePath(":prop"),
  185. "Slice of an absolute path that does not start at the beginning should be a relative path.");
  186. CHECK_MESSAGE(
  187. NodePath("").slice(0, 1) == NodePath(""),
  188. "Slice of an empty path should be an empty path.");
  189. CHECK_MESSAGE(
  190. NodePath("").slice(-1, 2) == NodePath(""),
  191. "Slice of an empty path should be an empty path.");
  192. CHECK_MESSAGE(
  193. NodePath("/").slice(-1, 2) == NodePath("/"),
  194. "Slice of an empty absolute path should be an empty absolute path.");
  195. }
  196. } // namespace TestNodePath