libs-base/Tests/base/NSKVOSupport/basic.m

121 lines
2.2 KiB
Mathematica
Raw Normal View History

Initial port of WinObjC's KVO implementation to GNUstep (#420) * GSAtomic: Add prefix to macro definitions * NSKVOSupport: Import * NSKVOSupport: Add test cases * NSKVOSwizzling: Ugly C Rewrite * NSKeyValueObserving: Use old implementation as fallback * NSKeyValueObserving: Rename TypeEncodingCases header * NSKVOSupport: Fix new objects not being added to NSKeyValueChangeNew set on set mutation * NSKeyValueMutableSet: Fix will and didChange notifications for set operations * NSKeyValueMutableSet: Document Accessor Search Patterns * NSKVOSupport: Add toMany test * NSKeyValueCoding: Change notifications when changing value via setValue:forKey: * NSKVOSupport: Add more tests * NSKVOSupport: Do not wrap block in try/finally to avoid crash in windows * NSKVOSwizzling: use _alloca on Windows * NSKVOSupport: Do not autorelease newWithObservee: * NSKVOSupport: Do not leak Observee and TestFacade objects * Improve runtime detection in makefile * Add file extension of source file in GNUMakefile * NSKVOSupport: Remove @status comments * NSKVOSupport: Implement private notify method * NSUserDefaults: KVO Support and fix macOS incompatibilities * NSKeyValueObserving: Set old to null if nil * NSKeyValueObserving: Remove cached new value * NSMethodSignature: Add signature cache * NSKVOSupport: Remove ObjC2 features and mark tests failing on GCC as hopeful * Call class method instead of private _keyPathsForValuesAffectingValueForKey * Move _keyPathsForValuesAffectingValueForKey body into class method and statically construct empty NSSet * NSUserDefaults: Change notification should contain old value from other domains aswell * NSUserDefaults: Fetch new value from all domains * NSKVOInternal: Fixup filename in header * NSUserDefaults: Go through search list instead of only one domain in KVO change * Making indentation a bit less worse * Add NSUserDefaults KVO tests * NSKVOSupport: NSUserDefaults test small fixes * Add autoreleasepool * NSUserDefaults: Only emit change notifications if value changed * Avoid compiler warnings and tidy some of the whitespace/formatting --------- Co-authored-by: Frederik Seiffert <frederik@algoriddim.com> Co-authored-by: rfm <richardfrithmacdonald@gmail.com> Co-authored-by: rfm <rfm@gnu.org>
2024-11-10 16:05:23 +00:00
#import <Foundation/Foundation.h>
#import "ObjectTesting.h"
@interface Foo : NSObject
{
@public
BOOL a;
NSInteger b;
NSString *c;
NSArray *d;
}
- (void) setA: (BOOL)v;
- (void) setB: (NSInteger)v;
- (void) setC: (NSString *)v;
@end
@implementation Foo
- (void) setA: (BOOL)v
{
a = v;
}
- (void) setB: (NSInteger)v
{
b = v;
}
- (void) setC: (NSString *)v
{
c = v;
}
@end
@interface Observer : NSObject
{
Foo *object;
NSString *expectedKeyPath;
NSInteger receivedCalls;
}
- (NSString*) expectedKeyPath;
- (void) setExpectedKeyPath: (NSString*)s;
- (NSInteger) receivedCalls;
- (void) setReceivedCalls: (NSInteger)i;
@end
@implementation Observer
- (id)init
{
self = [super init];
if (self)
{
receivedCalls = 0;
}
return self;
}
static char observerContext;
- (void)startObserving:(Foo *)target
{
object = target;
[target addObserver:self forKeyPath:@"a" options:0 context:&observerContext];
[target addObserver:self forKeyPath:@"b" options:0 context:&observerContext];
[target addObserver:self forKeyPath:@"c" options:0 context:&observerContext];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)o
change:(NSDictionary *)change
context:(void *)context
{
PASS(context == &observerContext, "context");
PASS(o == self->object, "object");
PASS([keyPath isEqualToString: [self expectedKeyPath]], "key path");
[self setReceivedCalls: [self receivedCalls] + 1];
}
- (NSString*) expectedKeyPath
{
return expectedKeyPath;
}
- (void) setExpectedKeyPath: (NSString*)s
{
expectedKeyPath = s;
}
- (NSInteger) receivedCalls
{
return receivedCalls;
}
- (void) setReceivedCalls: (NSInteger)i
{
receivedCalls = i;
}
@end
int
main(int argc, char *argv[])
{
[NSAutoreleasePool new];
Foo *foo = [Foo new];
Observer *obs = [Observer new];
[obs startObserving: foo];
[obs setExpectedKeyPath: @"a"];
[foo setA: YES];
PASS([obs receivedCalls] == 1, "received calls")
[obs setExpectedKeyPath: @"b"];
[foo setB: 1];
PASS([obs receivedCalls] == 2, "received calls")
[obs setExpectedKeyPath: @"c"];
[foo setC: @"henlo"];
PASS([obs receivedCalls] == 3, "received calls")
return 0;
}