forked from BlocksKit/BlocksKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSIndexSetBlocksKitTest.m
130 lines (116 loc) · 4.49 KB
/
NSIndexSetBlocksKitTest.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
//
// NSIndexSetBlocksKitTest.m
// BlocksKit Unit Tests
//
// Contributed by Kai Wu.
//
#import <XCTest/XCTest.h>
#import <BlocksKit/NSIndexSet+BlocksKit.h>
@interface NSIndexSetBlocksKitTest : XCTestCase
@end
@implementation NSIndexSetBlocksKitTest {
NSIndexSet *_subject;
NSMutableArray *_target;
}
- (void)setUp {
_target = [@[@"0", @"0", @"0", @"0"] mutableCopy];
_subject = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)];
}
- (void)testEach {
NSMutableString *order = [NSMutableString string];
void(^indexBlock)(NSUInteger) = ^(NSUInteger index) {
[order appendFormat:@"%lu", (unsigned long)index];
_target[index] = [NSString stringWithFormat:@"%lu", (unsigned long)index];
};
[_subject bk_each:indexBlock];
XCTAssertTrue([order isEqualToString:@"123"], @"the index loop order is %@", order);
NSArray *target = @[ @"0", @"1", @"2", @"3" ];
XCTAssertEqualObjects(_target, target, @"the target array becomes %@", _target);
}
- (void)testMatch {
NSMutableString *order = [NSMutableString string];
BOOL(^indexValidationBlock)(NSUInteger) = ^(NSUInteger index) {
[order appendFormat:@"%lu", (unsigned long)index];
BOOL match = NO;
if (index%2 == 0 ) {
_target[index] = [NSString stringWithFormat:@"%lu", (unsigned long)index];
match = YES;
}
return match;
};
NSUInteger found = [_subject bk_match:indexValidationBlock];
XCTAssertTrue([order isEqualToString:@"12"], @"the index loop order is %@", order);
XCTAssertEqualObjects(_target[found], @"2", @"the target array becomes %@", _target);
}
- (void)testNotMatch {
NSMutableString *order = [NSMutableString string];
BOOL(^indexValidationBlock)(NSUInteger) = ^(NSUInteger index) {
[order appendFormat:@"%lu", (unsigned long)index];
BOOL match = index > 4 ? YES : NO;
return match;
};
NSUInteger found = [_subject bk_match:indexValidationBlock];
XCTAssertTrue([order isEqualToString:@"123"], @"the index loop order is %@", order);
XCTAssertEqual((NSUInteger)found, (NSUInteger)NSNotFound, @"no items are found");
}
- (void)testSelect {
NSMutableString *order = [NSMutableString string];
BOOL(^indexValidationBlock)(NSUInteger) = ^(NSUInteger index) {
[order appendFormat:@"%lu", (unsigned long)index];
BOOL match = index < 3 ? YES : NO;
return match;
};
NSIndexSet *found = [_subject bk_select:indexValidationBlock];
XCTAssertTrue([order isEqualToString:@"123"], @"the index loop order is %@", order);
NSIndexSet *target = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1,2)];
XCTAssertEqualObjects(found, target, @"the selected index set is %@", found);
}
- (void)testSelectedNone {
NSMutableString *order = [NSMutableString string];
BOOL(^indexValidationBlock)(NSUInteger) = ^(NSUInteger index) {
[order appendFormat:@"%lu", (unsigned long)index];
BOOL match = index == 0 ? YES : NO;
return match;
};
NSIndexSet *found = [_subject bk_select:indexValidationBlock];
XCTAssertTrue([order isEqualToString:@"123"], @"the index loop order is %@", order);
XCTAssertNil(found,@"no index found");
}
- (void)testReject {
NSMutableString *order = [NSMutableString string];
BOOL(^indexValidationBlock)(NSUInteger) = ^(NSUInteger index) {
[order appendFormat:@"%lu", (unsigned long)index];
BOOL match = [_target[index] isEqual:@"0"] ? YES : NO;
return match;
};
NSIndexSet *found = [_subject bk_reject:indexValidationBlock];
XCTAssertTrue([order isEqualToString:@"123"], @"the index loop order is %@", order);
XCTAssertNil(found,@"all indexes are rejected");
}
- (void)testRejectedNone {
NSMutableString *order = [NSMutableString string];
BOOL(^indexValidationBlock)(NSUInteger) = ^(NSUInteger index) {
[order appendFormat:@"%lu", (unsigned long)index];
BOOL match = [_target[index] isEqual:@"0"] ? NO : YES;
return match;
};
NSIndexSet *found = [_subject bk_reject:indexValidationBlock];
XCTAssertTrue([order isEqualToString:@"123"], @"the index loop order is %@", order);
XCTAssertEqualObjects(found, _subject, @"all indexes that are not rejected %@", found);
}
- (void)testAny {
__block NSMutableString *order = [NSMutableString string];
BOOL(^indexValidationBlock)(NSUInteger) = ^(NSUInteger index) {
[order appendFormat:@"%lu", (unsigned long)index];
BOOL match = NO;
if (index%2 == 0 ) {
_target[index] = [NSString stringWithFormat:@"%lu", (unsigned long)index];
match = YES;
}
return match;
};
BOOL didFind = [_subject bk_any:indexValidationBlock];
XCTAssertTrue([order isEqualToString:@"12"], @"the index loop order is %@", order);
XCTAssertTrue(didFind, @"result found in target array");
}
@end