From ee0c6fdf2d333a4b555336fbd3bb606ed25efc0f Mon Sep 17 00:00:00 2001 From: David Richardson Date: Thu, 27 Aug 2026 16:54:31 -0600 Subject: [PATCH] CPTreeNodeTest: fix child-move index arithmetic; correct a flawed test expectation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- AppKit/CPTreeNode.j | 17 ++++++++++------- Tests/AppKit/CPTreeNodeTest.j | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/AppKit/CPTreeNode.j b/AppKit/CPTreeNode.j index b6d31066c..0a89f4430 100644 --- a/AppKit/CPTreeNode.j +++ b/AppKit/CPTreeNode.j @@ -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) { diff --git a/Tests/AppKit/CPTreeNodeTest.j b/Tests/AppKit/CPTreeNodeTest.j index 0895e6591..6a7128678 100644 --- a/Tests/AppKit/CPTreeNodeTest.j +++ b/Tests/AppKit/CPTreeNodeTest.j @@ -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]];