-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReactiveNSXMLParserLibTests.m
203 lines (163 loc) · 7.1 KB
/
ReactiveNSXMLParserLibTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// ReactiveNSXMLParserLibTests.m
//
//
// The MIT License (MIT)
//
// Copyright (c) 2014 Alex Manarpies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import <XCTest/XCTest.h>
#import <ReactiveCocoa.h>
#import <TRVSMonitor.h>
#import "NSXMLParser+ReactiveCocoa.h"
/**
* Unit tests for NSXMLParser+ReactiveCocoa.
*
* A note on TRVSMonitor:
* TRVSMonitor is a utility which blocks the current thread while tasks on another
* thread continues to run. This allows the XCTestCase to remain running until
* the async code completes.
*/
@interface ReactiveNSXMLParserLibTests : XCTestCase
@property (nonatomic,copy) ElementFilterBlock filterBlock;
@property (nonatomic,copy) NSString *xmlString;
@property (nonatomic,copy) NSData *xmlData;
@end
@implementation ReactiveNSXMLParserLibTests
- (void)setUp
{
[super setUp];
// Create a preset filter which rejects non-podcast related elements
NSSet *elementFilter = [NSSet setWithArray:@[@"title", @"itunes:author", @"enclosure", @"pubDate", @"link", // General
@"image", @"width", @"height", @"url", // Album art
@"item", @"itunes:subtitle", @"itunes:summary"]]; // Items
self.filterBlock = ^BOOL(NSString *elementName) {
return [elementFilter containsObject:elementName];
};
// Some XML in string format
self.xmlString = @"<rss><item><title>Test 1</title></item><item><title>Test 2</title></item></rss>";
// Some XML in NSData format
self.xmlData = [self.xmlString dataUsingEncoding:NSUTF8StringEncoding];
}
- (void)tearDown
{
// TODO
[super tearDown];
}
#pragma mark - Tests
/**
* Tests whether the Windows Weekly podcast feed is parsable.
* Only parses a subset of elements (see self.filterBlock).
* Requires an internet connection.
*/
- (void)testParseWindowsWeeklyFromURL
{
TRVSMonitor *monitor = [TRVSMonitor monitor];
[[NSXMLParser rac_dictionaryFromURL:[NSURL URLWithString:@"http://feeds.twit.tv/ww.xml"] elementFilter:self.filterBlock] subscribeNext:^(NSDictionary *feed) {
XCTAssertTrue([feed count] > 0, @"Feed should contain child nodes");
} error:^(NSError *error) {
XCTFail(@"%@", error);
[monitor signal];
} completed:^{
[monitor signal];
}];
[monitor wait];
}
- (void)testParseWindowsWeeklyFromData
{
TRVSMonitor *monitor = [TRVSMonitor monitor];
[[NSXMLParser rac_dictionaryFromData:self.xmlData elementFilter:self.filterBlock] subscribeNext:^(NSDictionary *feed) {
XCTAssertTrue([feed count] > 0, @"Feed should contain child nodes");
} error:^(NSError *error) {
XCTFail(@"%@", error);
[monitor signal];
} completed:^{
[monitor signal];
}];
[monitor wait];
}
- (void)testParseWindowsWeeklyFromString
{
TRVSMonitor *monitor = [TRVSMonitor monitor];
[[NSXMLParser rac_dictionaryFromString:self.xmlString elementFilter:self.filterBlock] subscribeNext:^(NSDictionary *feed) {
XCTAssertTrue([feed count] > 0, @"Feed should contain child nodes");
} error:^(NSError *error) {
XCTFail(@"%@", error);
[monitor signal];
} completed:^{
[monitor signal];
}];
[monitor wait];
}
/**
* Tests whether the Windows Weekly podcast feed is parsable without filters.
* Requires an internet connection.
*/
- (void)testParseWindowsWeeklyNoFilter
{
TRVSMonitor *monitor = [TRVSMonitor monitor];
[[NSXMLParser rac_dictionaryFromURL:[NSURL URLWithString:@"http://feeds.twit.tv/ww.xml"] elementFilter:nil] subscribeNext:^(NSDictionary *feed) {
XCTAssertTrue([feed count] > 0, @"Feed should contain child nodes");
} error:^(NSError *error) {
XCTFail(@"%@", error);
[monitor signal];
} completed:^{
[monitor signal];
}];
[monitor wait];
}
/**
* Tests whether multiple feeds can be parsed in sequence.
* Demonstrates how to map each NSDictionary into a different datatype and
* collect these into a single NSArray.
* Requires an internet connection.
*/
- (void)testParseMultipleFeeds
{
TRVSMonitor *monitor = [TRVSMonitor monitor];
// Store signals for each parse job in this array.
NSArray *signals = @[[NSXMLParser rac_dictionaryFromURL:[NSURL URLWithString:@"http://feeds.twit.tv/ww.xml"] elementFilter:self.filterBlock],
[NSXMLParser rac_dictionaryFromURL:[NSURL URLWithString:@"http://feeds.twit.tv/sn.xml"] elementFilter:self.filterBlock]];
// [RACSignal -concat] creates a list of aforementioned signals an executes
// them *sequentially*. If you desire parallel execution, you could use one of
// the [RACSignal -conmbine..] methods.
[[[[RACSignal concat:signals] map:^NSDictionary*(NSDictionary *result) {
// [RACSignal -map:] transforms the dictionary into the desired data type
// (this happens to be another dictionary, for demonstration purposes.
// It can be whichever class you wish, however.
NSDictionary *podcast = @{@"title": [result valueForKeyPath:@"title.text"]};
return podcast;
}] collect] subscribeNext:^(NSArray *podcasts) {
// [RACSignal -collect:] aggregates all values into a single NSArray.
// This is where we'd generally hand the result back to any subscribers.
// Quickly assert whether the NSArray contains the correct data..
XCTAssertTrue([podcasts count] == 2, @"List should contain 2 podcasts");
XCTAssertTrue(podcasts[0][@"title"] != nil, @"First podcast should contain a title");
XCTAssertTrue(podcasts[1][@"title"] != nil, @"Second podcast should contain a title");
// Note that these events come in on a background scheduler (thread/queue).
// If you want these to arrive on the main queue, use [RACSignal -deliverOn:].
[monitor signal];
} error:^(NSError *error) {
XCTFail(@"%@", error);
[monitor signal];
}];
[monitor wait];
}
@end