From 8ef7f071951788d58c552ca006beac88c59d5a74 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Tue, 14 Dec 2010 23:33:14 +0100 Subject: [PATCH] Fixed: CPArrayController's addObject and CPArray's insertObject:inArraySortedByDescriptors:. CPArray insertObject:inArraySortedByDescriptors: now quickly inserts objects at the end of the array if no sort descriptors are specified. CPArrayController initialises its sort descriptors to an empty array. Fixed: array controller's addObject added objects to the arranged objects array in a different order than in the content array if no sort descriptor was set. --- AppKit/CPArrayController.j | 1 + Foundation/CPArray.j | 28 +++++++++++++++++----------- Tests/AppKit/CPArrayControllerTest.j | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index bdb3a38cb..6681a768b 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -108,6 +108,7 @@ if (self) { + _sortDescriptors = [CPArray array]; _selectionIndexes = [CPIndexSet indexSet]; } diff --git a/Foundation/CPArray.j b/Foundation/CPArray.j index 942652e6c..ffc056c7f 100755 --- a/Foundation/CPArray.j +++ b/Foundation/CPArray.j @@ -1067,22 +1067,28 @@ CPEnumerationReverse = 1 << 1; - (unsigned)insertObject:(id)anObject inArraySortedByDescriptors:(CPArray)descriptors { - var count = [descriptors count]; + var count = [descriptors count], + index; - var index = [self _indexOfObject:anObject sortedByFunction:function(lhs, rhs) + if (count) { - var i = 0, - result = CPOrderedSame; + index = [self _indexOfObject:anObject sortedByFunction:function(lhs, rhs) + { + var i = 0, + result = CPOrderedSame; - while (i < count) - if ((result = [descriptors[i++] compareObject:lhs withObject:rhs]) != CPOrderedSame) - return result; + while (i < count) + if ((result = [descriptors[i++] compareObject:lhs withObject:rhs]) != CPOrderedSame) + return result; - return result; - } context:nil]; + return result; + } context:nil]; - if (index < 0) - index = -index - 1; + if (index < 0) + index = -index - 1; + } + else + index = self.length; [self insertObject:anObject atIndex:index]; return index; diff --git a/Tests/AppKit/CPArrayControllerTest.j b/Tests/AppKit/CPArrayControllerTest.j index 9cac39aee..dfa3c1fdb 100644 --- a/Tests/AppKit/CPArrayControllerTest.j +++ b/Tests/AppKit/CPArrayControllerTest.j @@ -92,6 +92,30 @@ [self assert:[CPIndexSet indexSetWithIndex:count - 1] equals:[arrayController selectionIndexes]]; } +/* + Verify that the arranged objects ordering is correct versus the content array when objects are added with addObject and no sort descriptors are set. +*/ +- (void)testAddObjects +{ + _arrayController = [[CPArrayController alloc] init]; + var content = []; + [_arrayController setContent:content]; + + [_arrayController addObject:[CPNumber numberWithInt:1]]; + [_arrayController addObject:[CPNumber numberWithInt:2]]; + + [self assert:[CPNumber numberWithInt:1] equals:content[0]]; + [self assert:[CPNumber numberWithInt:2] equals:content[1]]; + + [self assert:[CPNumber numberWithInt:1] equals:[_arrayController arrangedObjects][0] message:"arranged objects should be in the correct order"]; + [self assert:[CPNumber numberWithInt:2] equals:[_arrayController arrangedObjects][1] message:"arranged objects should be in the correct order"]; + + [_arrayController rearrangeObjects]; + + [self assert:[CPNumber numberWithInt:1] equals:[_arrayController arrangedObjects][0]]; + [self assert:[CPNumber numberWithInt:2] equals:[_arrayController arrangedObjects][1]]; +} + - (void)testRemoveObjects { var arrayController = [self arrayController];