sogo/Tests/Unit/TestSBJsonParser.m
Wolfgang Sourdeau c9afeb9827 Monotone-Parent: 6dfadf575c7c0be4ae75752239ab48f7531dba38
Monotone-Revision: 4dead0c3c1d9edc0c48e5bfb39f0ec7a89f28007

Monotone-Author: wsourdeau@inverse.ca
Monotone-Date: 2011-04-01T21:13:19
Monotone-Branch: ca.inverse.sogo
2011-04-01 21:13:19 +00:00

96 lines
3 KiB
Objective-C

/* TestSBJsonParser.m - this file is part of SOGo
*
* Copyright (C) 2010 Inverse inc.
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>
#import <SBJson/SBJsonParser.h>
#import "SOGo/NSString+Utilities.h"
#import "SOGoTest.h"
@interface TestSBJsonParser : SOGoTest
@end
@implementation TestSBJsonParser
- (void) test_parseJSONString
{
SBJsonParser *parser;
NSString *currentString, *error;
NSArray *expected;
NSObject *resultObject;
int count;
NSString *testStrings[] = { @"\"\\\\\"", @"\\",
@"\"\\u0041\"", @"A",
@"\"\\u000A\"", @"\n",
@"\"\\u000a\"", @"\n",
nil };
parser = [SBJsonParser new];
[parser autorelease];
count = 0;
while ((currentString = testStrings[count * 2]))
{
resultObject = [parser objectWithString: [NSString stringWithFormat:
@"[%@]",
currentString]];
expected = [NSArray arrayWithObject: testStrings[count * 2 + 1]];
error = [NSString stringWithFormat:
@"objects '%@' and '%@' differs (count: %d)",
expected, resultObject, count];
testEqualsWithMessage(expected, resultObject, error);
count++;
}
}
- (void) test_parseJSONNumber
{
SBJsonParser *parser;
NSObject *result;
parser = [SBJsonParser new];
[parser autorelease];
result = [parser objectWithString: @""];
testEquals (result, nil);
result = [parser objectWithString: @"[ 0 ]"];
testEquals (result, [NSArray arrayWithObject: [NSNumber numberWithInt: 0]]);
result = [parser objectWithString: @"[ -1 ]"];
testEquals (result, [NSArray arrayWithObject: [NSNumber numberWithInt: -1]]);
/* TODO: the 2 following fail because NSDecimalNumber does not implement
"compare:" */
result = [parser objectWithString: @"[ 12.3456 ]"];
testEquals (result, [NSArray arrayWithObject: [NSNumber numberWithDouble: 12.3456]]);
result = [parser objectWithString: @"[ -312.3456 ]"];
testEquals (result, [NSArray arrayWithObject: [NSNumber numberWithDouble: -312.3456]]);
}
@end