forked from BlocksKit/BlocksKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSArrayBlocksKitTest.m
More file actions
221 lines (182 loc) · 6.55 KB
/
Copy pathNSArrayBlocksKitTest.m
File metadata and controls
221 lines (182 loc) · 6.55 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// NSArrayBlocksKitTest.m
// BlocksKit Unit Tests
//
// Contributed by Kai Wu.
//
#import <XCTest/XCTest.h>
#import <BlocksKit/NSArray+BlocksKit.h>
@interface NSArrayBlocksKitTest : XCTestCase
@end
@implementation NSArrayBlocksKitTest {
NSArray *_subject;
NSArray *_integers;
NSArray *_floats;
NSInteger _total;
}
- (void)setUp {
_subject = @[ @"1", @"22", @"333" ];
_integers = @[@(1), @(2), @(3)];
_floats = @[@(.1), @(.2), @(.3)];
_total = 0;
}
- (void)tearDown {
_subject = nil;
}
- (void)testEach {
void (^senderBlock)(NSString *) = ^(NSString *sender) {
_total += [sender length];
};
[_subject bk_each:senderBlock];
XCTAssertEqual(_total, (NSInteger)6, @"total length of \"122333\" is %ld", (long)_total);
}
- (void)testMatch {
BOOL(^validationBlock)(id) = ^(NSString *obj) {
_total += [obj length];
BOOL match = ([obj intValue] == 22) ? YES : NO;
return match;
};
id found = [_subject bk_match:validationBlock];
// bk_match: is functionally identical to bk_select:, but will stop and return on the first match
XCTAssertEqual(_total, (NSInteger)3, @"total length of \"122\" is %ld", (long)_total);
XCTAssertEqual(found, @"22", @"matched object is %@", found);
}
- (void)testNotMatch {
BOOL(^validationBlock)(id) = ^(NSString *obj) {
_total += [obj length];
BOOL match = ([obj intValue] == 4444) ? YES : NO;
return match;
};
id found = [_subject bk_match:validationBlock];
// @return Returns the object if found, `nil` otherwise.
XCTAssertEqual(_total, (NSInteger)6, @"total length of \"122333\" is %ld", (long)_total);
XCTAssertNil(found, @"no matched object");
}
- (void)testSelect {
BOOL(^validationBlock)(id) = ^(NSString *obj) {
_total += [obj length];
BOOL match = ([obj intValue] < 300) ? YES : NO;
return match;
};
NSArray *found = [_subject bk_select:validationBlock];
XCTAssertEqual(_total, (NSInteger)6, @"total length of \"122333\" is %ld", (long)_total);
NSArray *target = @[ @"1", @"22" ];
XCTAssertEqualObjects(found, target, @"selected items are %@", found);
}
- (void)testSelectedNone {
BOOL(^validationBlock)(id) = ^(NSString *obj) {
_total += [obj length];
BOOL match = ([obj intValue] > 400) ? YES : NO;
return match;
};
NSArray *found = [_subject bk_select:validationBlock];
XCTAssertEqual(_total, (NSInteger)6, @"total length of \"122333\" is %ld", (long)_total);
XCTAssertTrue(found.count == 0, @"no item is selected");
}
- (void)testReject {
BOOL(^validationBlock)(id) = ^(NSString *obj) {
_total += [obj length];
BOOL match = ([obj intValue] > 300) ? YES : NO;
return match;
};
NSArray *left = [_subject bk_reject:validationBlock];
XCTAssertEqual(_total, (NSInteger)6, @"total length of \"122333\" is %ld", (long)_total);
NSArray *target = @[ @"1", @"22" ];
XCTAssertEqualObjects(left, target, @"not rejected items are %@", left);
}
- (void)testRejectedAll {
BOOL(^validationBlock)(id) = ^(NSString *obj) {
_total += [obj length];
BOOL match = ([obj intValue] < 400) ? YES : NO;
return match;
};
NSArray *left = [_subject bk_reject:validationBlock];
XCTAssertEqual(_total, (NSInteger)6, @"total length of \"122333\" is %ld", (long)_total);
XCTAssertTrue(left.count == 0, @"all items are rejected");
}
- (void)testMap {
id(^transformBlock)(id) = ^(NSString *obj) {
_total += [obj length];
return [obj substringToIndex:1];
};
NSArray *transformed = [_subject bk_map:transformBlock];
XCTAssertEqual(_total, (NSInteger)6, @"total length of \"122333\" is %ld", (long)_total);
NSArray *target = @[ @"1", @"2", @"3" ];
XCTAssertEqualObjects(transformed, target, @"transformed items are %@", transformed);
}
- (void)testReduceWithBlock {
id(^accumlationBlock)(id, id) = ^(id sum,id obj) {
return [sum stringByAppendingString:obj];
};
NSString *concatenated = [_subject bk_reduce:@"" withBlock:accumlationBlock];
XCTAssertTrue([concatenated isEqualToString:@"122333"], @"concatenated string is %@", concatenated);
}
- (void)testReduceWithBlockInteger {
NSInteger(^accumlationBlockInteger)(NSInteger, id) = ^(NSInteger result, id obj) {
return result + [obj intValue];
};
NSInteger result = [_integers bk_reduceInteger:0 withBlock:accumlationBlockInteger];
XCTAssertEqual(result, (NSInteger)6, @"reduce int result is %ld", (long)result);
}
- (void)testReduceWithBlockFloat {
CGFloat(^accumlationBlockFloat)(CGFloat, id) = ^CGFloat(CGFloat result, id obj) {
#if __LP64__
return result + [obj doubleValue];
#else
return result + [obj floatValue];
#endif
};
CGFloat result = [_floats bk_reduceFloat:.0 withBlock:accumlationBlockFloat];
#if __LP64__
CGFloat accuracy = DBL_EPSILON;
#else
CGFloat accuracy = FLT_EPSILON;
#endif
XCTAssertEqualWithAccuracy(result, (CGFloat)0.6, accuracy, @"reduce float result is %f", result);
}
- (void)testAny {
// Check if array has element with prefix 1
BOOL(^existsBlockTrue)(id) = ^(id obj) {
return [obj hasPrefix:@"1"];
};
BOOL(^existsBlockFalse)(id) = ^(id obj) {
return [obj hasPrefix:@"4"];
};
BOOL letterExists = [_subject bk_any:existsBlockTrue];
XCTAssertTrue(letterExists, @"letter is not in array");
BOOL letterDoesNotExist = [_subject bk_any:existsBlockFalse];
XCTAssertFalse(letterDoesNotExist, @"letter is in array");
}
- (void)testAll {
NSArray *names = @[ @"John", @"Joe", @"Jon", @"Jester" ];
NSArray *names2 = @[ @"John", @"Joe", @"Jon", @"Mary" ];
// Check if array has element with prefix 1
BOOL(^nameStartsWithJ)(id) = ^(id obj) {
return [obj hasPrefix:@"J"];
};
BOOL allNamesStartWithJ = [names bk_all:nameStartsWithJ];
XCTAssertTrue(allNamesStartWithJ, @"all names do not start with J in array");
BOOL allNamesDoNotStartWithJ = [names2 bk_all:nameStartsWithJ];
XCTAssertFalse(allNamesDoNotStartWithJ, @"all names do start with J in array");
}
- (void)testNone {
NSArray *names = @[ @"John", @"Joe", @"Jon", @"Jester" ];
NSArray *names2 = @[ @"John", @"Joe", @"Jon", @"Mary" ];
// Check if array has element with prefix 1
BOOL(^nameStartsWithM)(id) = ^(id obj) {
return [obj hasPrefix:@"M"];
};
BOOL noNamesStartWithM = [names bk_none:nameStartsWithM];
XCTAssertTrue(noNamesStartWithM, @"some names start with M in array");
BOOL someNamesStartWithM = [names2 bk_none:nameStartsWithM];
XCTAssertFalse(someNamesStartWithM, @"no names start with M in array");
}
- (void)testCorresponds {
NSArray *numbers = @[ @(1), @(2), @(3) ];
NSArray *letters = @[ @"1", @"2", @"3" ];
BOOL doesCorrespond = [numbers bk_corresponds:letters withBlock:^(id number, id letter) {
return [[number stringValue] isEqualToString:letter];
}];
XCTAssertTrue(doesCorrespond, @"1,2,3 does not correspond to \"1\",\"2\",\"3\"");
}
@end