More experiments with NSProxy

This commit is contained in:
michael starke
2016-03-14 09:40:25 +01:00
parent ac7a0078f0
commit 9d1efb4ef3
2 changed files with 26 additions and 3 deletions

View File

@@ -12,6 +12,7 @@
@interface MPEntryProxy ()
@property (strong) KPKEntry *entry;
@property (strong) NSMutableDictionary *valueStore;
@end
@@ -22,12 +23,34 @@
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:nil userInfo:nil];
}
_entry = entry;
_valueStore = [[NSMutableDictionary alloc] init];
return self;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
NSLog(@"forwardInvocation: %@", NSStringFromSelector(invocation.selector));
[invocation invokeWithTarget:self.entry];
NSString *seletor = NSStringFromSelector(invocation.selector);
if([seletor hasPrefix:@"set"]) {
NSLog(@"forwardInvocation: setter detected");
NSString *property = [seletor substringFromIndex:3].lowercaseString; // lowercase fist letter!!!
if(invocation.methodSignature.numberOfArguments == 3) {
id value;
[invocation getArgument:&value atIndex:2];
NSLog(@"forwardInvocation: captured value %@", value);
if(value) {
self.valueStore[property] = value;
}
return; // captures getter, just return
}
}
id change = self.valueStore[seletor.lowercaseString];
if(change) {
NSLog(@"forwardInvocation: hit cached value. Returning cache!");
[invocation setReturnValue:&change];
}
else {
[invocation invokeWithTarget:self.entry];
}
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {

View File

@@ -41,8 +41,8 @@
NSString *newPassword = @"new password";
NSString *newKeystrokes = @"{ENTER 3}";
[((id)self.proxy) setPassword:newPassword];
XCTAssertEqualObjects(self.entry.password, newPassword, @"Proxy sets password on entry!");
XCTAssertNotEqualObjects(self.entry.password, newPassword, @"Proxy does not set password on entry!");
[((id)self.proxy) autotype].defaultKeystrokeSequence= newKeystrokes;
XCTAssertEqualObjects(self.entry.autotype.defaultKeystrokeSequence, newKeystrokes, @"Proxy sets default keystroke sequence on entry autotype!");
}