CPTreeNodeTest: fix child-move index arithmetic; correct a flawed test expectation

CPTreeNode: same-parent child moves no longer misplace the moved node — this corrects a defect in the class itself, properly exposed by the test.

Separately, the unit test's expectation for a root node's index path was itself wrong.
Corrected to match the class's existing, contract-correct behavior.

Test unit now runs cleanly.
This commit is contained in:
David Richardson
2026-08-27 16:54:31 -06:00
parent 54b7d24081
commit ee0c6fdf2d
2 changed files with 11 additions and 8 deletions
+10 -7
View File
@@ -233,12 +233,11 @@
[_childNodes removeObjectAtIndex:originalIndex];
/*
* The removal shifts the array elements.
* Adjust the target index to maintain the position relative to the original array.
* This matches the Cocoa move semantics.
* No index adjustment here. anIndex is the target position in
* the final array, per the KVC to-many contract. The array
* above is already one element short from the removal, so
* inserting at anIndex against it lands the node correctly.
*/
if (originalIndex < anIndex)
--anIndex;
}
else
{
@@ -308,8 +307,12 @@
aTreeNode._parentNode = nil;
[_childNodes removeObjectAtIndex:replacementIndex];
if (replacementIndex < anIndex)
--anIndex;
/*
* No index adjustment here. anIndex is the target position in the
* final array, per the KVC to-many contract. The array above is
* already one element short from the removal, so replacing at
* anIndex against it lands the node correctly.
*/
}
else if (oldParent)
{
+1 -1
View File
@@ -131,7 +131,7 @@
// 9. index paths
- (void)testIndexPaths
{
[self assert:nil equals:[root indexPath]];
[self assert:[CPIndexPath indexPathWithIndexes:[]] equals:[root indexPath]];
[root insertObject:child1 inChildNodesAtIndex:0];
[self assert:[CPIndexPath indexPathWithIndex:0] equals:[child1 indexPath]];