Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NodePath::slice() incorrect behavior for subname indexing #100954

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/string/node_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringName> names = get_names().slice(begin, end);
Expand Down
17 changes: 10 additions & 7 deletions tests/core/string/test_node_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down