From df66ea74d7ab575b03ce48c358e9cdf89370dd31 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Mon, 30 Dec 2024 21:22:26 +0100 Subject: [PATCH] Fix `NodePath::slice()` incorrect behavior for subname indexing Adjust slice boundaries in `NodePath` logic to correctly handle subnames. Update test cases to reflect these changes. --- core/string/node_path.cpp | 2 +- tests/core/string/test_node_path.h | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/core/string/node_path.cpp b/core/string/node_path.cpp index b32bed5ee45d..3ab8eb860de1 100644 --- a/core/string/node_path.cpp +++ b/core/string/node_path.cpp @@ -255,7 +255,7 @@ NodePath NodePath::slice(int p_begin, int p_end) const { if (end < 0) { end += total_count; } - const int sub_begin = MAX(begin - name_count - 1, 0); + const int sub_begin = MAX(begin - name_count, 0); const int sub_end = MAX(end - name_count, 0); const Vector names = get_names().slice(begin, end); diff --git a/tests/core/string/test_node_path.h b/tests/core/string/test_node_path.h index bdbc578e8507..3e7d4de40010 100644 --- a/tests/core/string/test_node_path.h +++ b/tests/core/string/test_node_path.h @@ -169,28 +169,31 @@ TEST_CASE("[NodePath] Empty path") { } TEST_CASE("[NodePath] Slice") { - const NodePath node_path_relative = NodePath("Parent/Child:prop"); + const NodePath node_path_relative = NodePath("Parent/Child:prop:subprop"); const NodePath node_path_absolute = NodePath("/root/Parent/Child:prop"); CHECK_MESSAGE( node_path_relative.slice(0, 2) == NodePath("Parent/Child"), "The slice lower bound should be inclusive and the slice upper bound should be exclusive."); CHECK_MESSAGE( - node_path_relative.slice(3) == NodePath(":prop"), - "Slicing on the length of the path should return the last entry."); + node_path_relative.slice(3) == NodePath(":subprop"), + "Slicing on the last index (length - 1) should return the last entry."); + CHECK_MESSAGE( + node_path_relative.slice(1) == NodePath("Child:prop:subprop"), + "Slicing without upper bound should return remaining entries after index."); CHECK_MESSAGE( node_path_relative.slice(1, 3) == NodePath("Child:prop"), "Slicing should include names and subnames."); CHECK_MESSAGE( - node_path_relative.slice(-1) == NodePath(":prop"), + node_path_relative.slice(-1) == NodePath(":subprop"), "Slicing on -1 should return the last entry."); CHECK_MESSAGE( - node_path_relative.slice(0, -1) == NodePath("Parent/Child"), + node_path_relative.slice(0, -1) == NodePath("Parent/Child:prop"), "Slicing up to -1 should include the second-to-last entry."); CHECK_MESSAGE( - node_path_relative.slice(-2, -1) == NodePath("Child"), + node_path_relative.slice(-2, -1) == NodePath(":prop"), "Slicing from negative to negative should treat lower bound as inclusive and upper bound as exclusive."); CHECK_MESSAGE( - node_path_relative.slice(0, 10) == NodePath("Parent/Child:prop"), + node_path_relative.slice(0, 10) == NodePath("Parent/Child:prop:subprop"), "Slicing past the length of the path should work like slicing up to the last entry."); CHECK_MESSAGE( node_path_relative.slice(-10, 2) == NodePath("Parent/Child"),