From 4fbd0fdc42e96cf7547bba6bcb643efe63158fc9 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Sun, 19 Aug 2012 16:50:09 +0100 Subject: [PATCH] Unit test `KVCArray setArray:` behaviour. --- Tests/Foundation/CPKVCArrayTest.j | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/Tests/Foundation/CPKVCArrayTest.j b/Tests/Foundation/CPKVCArrayTest.j index 6b893d9bc..448e41175 100644 --- a/Tests/Foundation/CPKVCArrayTest.j +++ b/Tests/Foundation/CPKVCArrayTest.j @@ -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];