From fc403d679c48995e8ad0e53deb5f32f9fd847ef8 Mon Sep 17 00:00:00 2001 From: Martin Carlberg Date: Tue, 9 Jul 2013 13:37:12 +0200 Subject: [PATCH] New: CPString 'compare:' method handles nil and CPNull in correct way This is the same way as Cocoa works. Also added test case. --- Foundation/CPString.j | 8 ++++++++ Tests/Foundation/CPStringTest.j | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/Foundation/CPString.j b/Foundation/CPString.j index dc04d6aa2..4e7f7996b 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -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; diff --git a/Tests/Foundation/CPStringTest.j b/Tests/Foundation/CPStringTest.j index e3d7a44ce..3bc97c334 100644 --- a/Tests/Foundation/CPStringTest.j +++ b/Tests/Foundation/CPStringTest.j @@ -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