PageRenderTime 58ms CodeModel.GetById 7ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/NSObjectBlockObservationTest.m

http://github.com/zwaldowski/BlocksKit
Objective C | 141 lines | 108 code | 29 blank | 4 comment | 1 complexity | 090dfc99b09df09adca835ea06f6981e MD5 | raw file
  1//
  2//  NSObjectBlockObservationTest.m
  3//  BlocksKit Unit Tests
  4//
  5
  6#import "NSObjectBlockObservationTest.h"
  7
  8@interface SubjectKVCAndKVO : NSObject
  9
 10@property (nonatomic, getter=isKvc) BOOL kvc; //scalar
 11@property (nonatomic, retain) NSNumber *number;	  //scalar
 12@property (nonatomic, retain) NSObjectBlockObservationTest *test; //to-one
 13@property (nonatomic, retain) NSMutableArray *names; //ordered to-many
 14@property (nonatomic, retain) NSMutableSet *members; //unordered to-many
 15
 16- (void)insertObject:(NSString *)name inNamesAtIndex:(NSUInteger)index;
 17- (void)removeObjectFromNamesAtIndex:(NSUInteger)index;
 18- (void)addMembersObject:(NSString *)member;
 19- (void)removeMembersObject:(NSString *)member;
 20@end
 21
 22@implementation SubjectKVCAndKVO
 23
 24@synthesize kvc=_kvc, test=_test, number=_number, names=_names, members=_members;
 25
 26- (id)initSubjectWithTest:(NSObjectBlockObservationTest *)test {
 27	if ( (self = [super init]) ) {
 28		self.kvc = YES;
 29		self.test = test;
 30		self.number = [NSNumber numberWithInteger:0];
 31		self.names = [NSMutableArray arrayWithObjects:@"one",@"two",nil];
 32		self.members = [NSMutableSet setWithObjects:@"foo",@"bar",nil];
 33	}
 34	return self;
 35}
 36
 37- (void)dealloc {
 38	[_number release];
 39	[_test release];
 40	[_names release];
 41	[_members release];
 42	[super dealloc];
 43}
 44
 45- (void)insertObject:(NSString *)name inNamesAtIndex:(NSUInteger)index {
 46	[_names insertObject:name atIndex:index];
 47}
 48
 49- (void)removeObjectFromNamesAtIndex:(NSUInteger)index {
 50	[_names removeObjectAtIndex:index];
 51}
 52
 53- (void)addMembersObject:(NSString *)member {
 54	[_members addObject:member];
 55}
 56
 57- (void)removeMembersObject:(NSString *)member {
 58	[_members removeObject:member];
 59}
 60
 61@end
 62
 63@implementation NSObjectBlockObservationTest  {
 64	SubjectKVCAndKVO *_subject; 
 65	NSInteger _total;
 66}
 67
 68- (void)setUpClass {
 69	_subject = [[SubjectKVCAndKVO alloc] initSubjectWithTest:self];
 70}
 71
 72- (void)tearDownClass {
 73	[_subject release];
 74}
 75
 76- (void)setUp {
 77	_total = 0;
 78}
 79
 80- (void)action {
 81	_total += 1;
 82}
 83
 84- (void)testBoolKeyValueObservation {
 85	BKObservationBlock observeBlock = ^(id obj, NSDictionary *change) {
 86		[(NSObjectBlockObservationTest *)obj action];
 87	};
 88	NSString *token = [self addObserverForKeyPath:@"subject.kvc" task:observeBlock];
 89
 90	[self setValue:[NSNumber numberWithBool:NO] forKeyPath:@"subject.kvc"];
 91	GHAssertFalse(_subject.kvc,@"kvc is NO");
 92	GHAssertEquals(_total,1,@"total is %d",_total);
 93	[self removeObserverForKeyPath:@"subject.kvc" identifier:token];
 94}
 95
 96- (void)testNSNumberKeyValueObservation {
 97	BKObservationBlock observeBlock = ^(id obj, NSDictionary *change) {
 98		[(NSObjectBlockObservationTest *)obj action];
 99	};
100	NSString *token = [self addObserverForKeyPath:@"subject.number" task:observeBlock];
101
102	NSNumber *number = [NSNumber numberWithInteger:1];
103	[self setValue:number forKeyPath:@"subject.number"];
104	GHAssertEquals(_subject.number,number,@"number is %@",_subject.number);
105	GHAssertEquals(_total,1,@"total is %d",_total);
106	
107	[self removeObserverForKeyPath:@"subject.number" identifier:token];
108}
109
110- (void)testNSArrayKeyValueObservation {
111	BKObservationBlock observeBlock = ^(id obj, NSDictionary *change) {
112		[(NSObjectBlockObservationTest *)obj action];
113	};
114	NSString *token = [self addObserverForKeyPath:@"subject.names" task:observeBlock];
115
116	NSMutableArray *names = [self mutableArrayValueForKeyPath:@"subject.names"];
117	[names replaceObjectAtIndex:0 withObject:@"1"];
118	[names replaceObjectAtIndex:1 withObject:@"2"];
119	NSArray *target = [NSArray arrayWithObjects:@"1",@"2",nil];
120	GHAssertEqualObjects(_subject.names,target,@"names are %@",_subject.names);
121	GHAssertEquals(_total,2,@"total is %d",_total);
122	
123	[self removeObserverForKeyPath:@"subject.names" identifier:token];
124}
125
126- (void)testNSSetKeyValueObservation {
127	BKObservationBlock observeBlock = ^(id obj, NSDictionary *change) {
128		[(NSObjectBlockObservationTest *)obj action];
129	};
130	NSString *token = [self addObserverForKeyPath:@"subject.members" task:observeBlock];
131
132	NSMutableSet *members = [self mutableSetValueForKeyPath:@"subject.members"];
133	[members removeObject:@"bar"];
134	[members addObject:@"one"];
135	NSSet *target = [NSSet setWithObjects:@"foo",@"one",nil];
136	GHAssertEqualObjects(_subject.members,target,@"members are %@",_subject.members);
137	GHAssertEquals(_total,2,@"total is %d",_total);
138	
139	[self removeObserverForKeyPath:@"subject.members" identifier:token];
140} 
141@end