diff --git a/SoObjects/SOGo/NSScanner+BSJSONAdditions.m b/SoObjects/SOGo/NSScanner+BSJSONAdditions.m index 6f9f41efd..2e653b875 100644 --- a/SoObjects/SOGo/NSScanner+BSJSONAdditions.m +++ b/SoObjects/SOGo/NSScanner+BSJSONAdditions.m @@ -26,6 +26,8 @@ // Unit Tests adapted from Jonathan Wight's CocoaJSON code: http://www.toxicsoftware.com // I have included those adapted unit tests in this package. +#include + #import #import #import @@ -224,9 +226,9 @@ NSString *jsonNullString = @"null"; [self scanJSONWhiteSpace]; NSString *substring = [[self string] substringWithRange:NSMakeRange([self scanLocation], 1)]; - unsigned int trueLocation = [[self string] rangeOfString:jsonTrueString options:0 range:NSMakeRange([self scanLocation], ([[self string] length] - [self scanLocation]))].location; - unsigned int falseLocation = [[self string] rangeOfString:jsonFalseString options:0 range:NSMakeRange([self scanLocation], ([[self string] length] - [self scanLocation]))].location; - unsigned int nullLocation = [[self string] rangeOfString:jsonNullString options:0 range:NSMakeRange([self scanLocation], ([[self string] length] - [self scanLocation]))].location; + NSUInteger trueLocation = [[self string] rangeOfString:jsonTrueString options:0 range:NSMakeRange([self scanLocation], ([[self string] length] - [self scanLocation]))].location; + NSUInteger falseLocation = [[self string] rangeOfString:jsonFalseString options:0 range:NSMakeRange([self scanLocation], ([[self string] length] - [self scanLocation]))].location; + NSUInteger nullLocation = [[self string] rangeOfString:jsonNullString options:0 range:NSMakeRange([self scanLocation], ([[self string] length] - [self scanLocation]))].location; if ([substring isEqualToString:jsonStringDelimiterString]) { result = [self scanJSONString:value]; @@ -258,9 +260,39 @@ NSString *jsonNullString = @"null"; //NSDecimal decimal; //BOOL result = [self scanDecimal:&decimal]; //*number = [NSDecimalNumber decimalNumberWithDecimal:decimal]; - int value; - BOOL result = [self scanInt: &value]; - *number = [NSNumber numberWithInt: value]; + long long intValue, commaValue; + double doubleValue; + NSUInteger curLocation, length; + NSString *stringValue; + + stringValue = [self string]; + + BOOL result = [self scanLongLong: &intValue]; + if (result) + { + curLocation = [self scanLocation]; + if (curLocation < [stringValue length] + && [stringValue characterAtIndex: curLocation] == '.') + { + [self scanString: @"." intoString: NULL]; + if ([self scanLongLong: &commaValue]) + { + result = YES; + length = [self scanLocation] - curLocation - 1; + if (intValue < 0) + commaValue = -commaValue; + doubleValue = intValue + commaValue / pow(10.0, length); + *number = [NSNumber numberWithDouble: doubleValue]; + } + else + *number = [NSNumber numberWithLongLong: intValue]; + } + else + *number = [NSNumber numberWithLongLong: intValue]; + } + else + *number = nil; + return result; } diff --git a/Tests/Unit/SOGoTest.h b/Tests/Unit/SOGoTest.h index 3e9e43cb4..bbc192ed3 100644 --- a/Tests/Unit/SOGoTest.h +++ b/Tests/Unit/SOGoTest.h @@ -66,7 +66,7 @@ #define failIf(c) test(!c) #define testEquals(a,b) \ - testWithMessage(([a isEqual: b]), \ + testWithMessage(((a == b) || ([a isEqual: b])), \ ([NSString stringWithFormat: @"objects '%@' and '%@' differs", (a), (b)])) #define testEqualsWithMessage(a,b,m) \ diff --git a/Tests/Unit/TestBSJSONAdditions.m b/Tests/Unit/TestBSJSONAdditions.m index 1384167c5..ec7698786 100644 --- a/Tests/Unit/TestBSJSONAdditions.m +++ b/Tests/Unit/TestBSJSONAdditions.m @@ -22,10 +22,13 @@ #import #import +#import #import "SOGo/NSScanner+BSJSONAdditions.h" #import "SOGo/NSDictionary+BSJSONAdditions.h" +#import "SOGo/NSString+Utilities.h" + #import "SOGoTest.h" @interface TestNSScannerBSJSONAdditions : SOGoTest @@ -60,9 +63,36 @@ } } +- (void) test_scanJSONNumber +{ + NSScanner *testScanner; + NSNumber *result; + + testScanner = [NSScanner scannerWithString: @""]; + [testScanner scanJSONNumber: &result]; + testEquals (result, nil); + + testScanner = [NSScanner scannerWithString: @"0"]; + [testScanner scanJSONNumber: &result]; + testEquals (result, [NSNumber numberWithInt: 0]); + + testScanner = [NSScanner scannerWithString: @"-1"]; + [testScanner scanJSONNumber: &result]; + testEquals (result, [NSNumber numberWithInt: -1]); + + testScanner = [NSScanner scannerWithString: @"12.3456"]; + [testScanner scanJSONNumber: &result]; + testEquals (result, [NSNumber numberWithDouble: 12.3456]); + + testScanner = [NSScanner scannerWithString: @"-312.3456"]; + [testScanner scanJSONNumber: &result]; + testEquals (result, [NSNumber numberWithDouble: -312.3456]); +} + @end @interface TestNSDictionaryBSJSONAdditions : SOGoTest + @end @implementation TestNSDictionaryBSJSONAdditions @@ -91,3 +121,4 @@ } @end +