Statistics
| Branch: | Revision:

root / json-framework-3.2.0 / Tests / ErrorTest.m @ 3ebe9884

History | View | Annotate | Download (5.5 kB)

1
/*
2
 Copyright (C) 2007-2011 Stig Brautaset. All rights reserved.
3

    
4
 Redistribution and use in source and binary forms, with or without
5
 modification, are permitted provided that the following conditions
6
 are met:
7

    
8
 * Redistributions of source code must retain the above copyright
9
   notice, this list of conditions and the following disclaimer.
10

    
11
 * Redistributions in binary form must reproduce the above copyright
12
   notice, this list of conditions and the following disclaimer in the
13
   documentation and/or other materials provided with the distribution.
14

    
15
 * Neither the name of the author nor the names of its contributors
16
   may be used to endorse or promote products derived from this
17
   software without specific prior written permission.
18

    
19
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30

    
31
 */
32

    
33

    
34
#import "JsonTestCase.h"
35

    
36

    
37
#define SBAssertStringContains(e, s) \
38
STAssertTrue([e rangeOfString:s].location != NSNotFound, @"%@ vs %@", e, s)
39

    
40
@interface ErrorTest : JsonTestCase
41
@end
42

    
43
@implementation ErrorTest
44

    
45
- (void)setUp {
46
    [super setUp];
47
    parser.maxDepth = 4u;
48
    writer.maxDepth = 4u;
49
}
50

    
51
- (NSString*)otherFileName {
52
    return @"error";
53
}
54

    
55
- (void)testData {
56
    [self foreachTestInSuite:@"Tests/Data/invalid" apply:^(NSString *inpath, NSString *errpath) {
57
        NSData *input = [NSData dataWithContentsOfFile:inpath options:0 error:nil];
58
        STAssertNotNil(input, inpath);
59
        
60
        NSString *error = [NSString stringWithContentsOfFile:errpath encoding:NSUTF8StringEncoding error:nil];
61
        STAssertNotNil(error, errpath);
62
        
63
        error = [error stringByReplacingOccurrencesOfString:@"\n" withString:@""];
64
        
65
        STAssertNil([parser objectWithData:input], inpath);
66
        STAssertEqualObjects(parser.error, error, @"%@: %@", inpath, input);
67
        
68
    }];
69
    
70
    STAssertEquals(count, (NSUInteger)31, nil);
71
}
72

    
73
- (void)testWriteRecursion {
74
    // create a challenge!
75
    NSMutableArray *a1 = [NSMutableArray array];
76
    NSMutableArray *a2 = [NSMutableArray arrayWithObject:a1];
77
    [a1 addObject:a2];
78
    
79
    STAssertNil([writer stringWithObject:a1], nil);
80
    STAssertEqualObjects(writer.error, @"Nested too deep", writer.error);
81
}
82

    
83

    
84
- (void)testUnsupportedObject {
85
    
86
    STAssertNil([writer stringWithObject:[NSData data]], nil);
87
    STAssertNotNil(writer.error, nil);
88
}
89

    
90
- (void)testNonStringDictionaryKey {
91
    NSArray *keys = [NSArray arrayWithObjects:[NSNull null],
92
                     [NSNumber numberWithInt:1],
93
                     [NSArray array],
94
                     [NSDictionary dictionary],
95
                     nil];
96
    
97
    for (id key in keys) {
98
        NSDictionary *object = [NSDictionary dictionaryWithObject:@"1" forKey:key];
99
        STAssertEqualObjects([writer stringWithObject:object], nil, nil);
100
        STAssertNotNil(writer.error, nil);
101
    }
102
}
103

    
104

    
105
- (void)testScalar {
106
    NSArray *fragments = [NSArray arrayWithObjects:@"foo", @"", [NSNull null], [NSNumber numberWithInt:1], [NSNumber numberWithBool:YES], nil];
107
    for (NSUInteger i = 0; i < [fragments count]; i++) {
108
        NSString *fragment = [fragments objectAtIndex:i];
109
        
110
        // We don't check the convenience category here, like we do for parsing,
111
        // because the category is explicitly on the NSArray and NSDictionary objects.
112
        // STAssertNil([fragment JSONRepresentation], nil);
113
        
114
        STAssertNil([writer stringWithObject:fragment], @"%@", fragment);
115
        SBAssertStringContains(parser.error, @"Not valid type for JSON");
116
    }
117
}
118

    
119
- (void)testInfinity {
120
    NSArray *obj = [NSArray arrayWithObject:[NSNumber numberWithDouble:INFINITY]];    
121
    STAssertNil([writer stringWithObject:obj], @"%@", obj);
122
    SBAssertStringContains(parser.error, @"Infinity is not a valid number in JSON");
123
}
124

    
125
- (void)testNegativeInfinity {
126
    NSArray *obj = [NSArray arrayWithObject:[NSNumber numberWithDouble:-INFINITY]];
127
    
128
    STAssertNil([writer stringWithObject:obj], nil);
129
    SBAssertStringContains(parser.error, @"Infinity is not a valid number in JSON");
130
}
131

    
132
- (void)testNaN {
133
    NSArray *obj = [NSArray arrayWithObject:[NSDecimalNumber notANumber]];
134
    
135
    STAssertNil([writer stringWithObject:obj], nil);
136
    SBAssertStringContains(parser.error, @"NaN is not a valid number in JSON");
137
}
138

    
139
- (void)testNil {
140
    STAssertNil([parser objectWithString:nil], nil);
141
    STAssertEqualObjects(parser.error, @"Input was 'nil'", nil);
142
    
143
    STAssertNil([writer stringWithObject:nil], nil);
144
    SBAssertStringContains(parser.error, @"Input was 'nil'");
145
    
146
}
147

    
148
- (void)testWriteDepth {
149
    writer.maxDepth = 2;
150
    
151
    NSArray *a1 = [NSArray array];
152
    NSArray *a2 = [NSArray arrayWithObject:a1];
153
    STAssertNotNil([writer stringWithObject:a2], nil);
154
    
155
    NSArray *a3 = [NSArray arrayWithObject:a2];
156
    STAssertNil([writer stringWithObject:a3], nil);
157
    STAssertEqualObjects(writer.error, @"Nested too deep", writer.error);
158
}
159

    
160
@end