mirror of
https://github.com/inverse-inc/sogo.git
synced 2026-02-24 18:56:24 +00:00
Monotone-Parent: b2324fd0b056d26c1f593b22e0516160fd06bd3c
Monotone-Revision: 32ad0a6147be7ae69fe1339fe1a08de4fbf7fc9b Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2010-08-31T19:56:45 Monotone-Branch: ca.inverse.sogo
This commit is contained in:
@@ -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 <math.h>
|
||||
|
||||
#import <Foundation/NSArray.h>
|
||||
#import <Foundation/NSDecimalNumber.h>
|
||||
#import <Foundation/NSNull.h>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) \
|
||||
|
||||
@@ -22,10 +22,13 @@
|
||||
|
||||
#import <Foundation/NSException.h>
|
||||
#import <Foundation/NSString.h>
|
||||
#import <Foundation/NSValue.h>
|
||||
|
||||
#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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user