New: CPString 'compare:' method handles nil and CPNull in correct way

This is the same way as Cocoa works.
Also added test case.
This commit is contained in:
Martin Carlberg
2013-07-09 13:37:12 +02:00
parent e8b2f2e39f
commit fc403d679c
2 changed files with 14 additions and 0 deletions
+8
View File
@@ -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;
+6
View File
@@ -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