forked from BlocksKit/BlocksKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSIndexSetBlocksKitTest.m
More file actions
116 lines (101 loc) · 3.83 KB
/
Copy pathNSIndexSetBlocksKitTest.m
File metadata and controls
116 lines (101 loc) · 3.83 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
//
// NSIndexSetBlocksKitTest.m
// BlocksKit Unit Tests
//
#import "NSIndexSetBlocksKitTest.h"
@implementation NSIndexSetBlocksKitTest {
NSIndexSet *_subject;
NSMutableArray *_target;
}
- (void)setUpClass {
_subject = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 3)];
}
- (void)tearDownClass {
[_target release];
}
- (void)setUp {
_target = [[NSMutableArray alloc] initWithObjects:@"0",@"0",@"0",@"0",nil];
}
- (void)testEach {
__block NSMutableString *order = [NSMutableString string];
BKIndexBlock indexBlock = ^(NSUInteger index) {
[order appendFormat:@"%d",index];
[_target replaceObjectAtIndex:index withObject:[NSString stringWithFormat:@"%d",index]];
};
[_subject each:indexBlock];
GHAssertEqualStrings(order,@"123",@"the index loop order is %@",order);
NSArray *target = [NSArray arrayWithObjects:@"0",@"1",@"2",@"3",nil];
GHAssertEqualObjects(_target,target,@"the target array becomes %@",_target);
}
- (void)testMatch {
__block NSMutableString *order = [NSMutableString string];
BKIndexValidationBlock indexValidationBlock = ^(NSUInteger index) {
[order appendFormat:@"%d",index];
BOOL match = NO;
if (index%2 == 0 ) {
[_target replaceObjectAtIndex:index withObject:[NSString stringWithFormat:@"%d",index]];
match = YES;
}
return match;
};
NSUInteger found = [_subject match:indexValidationBlock];
GHAssertEqualStrings(order,@"12",@"the index loop order is %@",order);
GHAssertTrue([[_target objectAtIndex:found] isEqual:@"2"],@"the target array becomes %@",_target);
}
- (void)testNotMatch {
__block NSMutableString *order = [NSMutableString string];
BKIndexValidationBlock indexValidationBlock = ^(NSUInteger index) {
[order appendFormat:@"%d",index];
BOOL match = index > 4 ? YES : NO;
return match;
};
NSUInteger found = [_subject match:indexValidationBlock];
GHAssertEqualStrings(order,@"123",@"the index loop order is %@",order);
GHAssertTrue(found == NSNotFound,@"no items are found");
}
- (void)testSelect {
__block NSMutableString *order = [NSMutableString string];
BKIndexValidationBlock indexValidationBlock = ^(NSUInteger index) {
[order appendFormat:@"%d",index];
BOOL match = index < 3 ? YES : NO; //1,2
return match;
};
NSIndexSet *found = [_subject select:indexValidationBlock];
GHAssertEqualStrings(order,@"123",@"the index loop order is %@",order);
NSIndexSet *target = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1,2)];
GHAssertEqualObjects(found,target,@"the selected index set is %@",found);
}
- (void)testSelectedNone {
__block NSMutableString *order = [NSMutableString string];
BKIndexValidationBlock indexValidationBlock = ^(NSUInteger index) {
[order appendFormat:@"%d",index];
BOOL match = index == 0 ? YES : NO;
return match;
};
NSIndexSet *found = [_subject select:indexValidationBlock];
GHAssertEqualStrings(order,@"123",@"the index loop order is %@",order);
GHAssertNil(found,@"no index found");
}
- (void)testReject {
__block NSMutableString *order = [NSMutableString string];
BKIndexValidationBlock indexValidationBlock = ^(NSUInteger index) {
[order appendFormat:@"%d",index];
BOOL match = [[_target objectAtIndex:index] isEqual: @"0"] ? YES : NO;
return match;
};
NSIndexSet *found = [_subject reject:indexValidationBlock];
GHAssertEqualStrings(order,@"123",@"the index loop order is %@",order);
GHAssertNil(found,@"all indexes are rejected");
}
- (void)testRejectedNone {
__block NSMutableString *order = [NSMutableString string];
BKIndexValidationBlock indexValidationBlock = ^(NSUInteger index) {
[order appendFormat:@"%d",index];
BOOL match = [[_target objectAtIndex:index] isEqual: @"0"] ? NO : YES;
return match;
};
NSIndexSet *found = [_subject reject:indexValidationBlock];
GHAssertEqualStrings(order,@"123",@"the index loop order is %@",order);
GHAssertEqualObjects(found,_subject,@"all indexes that are not rejected %@",found);
}
@end