Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

more fixes and tests for _CPKVCArray

  • Loading branch information...
commit e99e2660e9946c67a233bb45d6a0d4055cd4fb8c 1 parent 5b00f7f
@klaaspieter klaaspieter authored
Showing with 112 additions and 10 deletions.
  1. +27 −7 Foundation/CPArray+KVO.j
  2. +85 −3 Tests/Foundation/CPKVCArrayTest.j
View
34 Foundation/CPArray+KVO.j
@@ -303,6 +303,32 @@
[self removeObject:anObject inRange:CPMakeRange(0, [self count])];
}
+- (void)removeObjectsInArray:(CPArray)theObjects
+{
+ if (_removeMany)
+ {
+ var indexes = [CPIndexSet indexSet],
+ index = [theObjects count];
+
+ while (index--)
+ [indexes addIndex:[self indexOfObject:[theObjects objectAtIndex:index]]];
+
+ _removeMany(_proxyObject, _removeManySEL, indexes);
+ }
+ else if (_remove)
+ {
+ var index = [theObjects count];
+ while (index--)
+ _remove(_proxyObject, _removeSEL, [self indexOfObject:[theObjects objectAtIndex:index]]);
+ }
+ else
+ {
+ var target = [[self _representedObject] copy];
+ [target removeObjectsInArray:theObjects];
+ [self _setRepresentedObject:target];
+ }
+}
+
- (void)removeObject:(id)theObject inRange:(CPRange)theRange
{
if (_remove)
@@ -326,13 +352,7 @@
- (void)removeLastObject
{
- if (_remove)
- return _remove(_proxyObject, _removeSEL, [self count] - 1);
-
- var target = [[self _representedObject] copy];
-
- [target removeLastObject];
- [self _setRepresentedObject:target];
+ [self removeObjectsAtIndexes:[CPIndexSet indexSetWithIndex:[self count] - 1]];
}
- (void)removeObjectAtIndex:(unsigned)anIndex
View
88 Tests/Foundation/CPKVCArrayTest.j
@@ -16,6 +16,29 @@ var COUNTER;
- (void)_patchSelector:(SEL)theSelector
{
+ var method_dtable = [[self object] class].method_dtable,
+ method_list = [[self object] class].method_list,
+ selectorsToRemove = [@selector(countOfValues), @selector(objectInValuesAtIndex),
+ @selector(objectInValuesAtIndex:), @selector(valuesAtIndexes:),
+ @selector(insertObject:inValuesAtIndex:), @selector(insertValues:atIndexes:),
+ @selector(removeObjectFromValuesAtIndex:), @selector(removeValuesAtIndexes:),
+ @selector(removeObjectFromValuesAtIndex:),
+ @selector(replaceObjectInValuesAtIndex:withObject:),
+ @selector(replaceValuesAtIndexes:withValues:)],
+ selectorIndex = [selectorsToRemove count];
+
+ while (selectorIndex--)
+ {
+ var selector = [selectorsToRemove objectAtIndex:selectorIndex],
+ implementation = method_dtable[selector];
+
+ if (!implementation)
+ continue;
+
+ delete method_dtable[selector]
+ [method_list removeObject:implementation];
+ }
+
var method = class_getInstanceMethod([[self implementedObject] class], theSelector),
implementation = method_getImplementation(method);
@@ -122,7 +145,7 @@ var COUNTER;
message:@"insertValues:atIndexes: should have been called once for each object"];
}
-- (void)testRemoveObjectAtIndexUsesRemoveKeyAtIndex
+- (void)testRemoveObjectAtIndexUsesRemoveObjectFromKeyAtIndex
{
[self _patchSelector:@selector(removeObjectFromValuesAtIndex:)];
@@ -132,7 +155,7 @@ var COUNTER;
[self assert:1 equals:COUNTER message:@"removeObjectFromValuesAtIndex: should have been called once"];
}
-- (void)testRemoveObjectAtIndexesUsesRemoveKeyAtIndex
+- (void)testRemoveObjectAtIndexesUsesRemoveObjectFromKeyAtIndex
{
[self _patchSelector:@selector(removeObjectFromValuesAtIndex:)];
@@ -142,7 +165,6 @@ var COUNTER;
[self assert:[2, 3, 4, 5, 6, 7, 8, 9] equals:[[self object] values]];
[self assert:2 equals:COUNTER message:@"removeValuesAtIndex: should have been called once for each object"];
}
-
- (void)testRemoveObjectsAtIndexesUsesRemoveKeyAtIndex
{
[self _patchSelector:@selector(removeValuesAtIndexes:)];
@@ -166,6 +188,66 @@ var COUNTER;
[self assert:1 equals:COUNTER message:@"removeValuesAtIndexes: should have been called once"];
}
+- (void)testRemoveObjectUsesRemoveKeyAtIndex
+{
+ [self _patchSelector:@selector(removeObjectFromValuesAtIndex:)];
+
+ [[[self object] mutableArrayValueForKey:@"values"] removeObject:1];
+
+ [self assert:[0, 2, 3, 4, 5, 6, 7, 8, 9] equals:[[self object] values]];
+ [self assert:1 equals:COUNTER message:"removeObjectFromValuesAtIndex: should have been called once"]
+}
+
+- (void)testeRemoveObjectUsesRemoveKeyAtIndexes
+{
+ [self _patchSelector:@selector(removeValuesAtIndexes:)];
+
+ [[[self object] mutableArrayValueForKey:@"values"] removeObject:5];
+
+ [self assert:[0, 1, 2, 3, 4, 6, 7, 8, 9] equals:[[self object] values]];
+ [self assert:1 equals:COUNTER message:@"removeValuesAtIndexes: should have been called once"];
+}
+
+- (void)testRemoveObjectsInArrayUsesRemoveKeyAtIndex
+{
+ [self _patchSelector:@selector(removeObjectFromValuesAtIndex:)];
+
+ [[[self object] mutableArrayValueForKey:@"values"] removeObjectsInArray:[3, 6]];
+
+ [self assert:[0, 1, 2, 4, 5, 7, 8, 9] equals:[[self object] values]];
+ [self assert:2 equals:COUNTER message:"removeObjectFromValuesAtIndex: should have been called once"]
+}
+
+- (void)testRemoveObjectsInArrayUsesRemoveKeyAtIndexes
+{
+ [self _patchSelector:@selector(removeValuesAtIndexes:)];
+
+ [[[self object] mutableArrayValueForKey:@"values"] removeObjectsInArray:[0, 9]];
+
+ [self assert:[1, 2, 3, 4, 5, 6, 7, 8] equals:[[self object] values]];
+ [self assert:1 equals:COUNTER message:@"removeValuesAtIndexes: should have been called once"];
+}
+
+- (void)testRemoveLastObjectUsesRemoveKeyAtIndex
+{
+ [self _patchSelector:@selector(removeObjectFromValuesAtIndex:)];
+
+ [[[self object] mutableArrayValueForKey:@"values"] removeLastObject];
+
+ [self assert:[0, 1, 2, 3, 4, 5, 6, 7, 8] equals:[[self object] values]];
+ [self assert:1 equals:COUNTER message:"removeObjectFromValuesAtIndex: should have been called once"]
+}
+
+- (void)testRemoveLastObjectUsesRemoveKeyAtIndexes
+{
+ [self _patchSelector:@selector(removeValuesAtIndexes:)];
+
+ [[[self object] mutableArrayValueForKey:@"values"] removeLastObject];
+
+ [self assert:[0, 1, 2, 3, 4, 5, 6, 7, 8] equals:[[self object] values]];
+ [self assert:1 equals:COUNTER message:@"removeValuesAtIndexes: should have been called once"];
+}
+
- (void)testReplaceObjectAtIndexWithObjectUsesReplaceObjectInKeyAtIndexWithObject
{
[self _patchSelector:@selector(replaceObjectInValuesAtIndex:withObject:)];
Please sign in to comment.
Something went wrong with that request. Please try again.