Skip to content

Commit

Permalink
NSKVOSupport: NSUserDefaults test small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hmelder committed Oct 25, 2024
1 parent ba0ec1d commit 41e9c98
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions Tests/base/NSKVOSupport/userdefaults.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSNull.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSKeyValueObserving.h>
Expand Down Expand Up @@ -29,8 +30,6 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
change:(NSDictionary *)change
context:(void *)context
{
NSLog(@"KVO notification: keyPath: %@ ofObject: %@ change: %@ context: %p",
keyPath, object, change, context);
called++;
ASSIGN(lastKeyPath, keyPath);
ASSIGN(lastObject, object);
Expand Down Expand Up @@ -96,8 +95,8 @@ - (void)dealloc
PASS(obs->lastChange != nil, "change is not nil");
PASS_EQUAL([obs->lastChange objectForKey:@"kind"],
[NSNumber numberWithInteger:1], "value for 'kind' is 1");
PASS_EQUAL([obs->lastChange objectForKey:@"old"], nil,
"value for 'old' is nil");
PASS_EQUAL([obs->lastChange objectForKey:@"old"], [NSNull null],
"value for 'old' is [NSNull null]");
PASS_EQUAL([obs->lastChange objectForKey:@"new"], value1,
"value for 'new' is 'value1'");

Expand All @@ -109,8 +108,8 @@ - (void)dealloc
[NSNumber numberWithInteger:1], "value for 'kind' is 1");
PASS_EQUAL([obs->lastChange objectForKey:@"old"], value1,
"value for 'old' is value1");
PASS_EQUAL([obs->lastChange objectForKey:@"new"], nil,
"value for 'new' is nil");
PASS_EQUAL([obs->lastChange objectForKey:@"new"], [NSNull null],
"value for 'new' is [NSNull null]");

// Test setting two different values for the same key in application domain
// and registration domain. When removing the value in the application domain,
Expand All @@ -122,8 +121,8 @@ - (void)dealloc
PASS(obs->lastChange != nil, "change is not nil");
PASS_EQUAL([obs->lastChange objectForKey:@"kind"],
[NSNumber numberWithInteger:1], "value for 'kind' is 1");
PASS_EQUAL([obs->lastChange objectForKey:@"old"], nil,
"value for 'old' is nil");
PASS_EQUAL([obs->lastChange objectForKey:@"old"], [NSNull null],
"value for 'old' is [NSNull null]");
PASS_EQUAL([obs->lastChange objectForKey:@"new"], value2,
"value for 'new' is 'value2'");

Expand All @@ -132,7 +131,6 @@ - (void)dealloc
// notification, when the entry is removed from the application domain.
NSDictionary *registrationDict = [NSDictionary dictionaryWithObject:value2Alt
forKey:key2];
// -registerDefaults: does not emit a KVO notification
[defs registerDefaults:registrationDict];

[defs removeObjectForKey:key2];
Expand All @@ -142,7 +140,7 @@ - (void)dealloc
PASS_EQUAL([obs->lastChange objectForKey:@"kind"],
[NSNumber numberWithInteger:1], "value for 'kind' is 1");
PASS_EQUAL([obs->lastChange objectForKey:@"old"], value2,
"value for 'old' is nil");
"value for 'old' is value2");
// this must not be null in this case
PASS_EQUAL([obs->lastChange objectForKey:@"new"], value2Alt,
"value for 'new' is 'value2Alt'");
Expand All @@ -153,21 +151,15 @@ - (void)dealloc
registrationDict = [NSDictionary dictionaryWithObject:value1 forKey:key1];
[defs registerDefaults:registrationDict];

[defs setObject:value1 forKey:key1];
PASS(obs->called == 5, "KVO notification received");
PASS(obs->lastObject != nil, "object is not nil");
PASS(obs->lastChange != nil, "change is not nil");
PASS_EQUAL([obs->lastChange objectForKey:@"kind"],
[NSNumber numberWithInteger:1], "value for 'kind' is 1");
PASS_EQUAL([obs->lastChange objectForKey:@"old"], nil,
"value for 'old' is nil");
PASS_EQUAL([obs->lastChange objectForKey:@"new"], value1,
"value for 'new' is 'value1'");
// Does not emit a KVO notification as value is not changed
[defs setObject:value1 forKey:key1];
PASS(obs->called == 4,
"KVO notification was not emitted as other domain has the same entry");

// Remove the entry from the application domain.
[defs removeObjectForKey:key1];
PASS(obs->called == 5,
"KVO notification was not emitted when other domain has the same entry");
PASS(obs->called == 4,
"KVO notification was not emitted as other domain has the same entry");

[defs removeObserver:obs forKeyPath:key1];
[defs removeObserver:obs forKeyPath:key2];
Expand Down

0 comments on commit 41e9c98

Please sign in to comment.