From 10df31cbf977e73e47326f2388eab2f80d86ed95 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Tue, 22 Jun 2010 07:47:30 +0200 Subject: [PATCH 1/7] made isEqual: work on CPIndexSet and updated unit tests --- Foundation/CPIndexSet.j | 11 +++++++++++ Tests/Foundation/CPIndexSetTest.j | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/Foundation/CPIndexSet.j b/Foundation/CPIndexSet.j index 266c7273e..9975c0e11 100644 --- a/Foundation/CPIndexSet.j +++ b/Foundation/CPIndexSet.j @@ -130,6 +130,17 @@ return self; } +- (BOOL)isEqual:(id)anObject +{ + if (self === anObject) + return YES; + + if (![anObject isKindOfClass:[CPIndexSet class]]) + return NO; + + return [self isEqualToIndexSet:anObject]; +} + // Querying an Index Set /*! Compares the receiver with the provided index set. diff --git a/Tests/Foundation/CPIndexSetTest.j b/Tests/Foundation/CPIndexSetTest.j index c38778903..b63ff8cf9 100644 --- a/Tests/Foundation/CPIndexSetTest.j +++ b/Tests/Foundation/CPIndexSetTest.j @@ -368,6 +368,26 @@ function descriptionWithoutEntity(aString) [self assertTrue:[_set containsIndexes:[CPIndexSet indexSetWithIndexesInRange:startRange]]]; } +- (void)testIsEqual +{ + var differentSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 11)], + equalSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 10)]; + + [self assertFalse:[_set isEqual:differentSet]]; + [self assertTrue:[_set isEqual:equalSet]]; + [self assertTrue:[_set isEqual:_set]]; +} + +- (void)testIsEqualToIndexSet +{ + var differentSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 11)], + equalSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 10)]; + + [self assertFalse:[_set isEqualToIndexSet:differentSet]]; + [self assertTrue:[_set isEqualToIndexSet:equalSet]]; + [self assertTrue:[_set isEqualToIndexSet:_set]]; +} + - (void)tearDown { _set = nil; From 6b132c4d3b7eb44daae513001abf27a09fc7fc61 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Wed, 30 Jun 2010 11:15:38 +0200 Subject: [PATCH 2/7] added check for nil in CPIndexSet isEqual --- Foundation/CPIndexSet.j | 2 +- Tests/Foundation/CPIndexSetTest.j | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Foundation/CPIndexSet.j b/Foundation/CPIndexSet.j index 9975c0e11..af528b169 100644 --- a/Foundation/CPIndexSet.j +++ b/Foundation/CPIndexSet.j @@ -135,7 +135,7 @@ if (self === anObject) return YES; - if (![anObject isKindOfClass:[CPIndexSet class]]) + if (!anObject || ![anObject isKindOfClass:[CPIndexSet class]]) return NO; return [self isEqualToIndexSet:anObject]; diff --git a/Tests/Foundation/CPIndexSetTest.j b/Tests/Foundation/CPIndexSetTest.j index b63ff8cf9..49e3194df 100644 --- a/Tests/Foundation/CPIndexSetTest.j +++ b/Tests/Foundation/CPIndexSetTest.j @@ -373,6 +373,7 @@ function descriptionWithoutEntity(aString) var differentSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 11)], equalSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 10)]; + [self assertFalse:[_set isEqual:nil]]; [self assertFalse:[_set isEqual:differentSet]]; [self assertTrue:[_set isEqual:equalSet]]; [self assertTrue:[_set isEqual:_set]]; From 138888e6201d4efee04e55b159dac6122c341895 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Mon, 26 Jul 2010 14:04:05 +0200 Subject: [PATCH 3/7] implement CPArrayController insertObject:atArrangedObjectIndex: --- AppKit/CPArrayController.j | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index c11206f79..fed04b4b1 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -417,6 +417,33 @@ [self rearrangeObjects]; } +- (void)insertObject:(id)anObject atArrangedObjectIndex:(int)anIndex +{ + if (![self canAdd]) + return; + + [self willChangeValueForKey:@"content"]; + [_contentObject insertObject:anObject atIndex:anIndex]; + [self didChangeValueForKey:@"content"]; + + if (_clearsFilterPredicateOnInsertion) + [self setFilterPredicate:nil]; + + [[self arrangedObjects] insertObject:anObject atIndex:anIndex]; + + if ([self selectsInsertedObjects]) + [self setSelectionIndex:anIndex]; + else + { + [self willChangeValueForKey:@"selectionIndexes"] + [[self selectionIndexes] shiftIndexesStartingAtIndex:anIndex by:1]; + [self didChangeValueForKey:@"selectionIndexes"]; + } + + if ([self avoidsEmptySelection] && [[self selectionIndexes] count] <= 0 && [_contentObject count] > 0) + [self setSelectionIndexes:[CPIndexSet indexSetWithIndex:0]]; +} + - (void)removeObject:(id)object { if (![self canRemove]) From bb3c08ce0ba6b6a52164c131b6d22b02e39f74e1 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Mon, 26 Jul 2010 14:04:26 +0200 Subject: [PATCH 4/7] add CPArrayController test --- Tests/AppKit/CPArrayControllerTest.j | 86 ++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Tests/AppKit/CPArrayControllerTest.j diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j new file mode 100644 index 000000000..4ea774920 --- /dev/null +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -0,0 +1,86 @@ +@implementation CPArrayControllerTest : OJTestCase +{ + CPArrayController _arrayController @accessors(property=arrayController); + CPArray _contentArray @accessors(property=contentArray); +} + +- (void)setUp +{ + _contentArray = []; + + [_contentArray addObject:[Person personWithName:@"Francisco" age:21]]; + [_contentArray addObject:[Person personWithName:@"Ross" age:30]]; + [_contentArray addObject:[Person personWithName:@"Tom" age:15]]; + + _arrayController = [[CPArrayController alloc] initWithContent:[self contentArray]]; +} + +- (void)testInitWithContent +{ + [self assert:[self contentArray] equals:[[self arrayController] contentArray]]; + [self assert:[_CPObservableArray class] equals:[[[self arrayController] arrangedObjects] class]]; +} + +- (void)testSetContent +{ + otherContent = [@"5", @"6"]; + [[self arrayController] setContent:otherContent]; + + [self assert:otherContent equals:[[self arrayController] contentArray]]; + [self assert:[_CPObservableArray class] equals:[[[self arrayController] arrangedObjects] class]]; +} + +- (void)testInsertObjectAtArrangedObjectIndex +{ + var object = [Person personWithName:@"Klaas Pieter" age:24], + arrayController = [self arrayController]; + + [arrayController setSortDescriptors:[[CPSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]]]; + [arrayController insertObject:object atArrangedObjectIndex:1]; + + [self assert:object equals:[[arrayController arrangedObjects] objectAtIndex:1]]; +} + +- (void)testContentBinding +{ + [[self arrayController] bind:@"contentArray" toObject:self withKeyPath:@"contentArray" options:0]; + + [self assert:[[self arrayController] contentArray] equals:[self contentArray]]; + + [[self mutableArrayValueForKey:@"contentArray"] addObject:@"4"]; + [self assert:[self contentArray] equals:[[self arrayController] contentArray]]; + + [[self arrayController] insertObject:@"2" atArrangedObjectIndex:1]; + [self assert:[[self arrayController] contentArray] equals:[self contentArray]]; +} + +@end + +@implementation Person : CPObject +{ + CPString _name @accessors(property=name); + int _age @accessors(property=age); +} + ++ (id)personWithName:(CPString)aName age:(int)anAge +{ + return [[self alloc] initWithName:aName age:anAge]; +} + +- (id)initWithName:(CPString)aName age:(int)anAge +{ + if (self = [super init]) + { + _name = aName; + _age = anAge; + } + + return self; +} + +- (CPString)description +{ + return [CPString stringWithFormat:@"%@ : %@", [self name], [self age]]; +} + +@end \ No newline at end of file From b9c2becd3e6e9eb1ed82e37d440fc6a528e19af0 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Tue, 27 Jul 2010 09:09:59 +0200 Subject: [PATCH 5/7] implement CPIndexSet isEqual: --- Foundation/CPIndexSet.j | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Foundation/CPIndexSet.j b/Foundation/CPIndexSet.j index 266c7273e..af528b169 100644 --- a/Foundation/CPIndexSet.j +++ b/Foundation/CPIndexSet.j @@ -130,6 +130,17 @@ return self; } +- (BOOL)isEqual:(id)anObject +{ + if (self === anObject) + return YES; + + if (!anObject || ![anObject isKindOfClass:[CPIndexSet class]]) + return NO; + + return [self isEqualToIndexSet:anObject]; +} + // Querying an Index Set /*! Compares the receiver with the provided index set. From 48a5d55af714a89f88269a4ced9828c1e03fcd4f Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Tue, 27 Jul 2010 09:10:41 +0200 Subject: [PATCH 6/7] fix CPArrayController selectPrevious: and selectNext: Also added unit-tests for canSelectPrevious, canSelectNext, selectPrevious: and selectNext: --- AppKit/CPArrayController.j | 24 +++++------ Tests/AppKit/CPArrayControllerTest.j | 59 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index a799b296d..189485a8c 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -358,29 +358,29 @@ //Moving selection --(BOOL)canSelectPrevious +- (BOOL)canSelectPrevious { return [[self selectionIndexes] firstIndex] > 0 } --(BOOL)canSelectNext +-(void)selectPrevious:(id)sender { - return [[self selectionIndexes] firstIndex] < [[self arrangedObjects] count] -1; -} + var index = [[self selectionIndexes] firstIndex] - 1; --(void)selectNext:(id)sender -{ - var index = [[self selectionIndexes] firstIndex] + 1 || 0; - - if (index < [[self arrangedObjects] count]) + if (index >= 0) [self setSelectionIndexes:[CPIndexSet indexSetWithIndex:index]]; } --(void)selectPrevious:(id)sender +- (BOOL)canSelectNext { - var index = [[self selectionIndexes] firstIndex] - 1 || [[self arrangedObjects] count] - 1; + return [[self selectionIndexes] firstIndex] < [[self arrangedObjects] count] - 1; +} - if (index >= 0) +- (void)selectNext:(id)sender +{ + var index = [[self selectionIndexes] firstIndex] + 1; + + if (index < [[self arrangedObjects] count]) [self setSelectionIndexes:[CPIndexSet indexSetWithIndex:index]]; } diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j index 4ea774920..532771848 100644 --- a/Tests/AppKit/CPArrayControllerTest.j +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -41,6 +41,65 @@ [self assert:object equals:[[arrayController arrangedObjects] objectAtIndex:1]]; } +- (void)testSelectPrevious +{ + var arrayController = [self arrayController]; + + // Selection index: 1 + [arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:1]]; + [self assertTrue:[arrayController canSelectPrevious] message:@"index > 0; canSelectPrevious should return YES"] + + [arrayController selectPrevious:self]; + [self assert:[CPIndexSet indexSetWithIndex:0] equals:[arrayController selectionIndexes]]; + + // Selection index: 0 + [self assertFalse:[arrayController canSelectPrevious] message:@"index <= 0; canSelectPrevious should return NO"]; + + [arrayController selectPrevious:self]; + [self assert:[CPIndexSet indexSetWithIndex:0] equals:[arrayController selectionIndexes]]; +} + +- (void)testSelectNext +{ + var arrayController = [self arrayController], + count = [[arrayController arrangedObjects] count]; + + // Selection index: count - 2 + [arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:count - 2]]; + [self assertTrue:[arrayController canSelectNext] message:@"index < (count - 1); canSelectNext should return YES"]; + + [arrayController selectNext:self]; + [self assert:[CPIndexSet indexSetWithIndex:count - 1] equals:[arrayController selectionIndexes]]; + + // Selection index: count - 1 + [self assertFalse:[arrayController canSelectNext] message:@"index >= (count - 1) canSelectNext should return NO"]; + + [arrayController selectNext:self]; + [self assert:[CPIndexSet indexSetWithIndex:count - 1] equals:[arrayController selectionIndexes]]; +} +// - (void)testSelectNext +// { +// var arrayController = [self arrayController], +// arrangedObjects = [arrayController arrangedObjects], +// startIndex = 0, +// selectionIndexes = [CPIndexSet indexSetWithIndex:startIndex]; +// +// [arrayController setSelectionIndexes:selectionIndexes]; +// [arrayController selectNext:self]; +// +// [selectionIndexes shiftIndexesStartingAtIndex:startIndex by:1] +// [self assert:selectionIndexes equals:[arrayController selectionIndexes]]; +// +// // Test that the selection wraps around +// startIndex = [arrangedObjects count] - 1; +// selectionIndexes = [CPIndexSet indexSetWithIndex:startIndex]; +// +// [arrayController setSelectionIndexes:selectionIndexes]; +// [arrayController selectNext:self]; +// +// [self assert:[CPIndexSet indexSetWithIndex:0] equals:[arrayController selectionIndexes]]; +// } + - (void)testContentBinding { [[self arrayController] bind:@"contentArray" toObject:self withKeyPath:@"contentArray" options:0]; From 75ceeffab8068b4d136a6af8ccc575bb24a8abd5 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Wed, 28 Jul 2010 13:28:53 +0200 Subject: [PATCH 7/7] remove commented code from CPArrayControllerTest --- Tests/AppKit/CPArrayControllerTest.j | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j index 532771848..c7bf08a96 100644 --- a/Tests/AppKit/CPArrayControllerTest.j +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -77,28 +77,6 @@ [arrayController selectNext:self]; [self assert:[CPIndexSet indexSetWithIndex:count - 1] equals:[arrayController selectionIndexes]]; } -// - (void)testSelectNext -// { -// var arrayController = [self arrayController], -// arrangedObjects = [arrayController arrangedObjects], -// startIndex = 0, -// selectionIndexes = [CPIndexSet indexSetWithIndex:startIndex]; -// -// [arrayController setSelectionIndexes:selectionIndexes]; -// [arrayController selectNext:self]; -// -// [selectionIndexes shiftIndexesStartingAtIndex:startIndex by:1] -// [self assert:selectionIndexes equals:[arrayController selectionIndexes]]; -// -// // Test that the selection wraps around -// startIndex = [arrangedObjects count] - 1; -// selectionIndexes = [CPIndexSet indexSetWithIndex:startIndex]; -// -// [arrayController setSelectionIndexes:selectionIndexes]; -// [arrayController selectNext:self]; -// -// [self assert:[CPIndexSet indexSetWithIndex:0] equals:[arrayController selectionIndexes]]; -// } - (void)testContentBinding {