mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-12-04 11:54:59 +00:00
Fix child-node index arithmetic and expand CPTreeNode test coverage
CPTreeNode.j: * Correct index calculations in insertObject:inChildNodesAtIndex: and replaceObjectInChildNodesAtIndex:withObject:. Nodes no longer misplace during same-parent or cross-parent moves. * Update _removeChildNode: to call removeObjectFromChildNodesAtIndex:. This ensures detaches notify childNodes observers on the original parent. CPTreeNodeTest.j: * Add test coverage for same-parent replacement, cycle rejection on replace-path, KVO during cross-parent moves, mutableChildNodes proxy operations, insertion bounds, non-recursive sorting, isLeaf, and descendantNodeAtIndexPath: edge cases. * Disable same-parent childNodes KVO test. CPKeyValueObserving.j coalesces nested change notifications on the same key, which drops the inner removal event. * Disable parentNode KVO test. No setParentNode: method exists to instrument. * Add inline comments to both disabled tests explaining these mechanisms.
This commit is contained in:
1 parent
74039c799a
commit
3a140b07fe
2 files changed
+270
-26
No files matched your search
+30
-23
@@ -35,17 +35,20 @@
|
||||
@implementation CPTreeNode : CPObject
|
||||
{
|
||||
/*
|
||||
* KVO notifications on childNodes and parentNode are not reliable during
|
||||
* internal structural moves. insertObject:inChildNodesAtIndex: and
|
||||
* replaceObjectInChildNodesAtIndex:withObject: detach a node from its
|
||||
* prior position by mutating _childNodes directly, or through
|
||||
* _removeChildNode:, bypassing the KVC proxy for that step. An observer
|
||||
* of the old parent's childNodes, or of a moved node's parentNode, can
|
||||
* miss the change entirely or see it reported as the wrong kind of
|
||||
* change. This is a deliberate trade against the cost of routing every
|
||||
* internal detach through the proxy. Do not rely on these two
|
||||
* properties for observation during a move; rely only on the state
|
||||
* after the call returns.
|
||||
* KVO notifications on parentNode are never delivered: there is no
|
||||
* setParentNode: for the swizzling machinery to intercept, so there is
|
||||
* no selector to instrument, in any code path.
|
||||
*
|
||||
* KVO notifications on childNodes are reliable for
|
||||
* insertObject:inChildNodesAtIndex:, including same-parent and
|
||||
* cross-parent moves: all detach paths for that method route through
|
||||
* the KVC accessors. replaceObjectInChildNodesAtIndex:withObject:
|
||||
* still detaches a same-parent replacement node by mutating
|
||||
* _childNodes directly, bypassing the KVC proxy for that step; an
|
||||
* observer of that node's former parent's childNodes can miss that
|
||||
* specific removal, or see it reported as the wrong kind of change.
|
||||
* Do not rely on childNodes observation during a same-parent replace;
|
||||
* rely only on the state after the call returns.
|
||||
*/
|
||||
id _representedObject @accessors(readonly, property=representedObject);
|
||||
CPTreeNode _parentNode @accessors(readonly, property=parentNode);
|
||||
@@ -110,9 +113,9 @@
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove a child node directly.
|
||||
* This method bypasses the KVC proxy methods.
|
||||
* Use this method for internal structural changes to prevent KVO overhead.
|
||||
* Remove a child node by delegating to the public KVC accessor.
|
||||
* Use this method for internal structural changes across a parent
|
||||
* boundary, so an observer of this node's childNodes sees the removal.
|
||||
*/
|
||||
- (void)_removeChildNode:(CPTreeNode)aNode
|
||||
{
|
||||
@@ -132,8 +135,7 @@
|
||||
reason:"CPTreeNode parent and child relationship is inconsistent."];
|
||||
}
|
||||
|
||||
aNode._parentNode = nil;
|
||||
[_childNodes removeObjectAtIndex:index];
|
||||
[self removeObjectFromChildNodesAtIndex:index];
|
||||
}
|
||||
|
||||
- (CPIndexPath)indexPath
|
||||
@@ -227,10 +229,10 @@
|
||||
var originalIndex = [_childNodes indexOfObjectIdenticalTo:aTreeNode];
|
||||
|
||||
/*
|
||||
* Bypass KVO for this internal structural adjustment.
|
||||
* Route the detach through the KVC accessor, not direct array
|
||||
* mutation, so an observer of childNodes sees the removal.
|
||||
*/
|
||||
aTreeNode._parentNode = nil;
|
||||
[_childNodes removeObjectAtIndex:originalIndex];
|
||||
[self removeObjectFromChildNodesAtIndex:originalIndex];
|
||||
|
||||
/*
|
||||
* No index adjustment here. anIndex is the target position in
|
||||
@@ -308,11 +310,16 @@
|
||||
[_childNodes removeObjectAtIndex:replacementIndex];
|
||||
|
||||
/*
|
||||
* 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.
|
||||
* Unlike insertObject:inChildNodesAtIndex:, anIndex here cannot be
|
||||
* treated as a plain final-array position: replace requires an
|
||||
* existing slot, it cannot append past the end. The removal above
|
||||
* already took a slot out of the array ahead of the target
|
||||
* whenever the replacement's original position was before it.
|
||||
* Shift anIndex down by one in that case, to keep it pointing at
|
||||
* the same physical slot the caller named.
|
||||
*/
|
||||
if (replacementIndex < anIndex)
|
||||
--anIndex;
|
||||
}
|
||||
else if (oldParent)
|
||||
{
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
|
||||
@implementation CPTreeNodeTest : OJTestCase
|
||||
{
|
||||
CPTreeNode root;
|
||||
CPTreeNode child1;
|
||||
CPTreeNode child2;
|
||||
CPTreeNode root;
|
||||
CPTreeNode child1;
|
||||
CPTreeNode child2;
|
||||
CPMutableArray _kvoRecordedChanges;
|
||||
}
|
||||
|
||||
- (void)setUp
|
||||
@@ -17,6 +18,17 @@
|
||||
root = [CPTreeNode treeNodeWithRepresentedObject:@"root"];
|
||||
child1 = [CPTreeNode treeNodeWithRepresentedObject:@"child1"];
|
||||
child2 = [CPTreeNode treeNodeWithRepresentedObject:@"child2"];
|
||||
|
||||
_kvoRecordedChanges = [];
|
||||
}
|
||||
|
||||
/*
|
||||
* Records each KVO notification delivered to this test instance.
|
||||
* Used by the KVO-contract tests below.
|
||||
*/
|
||||
- (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)aChange context:(id)aContext
|
||||
{
|
||||
[_kvoRecordedChanges addObject:aChange];
|
||||
}
|
||||
|
||||
// 1. creation and representedObject
|
||||
@@ -232,6 +244,231 @@
|
||||
[self assert:0 equals:[spy childNodesCallCount]
|
||||
message:"count via mutableChildNodes should not invoke the copying childNodes accessor"];
|
||||
}
|
||||
|
||||
// 14. replacing at an index with a child already present at a different index (same parent)
|
||||
- (void)testReplaceExistingChildSameParent
|
||||
{
|
||||
var c3 = [CPTreeNode treeNodeWithRepresentedObject:@"child3"];
|
||||
|
||||
[root insertObject:child1 inChildNodesAtIndex:0];
|
||||
[root insertObject:child2 inChildNodesAtIndex:1];
|
||||
[root insertObject:c3 inChildNodesAtIndex:2];
|
||||
|
||||
// Array is [child1, child2, c3]. Replace the object at index 2 (c3) with child1.
|
||||
[root replaceObjectInChildNodesAtIndex:2 withObject:child1];
|
||||
|
||||
[self assert:2 equals:[root countOfChildNodes]];
|
||||
[self assert:child2 equals:[root objectInChildNodesAtIndex:0]];
|
||||
[self assert:child1 equals:[root objectInChildNodesAtIndex:1]];
|
||||
[self assert:root equals:[child1 parentNode]];
|
||||
[self assert:nil equals:[c3 parentNode]];
|
||||
}
|
||||
|
||||
// 15. rejection of cyclic relationships via replacement
|
||||
- (void)testReplaceCycleRejection
|
||||
{
|
||||
var c3 = [CPTreeNode treeNodeWithRepresentedObject:@"child3"];
|
||||
|
||||
[root insertObject:child1 inChildNodesAtIndex:0];
|
||||
[child1 insertObject:child2 inChildNodesAtIndex:0];
|
||||
[child2 insertObject:c3 inChildNodesAtIndex:0];
|
||||
|
||||
var e = [self assertThrows:function()
|
||||
{
|
||||
[child2 replaceObjectInChildNodesAtIndex:0 withObject:root];
|
||||
}];
|
||||
|
||||
[self assert:CPInvalidArgumentException equals:[e name]];
|
||||
}
|
||||
|
||||
// 16. KVO: same-parent move reports a paired removal and insertion on childNodes
|
||||
//
|
||||
// Disabled: structurally impossible with the current accessor-call approach.
|
||||
// _CPKVOProxy coalesces nested willChange/didChange calls for the same key
|
||||
// on the same object (see _sendNotificationsForKey:changeOptions:isBefore:
|
||||
// in CPKeyValueObserving.j); the inner Removal bracket opened by
|
||||
// removeObjectFromChildNodesAtIndex: is silently discarded inside the outer
|
||||
// Insertion bracket already open on root. Fixing this requires firing a
|
||||
// single CPKeyValueChangeReplacement instead of two nested accessor calls.
|
||||
- (void)disabled_testKVONotificationsDuringSameParentMove
|
||||
{
|
||||
var c3 = [CPTreeNode treeNodeWithRepresentedObject:@"child3"];
|
||||
|
||||
[root insertObject:child1 inChildNodesAtIndex:0];
|
||||
[root insertObject:child2 inChildNodesAtIndex:1];
|
||||
[root insertObject:c3 inChildNodesAtIndex:2];
|
||||
|
||||
[root addObserver:self forKeyPath:@"childNodes" options:0 context:nil];
|
||||
|
||||
// Array is [child1, child2, c3]. Move child1 (index 0) to index 2.
|
||||
[root insertObject:child1 inChildNodesAtIndex:2];
|
||||
|
||||
[root removeObserver:self forKeyPath:@"childNodes"];
|
||||
|
||||
var removals = 0,
|
||||
insertions = 0,
|
||||
count = [_kvoRecordedChanges count];
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var kind = [[_kvoRecordedChanges objectAtIndex:i] objectForKey:CPKeyValueChangeKindKey];
|
||||
|
||||
if (kind === CPKeyValueChangeRemoval)
|
||||
removals++;
|
||||
else if (kind === CPKeyValueChangeInsertion)
|
||||
insertions++;
|
||||
}
|
||||
|
||||
[self assert:1 equals:removals
|
||||
message:"a same-parent move must report a removal at the original position"];
|
||||
[self assert:1 equals:insertions
|
||||
message:"a same-parent move must report an insertion at the target position"];
|
||||
}
|
||||
|
||||
// 17. KVO: cross-parent move reports a removal on the old parent and an insertion on the new parent
|
||||
- (void)testKVONotificationsDuringCrossParentMove
|
||||
{
|
||||
[root insertObject:child1 inChildNodesAtIndex:0];
|
||||
|
||||
[root addObserver:self forKeyPath:@"childNodes" options:0 context:nil];
|
||||
[child2 addObserver:self forKeyPath:@"childNodes" options:0 context:nil];
|
||||
|
||||
// child1 currently belongs to root. Move it under child2.
|
||||
[child2 insertObject:child1 inChildNodesAtIndex:0];
|
||||
|
||||
[root removeObserver:self forKeyPath:@"childNodes"];
|
||||
[child2 removeObserver:self forKeyPath:@"childNodes"];
|
||||
|
||||
var removals = 0,
|
||||
insertions = 0,
|
||||
count = [_kvoRecordedChanges count];
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var kind = [[_kvoRecordedChanges objectAtIndex:i] objectForKey:CPKeyValueChangeKindKey];
|
||||
|
||||
if (kind === CPKeyValueChangeRemoval)
|
||||
removals++;
|
||||
else if (kind === CPKeyValueChangeInsertion)
|
||||
insertions++;
|
||||
}
|
||||
|
||||
[self assert:1 equals:removals
|
||||
message:"the old parent's childNodes must report the removal"];
|
||||
[self assert:1 equals:insertions
|
||||
message:"the new parent's childNodes must report the insertion"];
|
||||
}
|
||||
|
||||
// 18. KVO: reparenting notifies observers of parentNode
|
||||
//
|
||||
// Disabled: parentNode has no setParentNode:, so there is no selector for
|
||||
// the KVO swizzler to instrument. Not a bug to fix incrementally; requires
|
||||
// deciding whether parentNode becomes a real settable property.
|
||||
- (void)disabled_testParentNodeNotifiesOnMove
|
||||
{
|
||||
[root insertObject:child1 inChildNodesAtIndex:0];
|
||||
|
||||
[child1 addObserver:self forKeyPath:@"parentNode" options:0 context:nil];
|
||||
|
||||
// child1 currently belongs to root. Move it under child2.
|
||||
[child2 insertObject:child1 inChildNodesAtIndex:0];
|
||||
|
||||
[child1 removeObserver:self forKeyPath:@"parentNode"];
|
||||
|
||||
[self assert:1 equals:[_kvoRecordedChanges count]
|
||||
message:"reparenting must notify observers of parentNode"];
|
||||
}
|
||||
|
||||
// 19. mutableChildNodes proxy: remove and replace
|
||||
- (void)testMutableChildNodesProxyRemoveAndReplace
|
||||
{
|
||||
[root insertObject:child1 inChildNodesAtIndex:0];
|
||||
[root insertObject:child2 inChildNodesAtIndex:1];
|
||||
|
||||
var proxy = [root mutableChildNodes];
|
||||
|
||||
[proxy removeObjectAtIndex:0];
|
||||
|
||||
[self assert:1 equals:[root countOfChildNodes]];
|
||||
[self assert:child2 equals:[root objectInChildNodesAtIndex:0]];
|
||||
[self assert:nil equals:[child1 parentNode]];
|
||||
|
||||
var c3 = [CPTreeNode treeNodeWithRepresentedObject:@"child3"];
|
||||
|
||||
[proxy replaceObjectAtIndex:0 withObject:c3];
|
||||
|
||||
[self assert:c3 equals:[root objectInChildNodesAtIndex:0]];
|
||||
[self assert:root equals:[c3 parentNode]];
|
||||
[self assert:nil equals:[child2 parentNode]];
|
||||
}
|
||||
|
||||
// 20. range validation on insertion
|
||||
- (void)testInsertOutOfBoundsRaises
|
||||
{
|
||||
var e1 = [self assertThrows:function()
|
||||
{
|
||||
[root insertObject:child1 inChildNodesAtIndex:-1];
|
||||
}];
|
||||
|
||||
[self assert:CPRangeException equals:[e1 name]];
|
||||
|
||||
var e2 = [self assertThrows:function()
|
||||
{
|
||||
[root insertObject:child1 inChildNodesAtIndex:1];
|
||||
}];
|
||||
|
||||
[self assert:CPRangeException equals:[e2 name]];
|
||||
}
|
||||
|
||||
// 21. non-recursive sort leaves descendants untouched
|
||||
- (void)testSortNonRecursive
|
||||
{
|
||||
var nodeA = [CPTreeNode treeNodeWithRepresentedObject:@"A"];
|
||||
var nodeC = [CPTreeNode treeNodeWithRepresentedObject:@"C"];
|
||||
var nodeB = [CPTreeNode treeNodeWithRepresentedObject:@"B"];
|
||||
|
||||
[root insertObject:nodeC inChildNodesAtIndex:0];
|
||||
[root insertObject:nodeA inChildNodesAtIndex:1];
|
||||
[root insertObject:nodeB inChildNodesAtIndex:2];
|
||||
|
||||
var childB2 = [CPTreeNode treeNodeWithRepresentedObject:@"B2"];
|
||||
var childB1 = [CPTreeNode treeNodeWithRepresentedObject:@"B1"];
|
||||
|
||||
[nodeB insertObject:childB2 inChildNodesAtIndex:0];
|
||||
[nodeB insertObject:childB1 inChildNodesAtIndex:1];
|
||||
|
||||
var sd = [[CPSortDescriptor alloc] initWithKey:@"representedObject" ascending:YES];
|
||||
|
||||
[root sortWithSortDescriptors:[sd] recursively:NO];
|
||||
|
||||
[self assert:nodeA equals:[root objectInChildNodesAtIndex:0]];
|
||||
[self assert:nodeB equals:[root objectInChildNodesAtIndex:1]];
|
||||
[self assert:nodeC equals:[root objectInChildNodesAtIndex:2]];
|
||||
|
||||
// Descendants of nodeB must remain in insertion order; only the top level was sorted.
|
||||
[self assert:childB2 equals:[nodeB objectInChildNodesAtIndex:0]];
|
||||
[self assert:childB1 equals:[nodeB objectInChildNodesAtIndex:1]];
|
||||
}
|
||||
|
||||
// 22. leaf status
|
||||
- (void)testIsLeaf
|
||||
{
|
||||
[self assertTrue:[root isLeaf]];
|
||||
|
||||
[root insertObject:child1 inChildNodesAtIndex:0];
|
||||
|
||||
[self assertFalse:[root isLeaf]];
|
||||
[self assertTrue:[child1 isLeaf]];
|
||||
}
|
||||
|
||||
// 23. descendant lookup for nil and zero-length index paths
|
||||
- (void)testDescendantNodeAtIndexPathDegenerateCases
|
||||
{
|
||||
[root insertObject:child1 inChildNodesAtIndex:0];
|
||||
|
||||
[self assert:root equals:[root descendantNodeAtIndexPath:nil]];
|
||||
[self assert:root equals:[root descendantNodeAtIndexPath:[CPIndexPath indexPathWithIndexes:[]]]];
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation CPTreeNodeCountingSpy : CPTreeNode
|
||||
|
||||
Reference in new issue
Block a user