PageRenderTime 53ms CodeModel.GetById 1ms app.highlight 48ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/NSSetBlocksKitTest.m

http://github.com/zwaldowski/BlocksKit
Objective C | 129 lines | 101 code | 22 blank | 6 comment | 2 complexity | 4923d10bfc268ad24100b1a27b2d5242 MD5 | raw file
  1//
  2//  NSSetBlocksKitTest.m
  3//  BlocksKit Unit Tests
  4//
  5
  6#import "NSSetBlocksKitTest.h"
  7
  8@implementation NSSetBlocksKitTest {
  9	NSSet *_subject;
 10	NSInteger _total;
 11}
 12
 13- (void)setUpClass {
 14	_subject = [[NSSet alloc] initWithObjects:@"1",@"22",@"333",nil];
 15}
 16
 17- (void)tearDownClass {
 18	[_subject release];
 19}
 20
 21- (void)setUp {
 22	_total = 0;
 23}
 24
 25- (void)testEach {
 26	BKSenderBlock senderBlock = ^(id sender) {
 27		_total += [sender length];
 28	};
 29	[_subject each:senderBlock];
 30	GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
 31}
 32
 33- (void)testMatch {
 34	BKValidationBlock validationBlock = ^(id obj) {
 35		_total += [obj length];
 36		BOOL match = ([obj intValue] == 22) ? YES : NO;
 37		return match;
 38	};
 39	id found = [_subject match:validationBlock];
 40
 41	//match: is functionally identical to select:, but will stop and return on the first match
 42	GHAssertEquals(_total,3,@"total length of \"122\" is %d",_total);
 43	GHAssertEquals(found,@"22",@"matched object is %@",found);
 44}
 45
 46- (void)testNotMatch {
 47	BKValidationBlock validationBlock = ^(id obj) {
 48		_total += [obj length];
 49		BOOL match = ([obj intValue] == 4444) ? YES : NO;
 50		return match;
 51	};
 52	id found = [_subject match:validationBlock];
 53
 54	//@return Returns the object if found, `nil` otherwise.
 55	GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
 56	GHAssertNil(found,@"no matched object");
 57}
 58
 59- (void)testSelect {
 60	BKValidationBlock validationBlock = ^(id obj) {
 61		_total += [obj length];
 62		BOOL match = ([obj intValue] < 300) ? YES : NO;
 63		return match;
 64	};
 65	NSSet *found = [_subject select:validationBlock];
 66
 67	GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
 68	NSSet *target = [NSSet setWithObjects:@"1",@"22",nil];
 69	GHAssertEqualObjects(found,target,@"selected items are %@",found);
 70}
 71
 72- (void)testSelectedNone {
 73	BKValidationBlock validationBlock = ^(id obj) {
 74		_total += [obj length];
 75		BOOL match = ([obj intValue] > 400) ? YES : NO;
 76		return match;
 77	};
 78	NSSet *found = [_subject select:validationBlock];
 79
 80	GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
 81	GHAssertNil(found,@"no item is selected");
 82}
 83
 84- (void)testReject {
 85	BKValidationBlock validationBlock = ^(id obj) {
 86		_total += [obj length];
 87		BOOL match = ([obj intValue] > 300) ? YES : NO;
 88		return match;
 89	};
 90	NSSet *left = [_subject reject:validationBlock];
 91
 92	GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
 93	NSSet *target = [NSSet setWithObjects:@"1",@"22",nil];
 94	GHAssertEqualObjects(left,target,@"not rejected items are %@",left);
 95}
 96
 97- (void)testRejectedAll {
 98	BKValidationBlock validationBlock = ^(id obj) {
 99		_total += [obj length];
100		BOOL match = ([obj intValue] < 400) ? YES : NO;
101		return match;
102	};
103	NSSet *left = [_subject reject:validationBlock];
104
105	GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
106	GHAssertNil(left,@"all items are rejected");
107}
108
109- (void)testMap {
110	BKTransformBlock transformBlock = ^id(id obj) {
111		_total += [obj length];
112		return [obj substringToIndex:1];
113	};
114	NSSet *transformed = [_subject map:transformBlock];
115
116	GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
117	NSSet *target = [NSSet setWithObjects:@"1",@"2",@"3",nil];
118	GHAssertEqualObjects(transformed,target,@"transformed items are %@",transformed);
119}
120
121- (void)testReduceWithBlock {
122	BKAccumulationBlock accumlationBlock = ^id(id sum,id obj) {
123		return [sum stringByAppendingString:obj];
124	};
125	NSString *concatenated = [_subject reduce:@"" withBlock:accumlationBlock];
126	GHAssertEqualStrings(concatenated,@"122333",@"concatenated string is %@",concatenated);
127}
128
129@end