diff --git a/calabash/Classes/UISpec/UIScriptASTWith.m b/calabash/Classes/UISpec/UIScriptASTWith.m index 18bd768b6..af74f1303 100644 --- a/calabash/Classes/UISpec/UIScriptASTWith.m +++ b/calabash/Classes/UISpec/UIScriptASTWith.m @@ -163,21 +163,57 @@ - (NSMutableArray *) evalWith:(NSArray *) views direction:(UIScriptASTDirectionT if (self.selectorName) { if ([v respondsToSelector:_selector]) { - void *val = [v performSelector:_selector]; + BOOL Bvalue; + NSMethodSignature *sig = [v methodSignatureForSelector:_selector]; + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig]; + const char *type = [[invocation methodSignature] methodReturnType]; + NSString *returnType = [NSString stringWithFormat:@"%s", type]; + const char *trimmedType = [[returnType substringToIndex:1] + cStringUsingEncoding:NSASCIIStringEncoding]; + BOOL boolReturnType = ('B' == *trimmedType); + BOOL objectReturnType = ('*' == *trimmedType); + + void *val = nil; + if (boolReturnType) { + [invocation setSelector:_selector]; + [invocation setTarget:v]; + @try { + [invocation invoke]; + } + @catch (NSException *exception) { + NSLog(@"Perform %@ with target %@ caught %@: %@", _selectorName,v, [exception name], [exception reason]); + break; + } + [invocation getReturnValue:(void **) &Bvalue]; + } + else { + val = [v performSelector:_selector]; + } switch (self.valueType) { - case UIScriptLiteralTypeInteger: - if ((NSInteger) val == self.integerValue) { + case UIScriptLiteralTypeInteger: { + if (boolReturnType) { + if ((self.integerValue == 1 && Bvalue) || (self.integerValue == 0 && !Bvalue)) { + [res addObject:v]; + } + } + else if ((NSInteger) val == self.integerValue) { [res addObject:v]; } break; + } case UIScriptLiteralTypeString: { - if (val != nil && ([(NSString *) val isEqualToString:(NSString *) self.objectValue])) { - [res addObject:v]; + if (objectReturnType) { + id valObj = (id) val; + if (valObj != nil + && [valObj respondsToSelector:@selector(isEqualToString:)] + && ([valObj isEqualToString:(NSString *) self.objectValue])) { + [res addObject:v]; + } } break; } case UIScriptLiteralTypeBool: - if (self.boolValue == (BOOL) val) { + if (boolReturnType && self.boolValue == Bvalue) { [res addObject:v]; } break;