Unit test KVCArray setArray: behaviour.

This commit is contained in:
Alexander Ljungberg
2012-08-19 16:50:09 +01:00
parent 3dffbd6071
commit 4fbd0fdc42
+24 -6
View File
@@ -15,7 +15,7 @@ var COUNTER;
COUNTER = 0;
}
- (void)_patchSelector:(SEL)theSelector
- (void)_patchSelector:(SEL)selectors, ...
{
var method_dtable = [[self object] class].method_dtable,
method_list = [[self object] class].method_list,
@@ -40,10 +40,14 @@ var COUNTER;
[method_list removeObject:implementation];
}
var method = class_getInstanceMethod([[self implementedObject] class], theSelector),
implementation = method_getImplementation(method);
for (var i = 2; i < arguments.length; i++)
{
var theSelector = arguments[i],
method = class_getInstanceMethod([[self implementedObject] class], theSelector),
implementation = method_getImplementation(method);
class_addMethod([[self object] class], theSelector, implementation);
class_addMethod([[self object] class], theSelector, implementation);
}
}
- (void)testUsesCountOfKey
@@ -299,6 +303,20 @@ var COUNTER;
message:@"replaceObjectInValuesAtIndexes:withValues: should have been called once"];
}
- (void)testSetArray
{
[self _patchSelector:@selector(removeObjectFromValuesAtIndex:), @selector(insertObject:inValuesAtIndex:)];
// This should result in all 10 objects being removed from the values and the 2 new ones being inserted
// in a KVO compliant manner.
[[[self object] mutableArrayValueForKey:@"values"] setArray:[88, 99]];
[self assert:[88, 99] equals:[[self object] values]];
[self assert:12
equals:COUNTER
message:@"removeObjectFromValuesAtIndex: should have been called 10 times and insertObject:inValuesAtIndex: 2 times"];
}
- (void)testKVCArrayOperators
{
var one = [1, 1, 1, 1, 1, 1, 1, 1],
@@ -382,7 +400,7 @@ var COUNTER;
- (void)insertObject:(id)theObject inValuesAtIndex:(int)theIndex
{
// CPLog.warn(@"insertObject: %@ inValuesAtIndex: %@", theObject, theIndex);
CPLog.warn(@"insertObject: %@ inValuesAtIndex: %@", theObject, theIndex);
COUNTER += 1;
[[self values] insertObject:theObject atIndex:theIndex];
@@ -398,7 +416,7 @@ var COUNTER;
- (void)removeObjectFromValuesAtIndex:(int)theIndex
{
// CPLog.warn(@"removeObjectFromValuesAtIndex: %@", theIndex);
CPLog.warn(@"removeObjectFromValuesAtIndex: %@", theIndex);
COUNTER += 1;
[[self values] removeObjectAtIndex:theIndex];