PageRenderTime 44ms CodeModel.GetById 10ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

/Tests/NSDictionaryBlocksKitTest.m

http://github.com/zwaldowski/BlocksKit
Objective C | 108 lines | 90 code | 14 blank | 4 comment | 0 complexity | e568bcedb0bc23e93c129aee9e8b23a2 MD5 | raw file
  1//
  2//  NSDictionaryBlocksKitTest.m
  3//  BlocksKit Unit Tests
  4//
  5
  6#import "NSDictionaryBlocksKitTest.h"
  7
  8
  9@implementation NSDictionaryBlocksKitTest {
 10	NSDictionary *_subject;
 11	NSInteger _total;
 12}
 13
 14- (void)setUpClass {
 15	_subject = [[NSDictionary alloc] initWithObjectsAndKeys:
 16		[NSNumber numberWithInteger:1],@"1",
 17		[NSNumber numberWithInteger:2],@"2",
 18		[NSNumber numberWithInteger:3],@"3",
 19		nil];
 20}
 21
 22- (void)tearDownClass {
 23	[_subject release];
 24}
 25
 26- (void)setUp {
 27  _total = 0;
 28} 
 29
 30- (void)testEach {
 31	BKKeyValueBlock keyValueBlock = ^(id key,id value) {
 32		_total += [value intValue] + [key intValue];
 33	};
 34
 35	[_subject each:keyValueBlock];
 36	GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
 37}
 38
 39- (void)testSelect {
 40	BKKeyValueValidationBlock validationBlock = ^(id key,id value) {
 41		_total += [value intValue] + [key intValue];
 42		BOOL select = [value intValue] < 3 ? YES : NO;
 43		return select;
 44	};
 45	NSDictionary *selected = [_subject select:validationBlock];
 46	GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
 47	NSDictionary *target = [NSDictionary dictionaryWithObjectsAndKeys:
 48		[NSNumber numberWithInteger:1],@"1",
 49		[NSNumber numberWithInteger:2],@"2",
 50		nil
 51	];
 52	GHAssertEqualObjects(selected,target,@"selected dictionary is %@",selected);
 53}
 54
 55- (void)testSelectedNone {
 56	BKKeyValueValidationBlock validationBlock = ^(id key,id value) {
 57		_total += [value intValue] + [key intValue];
 58		BOOL select = [value intValue] > 4 ? YES : NO;
 59		return select;
 60	};
 61	NSDictionary *selected = [_subject select:validationBlock];
 62	GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
 63	GHAssertNil(selected,@"none item is selected");
 64}
 65
 66- (void)testReject {
 67	BKKeyValueValidationBlock validationBlock = ^(id key,id value) {
 68		_total += [value intValue] + [key intValue];
 69		BOOL reject = [value intValue] < 3 ? YES : NO;
 70		return reject;
 71	};
 72	NSDictionary *rejected = [_subject reject:validationBlock];
 73	GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
 74	NSDictionary *target = [NSDictionary dictionaryWithObjectsAndKeys:
 75		[NSNumber numberWithInteger:3],@"3",
 76		nil
 77	];
 78	GHAssertEqualObjects(rejected,target,@"dictionary after rejection is %@",rejected);
 79}
 80
 81- (void)testRejectedAll {
 82	BKKeyValueValidationBlock validationBlock = ^(id key,id value) {
 83		_total += [value intValue] + [key intValue];
 84		BOOL reject = [value intValue] < 4 ? YES : NO;
 85		return reject;
 86	};
 87	NSDictionary *rejected = [_subject reject:validationBlock];
 88	GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
 89	GHAssertNil(rejected,@"all items are selected");
 90}
 91
 92- (void)testMap {
 93	BKKeyValueTransformBlock transformBlock = ^id(id key,id value) {
 94		_total += [value intValue] + [key intValue];
 95		return [NSNumber numberWithInteger:_total];
 96	};
 97	NSDictionary *transformed = [_subject map:transformBlock];
 98	GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
 99	NSDictionary *target = [NSDictionary dictionaryWithObjectsAndKeys:
100		[NSNumber numberWithInteger:2],@"1",
101		[NSNumber numberWithInteger:6],@"2",
102		[NSNumber numberWithInteger:12],@"3",
103		nil
104	];
105	GHAssertEqualObjects(transformed,target,@"transformed dictionary is %@",transformed);
106}
107
108@end