1/* TestSBJsonParser.m - this file is part of SOGo
2 *
3 * Copyright (C) 2010 Inverse inc.
4 *
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6 *
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING.  If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23
24#import <SBJson/SBJsonParser.h>
25
26#import "SOGo/NSString+Utilities.h"
27
28#import "SOGoTest.h"
29
30@interface TestSBJsonParser : SOGoTest
31@end
32
33@implementation TestSBJsonParser
34
35- (void) test_parseJSONString
36{
37  SBJsonParser *parser;
38  NSString *currentString, *error;
39  NSArray *expected;
40  NSObject *resultObject;
41  int count;
42  NSString *testStrings[] = { @"\"\\\\\"", @"\\",
43                              @"\"\\u0041\"", @"A",
44                              @"\"\\u000A\"", @"\n",
45                              @"\"\\u000a\"", @"\n",
46                              @"\"weird data \\\\ ' \\\"; ^\"", @"weird data \\ ' \"; ^",
47                              nil };
48
49  parser = [SBJsonParser new];
50  [parser autorelease];
51
52  count = 0;
53  while ((currentString = testStrings[count]))
54    {
55      resultObject = [parser objectWithString: [NSString stringWithFormat:
56                                                           @"[%@]",
57                                                         currentString]];
58      expected = [NSArray arrayWithObject: testStrings[count + 1]];
59      error = [NSString stringWithFormat:
60                          @"objects '%@' and '%@' differs (count: %d)",
61                        expected, resultObject, count];
62      testEqualsWithMessage(expected, resultObject, error);
63      count += 2;
64    }
65}
66
67- (void) test_parseJSONNumber
68{
69  SBJsonParser *parser;
70  id result;
71  NSDecimalNumber *obtained, *expected;
72  NSDictionary *locale;
73
74  parser = [SBJsonParser new];
75  [parser autorelease];
76
77  result = [parser objectWithString: @""];
78  testEquals (result, nil);
79
80  result = [parser objectWithString: @"[ 0 ]"];
81  testEquals (result, [NSArray arrayWithObject: [NSNumber numberWithInt: 0]]);
82
83  result = [parser objectWithString: @"[ -1 ]"];
84  testEquals (result, [NSArray arrayWithObject: [NSNumber numberWithInt: -1]]);
85
86#if 0
87  locale = [NSDictionary dictionaryWithObject: @"." forKey: NSLocaleDecimalSeparator];
88  result = [parser objectWithString: @"[ 12.3456 ]"];
89  obtained = [result objectAtIndex: 0];
90  expected = [NSDecimalNumber decimalNumberWithString: @"12.3456" locale: locale];
91  test ([obtained compare: expected] == NSOrderedSame);
92#endif
93
94  locale = nil;
95  result = [parser objectWithString: @"[ -312.3456 ]"];
96  obtained = [result objectAtIndex: 0];
97  expected = [NSDecimalNumber decimalNumberWithString: @"-312.3456" locale: locale];
98  test ([obtained compare: expected] == NSOrderedSame);
99}
100
101@end
102