root / json-framework-3.2.0 / Tests / RoundTripTest.m @ 3ebe9884
History | View | Annotate | Download (4.8 kB)
1 |
/* |
---|---|
2 |
Copyright (C) 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 |
@interface RoundTripTest : JsonTestCase |
37 |
@end |
38 |
|
39 |
@implementation RoundTripTest |
40 |
|
41 |
- (void)testString { |
42 |
[self foreachTestInSuite:@"Tests/Data/valid" apply:^(NSString *inpath, NSString *outpath) { |
43 |
NSError *error = nil; |
44 |
NSString *input = [NSString stringWithContentsOfFile:inpath encoding:NSUTF8StringEncoding error:&error]; |
45 |
STAssertNotNil(input, @"%@ - %@", inpath, error); |
46 |
|
47 |
NSString *output = [NSString stringWithContentsOfFile:outpath encoding:NSUTF8StringEncoding error:&error]; |
48 |
STAssertNotNil(output, @"%@ - %@", outpath, error); |
49 |
|
50 |
id object = [parser objectWithString:input]; |
51 |
STAssertNotNil(object, parser.error); |
52 |
|
53 |
NSString *json = [writer stringWithObject:object]; |
54 |
STAssertNotNil(json, writer.error); |
55 |
|
56 |
json = [json stringByAppendingString:@"\n"]; |
57 |
STAssertEqualObjects(json, output, @"%@ vs %@", json, output); |
58 |
}]; |
59 |
|
60 |
STAssertEquals(count, (NSUInteger)17, nil); |
61 |
} |
62 |
|
63 |
|
64 |
- (void)testData { |
65 |
[self foreachTestInSuite:@"Tests/Data/valid" apply:^(NSString *inpath, NSString *outpath) { |
66 |
NSError *error = nil; |
67 |
NSData *input = [NSData dataWithContentsOfFile:inpath]; |
68 |
STAssertNotNil(input, @"%@ - %@", inpath, error); |
69 |
|
70 |
id object = [parser objectWithData:input]; |
71 |
STAssertNotNil(object, nil); |
72 |
|
73 |
NSData *json = [writer dataWithObject:object]; |
74 |
STAssertNotNil(json, nil); |
75 |
|
76 |
NSData *output = [NSData dataWithContentsOfFile:outpath]; |
77 |
STAssertNotNil(output, @"%@ - %@", outpath, error); |
78 |
|
79 |
output = [NSData dataWithBytes:output.bytes length:output.length-1]; |
80 |
STAssertEqualObjects(json, output, nil); |
81 |
}]; |
82 |
|
83 |
STAssertEquals(count, (NSUInteger)17, nil); |
84 |
} |
85 |
|
86 |
|
87 |
- (void)testStringCategory { |
88 |
[self foreachTestInSuite:@"Tests/Data/valid" apply:^(NSString *inpath, NSString *outpath) { |
89 |
NSError *error = nil; |
90 |
NSString *input = [NSString stringWithContentsOfFile:inpath encoding:NSUTF8StringEncoding error:&error]; |
91 |
STAssertNotNil(input, @"%@ - %@", inpath, error); |
92 |
|
93 |
NSString *output = [NSString stringWithContentsOfFile:outpath encoding:NSUTF8StringEncoding error:&error]; |
94 |
STAssertNotNil(output, @"%@ - %@", outpath, error); |
95 |
|
96 |
id object = [input JSONValue]; |
97 |
STAssertNotNil(object, nil); |
98 |
|
99 |
NSString *json = [object JSONRepresentation]; |
100 |
STAssertNotNil(json, nil); |
101 |
|
102 |
json = [json stringByAppendingString:@"\n"]; |
103 |
STAssertEqualObjects(json, output, nil); |
104 |
}]; |
105 |
|
106 |
STAssertEquals(count, (NSUInteger)17, nil); |
107 |
} |
108 |
|
109 |
- (void)testDataCategory { |
110 |
[self foreachTestInSuite:@"Tests/Data/valid" apply:^(NSString *inpath, NSString *outpath) { |
111 |
NSError *error = nil; |
112 |
NSData *input = [NSData dataWithContentsOfFile:inpath]; |
113 |
STAssertNotNil(input, @"%@ - %@", inpath, error); |
114 |
|
115 |
NSString *output = [NSString stringWithContentsOfFile:outpath encoding:NSUTF8StringEncoding error:&error]; |
116 |
STAssertNotNil(output, @"%@ - %@", outpath, error); |
117 |
|
118 |
id object = [input JSONValue]; |
119 |
STAssertNotNil(object, nil); |
120 |
|
121 |
NSString *json = [object JSONRepresentation]; |
122 |
STAssertNotNil(json, nil); |
123 |
|
124 |
json = [json stringByAppendingString:@"\n"]; |
125 |
STAssertEqualObjects(json, output, nil); |
126 |
}]; |
127 |
|
128 |
STAssertEquals(count, (NSUInteger)17, nil); |
129 |
} |
130 |
|
131 |
@end |