mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-19 21:19:24 +00:00
Merge pull request #1959 from mrcarlberg/compare_sort_with_null
CPString 'compare:' method and sort using descriptors handles nil and CPNull in correct way
This commit is contained in:
commit
2973743a0d
4 files changed
+49
-2
No files matched your search
@@ -429,6 +429,9 @@ var sortArrayUsingFunction = function(array, aFunction, aContext)
|
||||
}
|
||||
}
|
||||
|
||||
// This is for speed
|
||||
var CPMutableArrayNull = [CPNull null];
|
||||
|
||||
// Observe that the sort descriptors has the reversed order by the caller
|
||||
var sortArrayUsingJSDescriptors = function(a, d)
|
||||
{
|
||||
@@ -451,7 +454,10 @@ var sortArrayUsingJSDescriptors = function(a, d)
|
||||
aUID,
|
||||
bUID,
|
||||
key,
|
||||
dd;
|
||||
dd,
|
||||
value1,
|
||||
value2,
|
||||
cpNull = CPMutableArrayNull;
|
||||
|
||||
if (dl < 0)
|
||||
return;
|
||||
@@ -519,7 +525,14 @@ var sortArrayUsingJSDescriptors = function(a, d)
|
||||
{
|
||||
dd = d[cn];
|
||||
key = dd.k;
|
||||
o = objj_msgSend(C1[key], dd.s, C2[key]);
|
||||
value1 = C1[key];
|
||||
value2 = C2[key];
|
||||
if (value1 === nil || value1 === cpNull)
|
||||
o = value2 === nil || value2 === cpNull ? CPOrderedSame : CPOrderedAscending;
|
||||
else
|
||||
{
|
||||
o = value2 === nil || value2 === cpNull ? CPOrderedDescending : objj_msgSend(value1, dd.s, value2);
|
||||
}
|
||||
|
||||
if (o && !dd.a)
|
||||
o = -o;
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
@import "CPSortDescriptor.j"
|
||||
@import "CPURL.j"
|
||||
@import "CPValue.j"
|
||||
@import "CPNull.j"
|
||||
|
||||
@class CPException
|
||||
@class CPURL
|
||||
@@ -496,6 +497,9 @@ var CPStringUIDs = new CFMutableDictionary(),
|
||||
return [self compare:aString options:CPCaseInsensitiveSearch];
|
||||
}
|
||||
|
||||
// This is for speed
|
||||
var CPStringNull = [CPNull null];
|
||||
|
||||
/*!
|
||||
Compares the receiver to the specified string, using options.
|
||||
@param aString the string with which to compare
|
||||
@@ -504,6 +508,10 @@ var CPStringUIDs = new CFMutableDictionary(),
|
||||
*/
|
||||
- (CPComparisonResult)compare:(CPString)aString options:(int)aMask
|
||||
{
|
||||
if (aString === nil) return CPOrderedDescending;
|
||||
|
||||
if (aString === CPStringNull) [CPException raise:CPInvalidArgumentException reason:"compare: argument can't be 'CPNull'"];
|
||||
|
||||
var lhs = self,
|
||||
rhs = aString;
|
||||
|
||||
|
||||
@@ -470,6 +470,26 @@
|
||||
[self assert:[5, 4, 4, 3, 2, 2, 1, 1, 1, 1] equals:target];
|
||||
}
|
||||
|
||||
- (void)testThatCPArrayDoesSortCorrectWithNilAndCPNull
|
||||
{
|
||||
var descriptors = [[CPSortDescriptor sortDescriptorWithKey:@"number" ascending:NO]],
|
||||
target = [
|
||||
[[CPPrettyObject alloc] initWithValue:@"a" number:nil],
|
||||
[[CPPrettyObject alloc] initWithValue:@"a" number:[CPNull null]],
|
||||
[[CPPrettyObject alloc] initWithValue:@"a" number:@"Objective-J"],
|
||||
[[CPPrettyObject alloc] initWithValue:@"a" number:[CPNull null]],
|
||||
[[CPPrettyObject alloc] initWithValue:@"a" number:nil],
|
||||
];
|
||||
|
||||
[target sortUsingDescriptors:descriptors];
|
||||
|
||||
[self assert:@"Objective-J" equals:[target[0] number]];
|
||||
[self assert:nil equals:[target[1] number]];
|
||||
[self assert:[CPNull null] equals:[target[2] number]];
|
||||
[self assert:[CPNull null] equals:[target[3] number]];
|
||||
[self assert:nil equals:[target[4] number]];
|
||||
}
|
||||
|
||||
- (void)testMutableCopy
|
||||
{
|
||||
var normalArray = [],
|
||||
|
||||
@@ -566,4 +566,10 @@
|
||||
[self assert:"This is a test none" equals:[noneString stringByTrimmingCharactersInSet:set]];
|
||||
}
|
||||
|
||||
- (void)testCompareWithNil
|
||||
{
|
||||
[self assert:CPOrderedDescending equals:[@"Objective-J" compare:nil]];
|
||||
[self assertThrows:function () { [@"Objective-J" compare:[CPNull null]] }];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user