Skip to content

Commit

Permalink
Update tests to test removeObserver:forKeyPath:context method
Browse files Browse the repository at this point in the history
  • Loading branch information
gcasa committed Oct 1, 2023
1 parent 875d966 commit f7a6a8b
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion Tests/base/KVC/mutable.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,43 @@
} aStruct;

@interface Observer : NSObject
{
BOOL _string2DidChange;
}

- (void) reset;

- (BOOL) string2DidChange;

- (void) observeValueForKeyPath: (NSString *)keyPath
ofObject: (id)object
change: (NSDictionary *)change
context: (void *)context;
@end

@implementation Observer
- (void) reset;
{
_string2DidChange = NO;
}

- (BOOL) string2DidChange
{
return _string2DidChange;
}

- (void) observeValueForKeyPath: (NSString *)keyPath
ofObject: (id)object
change: (NSDictionary *)change
context: (void *)context
{
NSLog(@"observeValueForKeyPath: %@\nofObject:%@\nchange:%@\ncontext:%p",
keyPath, object, change, context);

if ([keyPath isEqualToString: @"string2"] )
{
_string2DidChange = YES;
}
}
@end

Expand All @@ -31,6 +54,7 @@ @interface Lists : NSObject
NSMutableArray * numbers;
NSMutableArray * third;
NSString *string;
NSString *string2;
aStruct x;
}

Expand Down Expand Up @@ -115,6 +139,16 @@ - (void) setX: (aStruct)s
x = s;
}

- (void) setString2: (NSString *)s
{
string2 = s;
}

- (NSString *) string2
{
return string2;
}

- (void) willChangeValueForKey: (NSString*)k
{
[super willChangeValueForKey: k];
Expand Down Expand Up @@ -179,11 +213,17 @@ int main(void)
NSMutableArray * proxy;
NSDictionary * temp;

[observer reset];
[list addObserver: observer forKeyPath: @"numbers" options: 15 context: 0];
[list addObserver: observer forKeyPath: @"string" options: 15 context: 0];
[list addObserver: observer forKeyPath: @"string2" options: 15 context: 0];
[list addObserver: observer forKeyPath: @"x" options: 15 context: 0];

[list setValue: @"x" forKey: @"string"];
[list setString2: @"Hello"];

PASS([observer string2DidChange],
"string did change properly");

proxy = [list mutableArrayValueForKey:@"numbers"];
PASS([proxy isKindOfClass:[NSMutableArray class]],
Expand Down Expand Up @@ -277,8 +317,14 @@ int main(void)

[list removeObserver: observer forKeyPath: @"numbers"];
[list removeObserver: observer forKeyPath: @"string"];
[list removeObserver: observer forKeyPath: @"x"];
[list removeObserver: observer forKeyPath: @"x"];
[list removeObserver: observer forKeyPath: @"string2" context: 0];

[observer reset];
[list setString2: @"Test"];
PASS([observer string2DidChange] == NO,
"string2 should NOT have changed");

[arp release]; arp = nil;
return 0;
}

0 comments on commit f7a6a8b

Please sign in to comment.