Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Unified Diff: third_party/ocmock/OCMock/NSInvocation+OCMAdditions.m

Issue 2410583002: Test update OCMock (Closed)
Patch Set: Patch in exactly 3.1.5 Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/ocmock/OCMock/NSInvocation+OCMAdditions.m
diff --git a/third_party/ocmock/OCMock/NSInvocation+OCMAdditions.m b/third_party/ocmock/OCMock/NSInvocation+OCMAdditions.m
index 25d51885bdddc40780f9c6d570befb68a5f9510c..f16e475572d8859ca1ed4da541d0a0db5f0fd0ea 100644
--- a/third_party/ocmock/OCMock/NSInvocation+OCMAdditions.m
+++ b/third_party/ocmock/OCMock/NSInvocation+OCMAdditions.m
@@ -1,135 +1,151 @@
-//---------------------------------------------------------------------------------------
-// $Id$
-// Copyright (c) 2006-2009 by Mulle Kybernetik. See License file for details.
-//---------------------------------------------------------------------------------------
+/*
+ * Copyright (c) 2006-2015 Erik Doernenburg and contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use these files except in compliance with the License. You may obtain
+ * a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
#import "NSInvocation+OCMAdditions.h"
-
+#import "OCMFunctions.h"
@implementation NSInvocation(OCMAdditions)
-- (id)getArgumentAtIndexAsObject:(int)argIndex
-{
- const char* argType;
-
- argType = [[self methodSignature] getArgumentTypeAtIndex:argIndex];
- while(strchr("rnNoORV", argType[0]) != NULL)
- argType += 1;
-
- if((strlen(argType) > 1) && (strchr("{^", argType[0]) == NULL) && (strcmp("@?", argType) != 0))
- [NSException raise:NSInvalidArgumentException format:@"Cannot handle argument type '%s'.", argType];
-
- switch (argType[0])
- {
- case '#':
- case '@':
- {
- id value;
- [self getArgument:&value atIndex:argIndex];
- return value;
- }
- case ':':
- {
- SEL s = (SEL)0;
- [self getArgument:&s atIndex:argIndex];
- id value = NSStringFromSelector(s);
- return value;
- }
- case 'i':
- {
- int value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithInt:value];
- }
- case 's':
- {
- short value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithShort:value];
- }
- case 'l':
- {
- long value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithLong:value];
- }
- case 'q':
- {
- long long value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithLongLong:value];
- }
- case 'c':
- {
- char value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithChar:value];
- }
- case 'C':
- {
- unsigned char value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithUnsignedChar:value];
- }
- case 'I':
- {
- unsigned int value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithUnsignedInt:value];
- }
- case 'S':
- {
- unsigned short value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithUnsignedShort:value];
- }
- case 'L':
- {
- unsigned long value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithUnsignedLong:value];
- }
- case 'Q':
- {
- unsigned long long value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithUnsignedLongLong:value];
- }
- case 'f':
- {
- float value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithFloat:value];
- }
- case 'd':
- {
- double value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithDouble:value];
- }
- case 'B':
- {
- bool value;
- [self getArgument:&value atIndex:argIndex];
- return [NSNumber numberWithBool:value];
- }
- case '^':
- {
- void *value = NULL;
- [self getArgument:&value atIndex:argIndex];
- return [NSValue valueWithPointer:value];
- }
- case '{': // structure
- {
- NSUInteger maxArgSize = [[self methodSignature] frameLength];
- NSMutableData *argumentData = [[[NSMutableData alloc] initWithLength:maxArgSize] autorelease];
- [self getArgument:[argumentData mutableBytes] atIndex:argIndex];
- return [NSValue valueWithBytes:[argumentData bytes] objCType:argType];
- }
-
- }
- [NSException raise:NSInvalidArgumentException format:@"Argument type '%s' not supported", argType];
- return nil;
+- (BOOL)hasCharPointerArgument {
+ NSMethodSignature* signature = [self methodSignature];
+ for (NSUInteger i = 0; i < [signature numberOfArguments]; i++) {
+ const char* argType =
+ OCMTypeWithoutQualifiers([signature getArgumentTypeAtIndex:i]);
+ if (strcmp(argType, "*") == 0)
+ return YES;
+ }
+ return NO;
+}
+
+- (id)getArgumentAtIndexAsObject:(NSInteger)argIndex {
+ const char* argType = OCMTypeWithoutQualifiers(
+ [[self methodSignature] getArgumentTypeAtIndex:(NSUInteger)argIndex]);
+
+ if ((strlen(argType) > 1) && (strchr("{^", argType[0]) == NULL) &&
+ (strcmp("@?", argType) != 0))
+ [NSException raise:NSInvalidArgumentException
+ format:@"Cannot handle argument type '%s'.", argType];
+
+ if (OCMIsObjectType(argType)) {
+ id value;
+ [self getArgument:&value atIndex:argIndex];
+ return value;
+ }
+
+ switch (argType[0]) {
+ case ':': {
+ SEL s = (SEL)0;
+ [self getArgument:&s atIndex:argIndex];
+ return [NSValue valueWithBytes:&s objCType:":"];
+ }
+ case 'i': {
+ int value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 's': {
+ short value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'l': {
+ long value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'q': {
+ long long value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'c': {
+ char value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'C': {
+ unsigned char value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'I': {
+ unsigned int value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'S': {
+ unsigned short value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'L': {
+ unsigned long value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'Q': {
+ unsigned long long value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'f': {
+ float value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'd': {
+ double value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case 'D': {
+ long double value;
+ [self getArgument:&value atIndex:argIndex];
+ return
+ [NSValue valueWithBytes:&value objCType:@encode(__typeof__(value))];
+ }
+ case 'B': {
+ bool value;
+ [self getArgument:&value atIndex:argIndex];
+ return @(value);
+ }
+ case '^':
+ case '*': {
+ void* value = NULL;
+ [self getArgument:&value atIndex:argIndex];
+ return [NSValue valueWithPointer:value];
+ }
+ case '{': // structure
+ {
+ NSUInteger argSize;
+ NSGetSizeAndAlignment(
+ [[self methodSignature] getArgumentTypeAtIndex:(NSUInteger)argIndex],
+ &argSize, NULL);
+ if (argSize == 0) // TODO: Can this happen? Is frameLength a good choice
+ // in that case?
+ argSize = [[self methodSignature] frameLength];
+ NSMutableData* argumentData =
+ [[[NSMutableData alloc] initWithLength:argSize] autorelease];
+ [self getArgument:[argumentData mutableBytes] atIndex:argIndex];
+ return [NSValue valueWithBytes:[argumentData bytes] objCType:argType];
+ }
+ }
+ [NSException raise:NSInvalidArgumentException
+ format:@"Argument type '%s' not supported", argType];
+ return nil;
}
- (NSString *)invocationDescription
@@ -142,205 +158,213 @@
NSArray *selectorParts = [NSStringFromSelector([self selector]) componentsSeparatedByString:@":"];
NSMutableString *description = [[NSMutableString alloc] init];
- unsigned int i;
- for(i = 2; i < numberOfArgs; i++)
- {
- [description appendFormat:@"%@%@:", (i > 2 ? @" " : @""), [selectorParts objectAtIndex:(i - 2)]];
- [description appendString:[self argumentDescriptionAtIndex:i]];
- }
-
- return [description autorelease];
+ NSUInteger i;
+ for (i = 2; i < numberOfArgs; i++) {
+ [description appendFormat:@"%@%@:", (i > 2 ? @" " : @""),
+ [selectorParts objectAtIndex:(i - 2)]];
+ [description
+ appendString:[self argumentDescriptionAtIndex:(NSInteger)i]];
+ }
+
+ return [description autorelease];
}
-- (NSString *)argumentDescriptionAtIndex:(int)argIndex
-{
- const char *argType = [[self methodSignature] getArgumentTypeAtIndex:argIndex];
- if(strchr("rnNoORV", argType[0]) != NULL)
- argType += 1;
-
- switch(*argType)
- {
- case '@': return [self objectDescriptionAtIndex:argIndex];
- case 'B': return [self boolDescriptionAtIndex:argIndex];
- case 'c': return [self charDescriptionAtIndex:argIndex];
- case 'C': return [self unsignedCharDescriptionAtIndex:argIndex];
- case 'i': return [self intDescriptionAtIndex:argIndex];
- case 'I': return [self unsignedIntDescriptionAtIndex:argIndex];
- case 's': return [self shortDescriptionAtIndex:argIndex];
- case 'S': return [self unsignedShortDescriptionAtIndex:argIndex];
- case 'l': return [self longDescriptionAtIndex:argIndex];
- case 'L': return [self unsignedLongDescriptionAtIndex:argIndex];
- case 'q': return [self longLongDescriptionAtIndex:argIndex];
- case 'Q': return [self unsignedLongLongDescriptionAtIndex:argIndex];
- case 'd': return [self doubleDescriptionAtIndex:argIndex];
- case 'f': return [self floatDescriptionAtIndex:argIndex];
- // Why does this throw EXC_BAD_ACCESS when appending the string?
- // case NSObjCStructType: return [self structDescriptionAtIndex:index];
- case '^': return [self pointerDescriptionAtIndex:argIndex];
- case '*': return [self cStringDescriptionAtIndex:argIndex];
- case ':': return [self selectorDescriptionAtIndex:argIndex];
- default: return [@"<??" stringByAppendingString:@">"]; // avoid confusion with trigraphs...
- }
-
+- (NSString*)argumentDescriptionAtIndex:(NSInteger)argIndex {
+ const char* argType = OCMTypeWithoutQualifiers(
+ [[self methodSignature] getArgumentTypeAtIndex:(NSUInteger)argIndex]);
+
+ switch (*argType) {
+ case '@':
+ return [self objectDescriptionAtIndex:argIndex];
+ case 'B':
+ return [self boolDescriptionAtIndex:argIndex];
+ case 'c':
+ return [self charDescriptionAtIndex:argIndex];
+ case 'C':
+ return [self unsignedCharDescriptionAtIndex:argIndex];
+ case 'i':
+ return [self intDescriptionAtIndex:argIndex];
+ case 'I':
+ return [self unsignedIntDescriptionAtIndex:argIndex];
+ case 's':
+ return [self shortDescriptionAtIndex:argIndex];
+ case 'S':
+ return [self unsignedShortDescriptionAtIndex:argIndex];
+ case 'l':
+ return [self longDescriptionAtIndex:argIndex];
+ case 'L':
+ return [self unsignedLongDescriptionAtIndex:argIndex];
+ case 'q':
+ return [self longLongDescriptionAtIndex:argIndex];
+ case 'Q':
+ return [self unsignedLongLongDescriptionAtIndex:argIndex];
+ case 'd':
+ return [self doubleDescriptionAtIndex:argIndex];
+ case 'f':
+ return [self floatDescriptionAtIndex:argIndex];
+ case 'D':
+ return [self longDoubleDescriptionAtIndex:argIndex];
+ case '{':
+ return [self structDescriptionAtIndex:argIndex];
+ case '^':
+ return [self pointerDescriptionAtIndex:argIndex];
+ case '*':
+ return [self cStringDescriptionAtIndex:argIndex];
+ case ':':
+ return [self selectorDescriptionAtIndex:argIndex];
+ default:
+ return [@"<??"
+ stringByAppendingString:@">"]; // avoid confusion with trigraphs...
+ }
}
+- (NSString*)objectDescriptionAtIndex:(NSInteger)anInt {
+ id object;
-- (NSString *)objectDescriptionAtIndex:(int)anInt
-{
- id object;
-
- [self getArgument:&object atIndex:anInt];
- if (object == nil)
- return @"nil";
- else if(![object isProxy] && [object isKindOfClass:[NSString class]])
- return [NSString stringWithFormat:@"@\"%@\"", [object description]];
- else
- return [object description];
+ [self getArgument:&object atIndex:anInt];
+ if (object == nil)
+ return @"nil";
+ else if (![object isProxy] && [object isKindOfClass:[NSString class]])
+ return [NSString stringWithFormat:@"@\"%@\"", [object description]];
+ else
+ // The description cannot be nil, if it is then replace it
+ return [object description] ?: @"<nil description>";
}
-- (NSString *)boolDescriptionAtIndex:(int)anInt
-{
- bool value;
+- (NSString*)boolDescriptionAtIndex:(NSInteger)anInt {
+ bool value;
+ [self getArgument:&value atIndex:anInt];
+ return value ? @"YES" : @"NO";
+}
+
+- (NSString*)charDescriptionAtIndex:(NSInteger)anInt {
+ unsigned char buffer[128];
+ memset(buffer, 0x0, 128);
+
+ [self getArgument:&buffer atIndex:anInt];
- [self getArgument:&value atIndex:anInt];
- return value ? @"YES" : @"NO";
+ // If there's only one character in the buffer, and it's 0 or 1, then we have
+ // a BOOL
+ if (buffer[1] == '\0' && (buffer[0] == 0 || buffer[0] == 1))
+ return (buffer[0] == 1 ? @"YES" : @"NO");
+ else
+ return [NSString stringWithFormat:@"'%c'", *buffer];
}
-- (NSString *)charDescriptionAtIndex:(int)anInt
-{
- unsigned char buffer[128];
- memset(buffer, 0x0, 128);
-
- [self getArgument:&buffer atIndex:anInt];
-
- // If there's only one character in the buffer, and it's 0 or 1, then we have a BOOL
- if (buffer[1] == '\0' && (buffer[0] == 0 || buffer[0] == 1))
- return [NSString stringWithFormat:@"%@", (buffer[0] == 1 ? @"YES" : @"NO")];
- else
- return [NSString stringWithFormat:@"'%c'", *buffer];
+- (NSString*)unsignedCharDescriptionAtIndex:(NSInteger)anInt {
+ unsigned char buffer[128];
+ memset(buffer, 0x0, 128);
+
+ [self getArgument:&buffer atIndex:anInt];
+ return [NSString stringWithFormat:@"'%c'", *buffer];
}
-- (NSString *)unsignedCharDescriptionAtIndex:(int)anInt
-{
- unsigned char buffer[128];
- memset(buffer, 0x0, 128);
-
- [self getArgument:&buffer atIndex:anInt];
- return [NSString stringWithFormat:@"'%c'", *buffer];
+- (NSString*)intDescriptionAtIndex:(NSInteger)anInt {
+ int intValue;
+
+ [self getArgument:&intValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%d", intValue];
}
-- (NSString *)intDescriptionAtIndex:(int)anInt
-{
- int intValue;
-
- [self getArgument:&intValue atIndex:anInt];
- return [NSString stringWithFormat:@"%d", intValue];
+- (NSString*)unsignedIntDescriptionAtIndex:(NSInteger)anInt {
+ unsigned int intValue;
+
+ [self getArgument:&intValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%d", intValue];
}
-- (NSString *)unsignedIntDescriptionAtIndex:(int)anInt
-{
- unsigned int intValue;
-
- [self getArgument:&intValue atIndex:anInt];
- return [NSString stringWithFormat:@"%d", intValue];
+- (NSString*)shortDescriptionAtIndex:(NSInteger)anInt {
+ short shortValue;
+
+ [self getArgument:&shortValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%hi", shortValue];
}
-- (NSString *)shortDescriptionAtIndex:(int)anInt
-{
- short shortValue;
-
- [self getArgument:&shortValue atIndex:anInt];
- return [NSString stringWithFormat:@"%hi", shortValue];
+- (NSString*)unsignedShortDescriptionAtIndex:(NSInteger)anInt {
+ unsigned short shortValue;
+
+ [self getArgument:&shortValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%hu", shortValue];
}
-- (NSString *)unsignedShortDescriptionAtIndex:(int)anInt
-{
- unsigned short shortValue;
-
- [self getArgument:&shortValue atIndex:anInt];
- return [NSString stringWithFormat:@"%hu", shortValue];
+- (NSString*)longDescriptionAtIndex:(NSInteger)anInt {
+ long longValue;
+
+ [self getArgument:&longValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%ld", longValue];
}
-- (NSString *)longDescriptionAtIndex:(int)anInt
-{
- long longValue;
-
- [self getArgument:&longValue atIndex:anInt];
- return [NSString stringWithFormat:@"%ld", longValue];
+- (NSString*)unsignedLongDescriptionAtIndex:(NSInteger)anInt {
+ unsigned long longValue;
+
+ [self getArgument:&longValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%lu", longValue];
}
-- (NSString *)unsignedLongDescriptionAtIndex:(int)anInt
-{
- unsigned long longValue;
-
- [self getArgument:&longValue atIndex:anInt];
- return [NSString stringWithFormat:@"%lu", longValue];
+- (NSString*)longLongDescriptionAtIndex:(NSInteger)anInt {
+ long long longLongValue;
+
+ [self getArgument:&longLongValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%qi", longLongValue];
}
-- (NSString *)longLongDescriptionAtIndex:(int)anInt
-{
- long long longLongValue;
-
- [self getArgument:&longLongValue atIndex:anInt];
- return [NSString stringWithFormat:@"%qi", longLongValue];
+- (NSString*)unsignedLongLongDescriptionAtIndex:(NSInteger)anInt {
+ unsigned long long longLongValue;
+
+ [self getArgument:&longLongValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%qu", longLongValue];
}
-- (NSString *)unsignedLongLongDescriptionAtIndex:(int)anInt
-{
- unsigned long long longLongValue;
-
- [self getArgument:&longLongValue atIndex:anInt];
- return [NSString stringWithFormat:@"%qu", longLongValue];
+- (NSString*)doubleDescriptionAtIndex:(NSInteger)anInt {
+ double doubleValue;
+
+ [self getArgument:&doubleValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%f", doubleValue];
}
-- (NSString *)doubleDescriptionAtIndex:(int)anInt;
-{
- double doubleValue;
-
- [self getArgument:&doubleValue atIndex:anInt];
- return [NSString stringWithFormat:@"%f", doubleValue];
+- (NSString*)floatDescriptionAtIndex:(NSInteger)anInt {
+ float floatValue;
+
+ [self getArgument:&floatValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%f", floatValue];
}
-- (NSString *)floatDescriptionAtIndex:(int)anInt
-{
- float floatValue;
-
- [self getArgument:&floatValue atIndex:anInt];
- return [NSString stringWithFormat:@"%f", floatValue];
+- (NSString*)longDoubleDescriptionAtIndex:(NSInteger)anInt {
+ long double longDoubleValue;
+
+ [self getArgument:&longDoubleValue atIndex:anInt];
+ return [NSString stringWithFormat:@"%Lf", longDoubleValue];
}
-- (NSString *)structDescriptionAtIndex:(int)anInt;
-{
- void *buffer;
-
- [self getArgument:&buffer atIndex:anInt];
- return [NSString stringWithFormat:@":(struct)%p", buffer];
+- (NSString*)structDescriptionAtIndex:(NSInteger)anInt {
+ return [NSString
+ stringWithFormat:@"(%@)",
+ [[self getArgumentAtIndexAsObject:anInt] description]];
}
-- (NSString *)pointerDescriptionAtIndex:(int)anInt
-{
- void *buffer;
-
- [self getArgument:&buffer atIndex:anInt];
- return [NSString stringWithFormat:@"%p", buffer];
+- (NSString*)pointerDescriptionAtIndex:(NSInteger)anInt {
+ void* buffer;
+
+ [self getArgument:&buffer atIndex:anInt];
+ return [NSString stringWithFormat:@"%p", buffer];
}
-- (NSString *)cStringDescriptionAtIndex:(int)anInt
-{
- char buffer[128];
-
- memset(buffer, 0x0, 128);
-
- [self getArgument:&buffer atIndex:anInt];
- return [NSString stringWithFormat:@"\"%s\"", buffer];
+- (NSString*)cStringDescriptionAtIndex:(NSInteger)anInt {
+ char buffer[104];
+ char* cStringPtr;
+
+ [self getArgument:&cStringPtr atIndex:anInt];
+ strlcpy(buffer, cStringPtr, sizeof(buffer));
+ strlcpy(buffer + 100, "...", (sizeof(buffer) - 100));
+ return [NSString stringWithFormat:@"\"%s\"", buffer];
}
-- (NSString *)selectorDescriptionAtIndex:(int)anInt
-{
- SEL selectorValue;
-
- [self getArgument:&selectorValue atIndex:anInt];
- return [NSString stringWithFormat:@"@selector(%@)", NSStringFromSelector(selectorValue)];
+- (NSString*)selectorDescriptionAtIndex:(NSInteger)anInt {
+ SEL selectorValue;
+
+ [self getArgument:&selectorValue atIndex:anInt];
+ return [NSString
+ stringWithFormat:@"@selector(%@)", NSStringFromSelector(selectorValue)];
}
@end
« no previous file with comments | « third_party/ocmock/OCMock/NSInvocation+OCMAdditions.h ('k') | third_party/ocmock/OCMock/NSMethodSignature+OCMAdditions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698