root / json-framework-3.2.0 / Tests / StreamParserIntegrationTest.m @ 3ebe9884
History | View | Annotate | Download (4.2 kB)
1 |
/* |
---|---|
2 |
Copyright (c) 2010, Stig Brautaset. |
3 |
All rights reserved. |
4 |
|
5 |
Redistribution and use in source and binary forms, with or without |
6 |
modification, are permitted provided that the following conditions are |
7 |
met: |
8 |
|
9 |
Redistributions of source code must retain the above copyright |
10 |
notice, this list of conditions and the following disclaimer. |
11 |
|
12 |
Redistributions in binary form must reproduce the above copyright |
13 |
notice, this list of conditions and the following disclaimer in the |
14 |
documentation and/or other materials provided with the distribution. |
15 |
|
16 |
Neither the name of the the author nor the names of its contributors |
17 |
may be used to endorse or promote products derived from this software |
18 |
without specific prior written permission. |
19 |
|
20 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
21 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
22 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
23 |
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
24 |
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
25 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
26 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
27 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
28 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
29 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
30 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 |
*/ |
32 |
|
33 |
|
34 |
#import <SenTestingKit/SenTestingKit.h> |
35 |
#import <SBJson/SBJson.h> |
36 |
|
37 |
#import "JsonTestCase.h" |
38 |
|
39 |
@interface StreamParserIntegrationTest : SenTestCase < SBJsonStreamParserAdapterDelegate> { |
40 |
SBJsonStreamParser *parser; |
41 |
SBJsonStreamParserAdapter *adapter; |
42 |
NSUInteger arrayCount, objectCount; |
43 |
NSDirectoryEnumerator *files; |
44 |
NSString *path; |
45 |
} |
46 |
@end |
47 |
|
48 |
@implementation StreamParserIntegrationTest |
49 |
|
50 |
- (void)setUp { |
51 |
adapter = [SBJsonStreamParserAdapter new]; |
52 |
adapter.delegate = self; |
53 |
|
54 |
parser = [SBJsonStreamParser new]; |
55 |
parser.delegate = adapter; |
56 |
parser.supportMultipleDocuments = YES; |
57 |
|
58 |
arrayCount = objectCount = 0u; |
59 |
|
60 |
NSString *suite = @"Tests/Stream/"; |
61 |
path = [JsonTestCase pathForSuite:suite]; |
62 |
files = [[NSFileManager defaultManager] enumeratorAtPath:path]; |
63 |
|
64 |
} |
65 |
|
66 |
- (void)parser:(SBJsonStreamParser *)parser foundArray:(NSArray *)array { |
67 |
arrayCount++; |
68 |
} |
69 |
|
70 |
- (void)parser:(SBJsonStreamParser *)parser foundObject:(NSDictionary *)dict { |
71 |
objectCount++; |
72 |
} |
73 |
|
74 |
/* |
75 |
This test reads a 100k chunk of data downloaded from |
76 |
http://stream.twitter.com/1/statuses/sample.json |
77 |
and split into 1k files. It simulates streaming by parsing |
78 |
this data incrementally. |
79 |
*/ |
80 |
- (void)testMultipleDocuments { |
81 |
NSString *fileName; |
82 |
while ((fileName = [files nextObject])) { |
83 |
NSString *file = [path stringByAppendingPathComponent:fileName]; |
84 |
|
85 |
// Don't accidentally test directories. That would be bad. |
86 |
BOOL isDir = NO; |
87 |
if (NO == [[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDir] || YES == isDir) |
88 |
continue; |
89 |
|
90 |
NSData *data = [NSData dataWithContentsOfMappedFile:file]; |
91 |
STAssertNotNil(data, nil); |
92 |
|
93 |
STAssertEquals([parser parse:data], SBJsonStreamParserWaitingForData, @"%@ - %@", file, parser.error); |
94 |
} |
95 |
STAssertEquals(arrayCount, (NSUInteger)0, nil); |
96 |
STAssertEquals(objectCount, (NSUInteger)98, nil); |
97 |
} |
98 |
|
99 |
- (void)parseArrayOfObjects { |
100 |
[parser parse:[NSData dataWithBytes:"[" length:1]]; |
101 |
for (int i = 1;; i++) { |
102 |
char *utf8 = "{\"foo\":[],\"bar\":[]}"; |
103 |
[parser parse:[NSData dataWithBytes:utf8 length:strlen(utf8)]]; |
104 |
if (i == 100) |
105 |
break; |
106 |
[parser parse:[NSData dataWithBytes:"," length:1]]; |
107 |
} |
108 |
[parser parse:[NSData dataWithBytes:"]" length:1]]; |
109 |
} |
110 |
|
111 |
- (void)testSingleArray { |
112 |
[self parseArrayOfObjects]; |
113 |
STAssertEquals(arrayCount, (NSUInteger)1, nil); |
114 |
STAssertEquals(objectCount, (NSUInteger)0, nil); |
115 |
} |
116 |
|
117 |
- (void)testSkipArray { |
118 |
adapter.levelsToSkip = 1; |
119 |
[self parseArrayOfObjects]; |
120 |
STAssertEquals(arrayCount, (NSUInteger)0, nil); |
121 |
STAssertEquals(objectCount, (NSUInteger)100, nil); |
122 |
} |
123 |
|
124 |
- (void)testSkipArrayAndObject { |
125 |
adapter.levelsToSkip = 2; |
126 |
[self parseArrayOfObjects]; |
127 |
STAssertEquals(arrayCount, (NSUInteger)200, nil); |
128 |
STAssertEquals(objectCount, (NSUInteger)0, nil); |
129 |
} |
130 |
|
131 |
|
132 |
@end |