Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 218 lines (167 sloc) 6.817 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
//
// Licensed under the terms in License.txt
//
// Copyright 2010 Allen Ding. All rights reserved.
//

#import "KWStub.h"
#import "KWMessagePattern.h"
#import "KWObjCUtilities.h"
#import "KWStringUtilities.h"
#import "KWValue.h"

#import "NSInvocation+OCMAdditions.h"

@implementation KWStub

#pragma mark -
#pragma mark Initializing

- (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern {
    return [self initWithMessagePattern:aMessagePattern value:nil];
}

- (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValue {
    if ((self = [super init])) {
        messagePattern = [aMessagePattern retain];
        value = [aValue retain];
    }

    return self;
}

- (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern block:(id (^)(NSArray *params))aBlock {
    if ((self = [super init])) {
        messagePattern = [aMessagePattern retain];
        block = [aBlock copy];
    }

    return self;
}

- (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue {
    if ((self = [super init])) {
        messagePattern = [aMessagePattern retain];
        value = [aValue retain];
        returnValueTimes = [times retain];
        secondValue = [aSecondValue retain];
    }
    
    return self;
}

+ (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern {
    return [self stubWithMessagePattern:aMessagePattern value:nil];
}

+ (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValue {
    return [[[self alloc] initWithMessagePattern:aMessagePattern value:aValue] autorelease];
}

+ (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern block:(id (^)(NSArray *params))aBlock {
    return [[[self alloc] initWithMessagePattern:aMessagePattern block:aBlock] autorelease];
}

+ (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue {
    return [[[self alloc] initWithMessagePattern:aMessagePattern value:aValue times:times afterThatReturn:aSecondValue] autorelease];
}

- (void)dealloc {
    [messagePattern release];
    [value release];
    [returnValueTimes release];
    [secondValue release];
[block release];
    [super dealloc];
}

#pragma mark -
#pragma mark Properties

@synthesize messagePattern;
@synthesize value;
@synthesize secondValue;
@synthesize returnValueTimes;
@synthesize returnedValueTimes;

#pragma mark -
#pragma mark Processing Invocations

- (void)writeZerosToInvocationReturnValue:(NSInvocation *)anInvocation {
    NSUInteger returnLength = [[anInvocation methodSignature] methodReturnLength];

    if (returnLength == 0)
        return;

    void *bytes = malloc(returnLength);
    memset(bytes, 0, returnLength);
    [anInvocation setReturnValue:bytes];
    free(bytes);
}

- (NSData *)valueDataWithObjCType:(const char *)objCType {
    assert(self.value && "self.value must not be nil");
    NSData *data = [self.value dataForObjCType:objCType];

    if (data == nil) {
        [NSException raise:@"KWStubException" format:@"wrapped stub value type (%s) could not be converted to the target type (%s)",
                                                     [self.value objCType],
                                                     objCType];
    }

    return data;
}

- (void)writeWrappedValueToInvocationReturnValue:(NSInvocation *)anInvocation {
    assert(self.value && "self.value must not be nil");
    const char *returnType = [[anInvocation methodSignature] methodReturnType];
    NSData *data = nil;

    NSData *choosedForData = [self.value dataValue];

    if (returnValueTimes != nil) {
        NSString *returnValueTimesString = returnValueTimes;
        int returnValueTimesInt = [returnValueTimesString intValue];
        
        if (returnedValueTimes >= returnValueTimesInt) {
            choosedForData = [self.secondValue dataValue];
        }
        returnedValueTimes++;
    }

    
    // When the return type is not the same as the type of the wrapped value,
    // attempt to convert the wrapped value to the desired type.

    if (KWObjCTypeEqualToObjCType([self.value objCType], returnType))
        data = choosedForData;
    else
        data = [self valueDataWithObjCType:returnType];

    [anInvocation setReturnValue:(void *)[data bytes]];
}

- (void)writeObjectValueToInvocationReturnValue:(NSInvocation *)anInvocation {
    assert(self.value && "self.value must not be nil");
    
    void *choosedForData = &value;
    
    if (returnValueTimes != nil) {
        NSString *returnValueTimesString = returnValueTimes;
        int returnValueTimesInt = [returnValueTimesString intValue];
        
        if (returnedValueTimes >= returnValueTimesInt) {
            choosedForData = &secondValue;
        }
        returnedValueTimes++;
    }

    [anInvocation setReturnValue:choosedForData];

#ifndef __clang_analyzer__
    NSString *selectorString = NSStringFromSelector([anInvocation selector]);

    // To conform to memory management conventions, retain if writing a result
    // that begins with alloc, new or contains copy. This shows up as a false
    // positive in clang due to the runtime conditional, so ignore it.
    if (KWStringHasWordPrefix(selectorString, @"alloc") ||
        KWStringHasWordPrefix(selectorString, @"new") ||
        KWStringHasWord(selectorString, @"copy") ||
        KWStringHasWord(selectorString, @"Copy")) {
        [self.value retain];
    }
#endif
}

- (BOOL)processInvocation:(NSInvocation *)anInvocation {
    if (![self.messagePattern matchesInvocation:anInvocation])
        return NO;

if (block) {
NSUInteger numberOfArguments = [[anInvocation methodSignature] numberOfArguments];
NSMutableArray *args = [NSMutableArray arrayWithCapacity:(numberOfArguments-2)];
for (NSUInteger i = 2; i < numberOfArguments; ++i) {
id arg = [anInvocation getArgumentAtIndexAsObject:i];

const char *argType = [[anInvocation methodSignature] getArgumentTypeAtIndex:i];
if (strcmp(argType, "@?") == 0) arg = [[arg copy] autorelease];
[args addObject:arg];
}

id newValue = block(args);
if (newValue != value) {
[value release];
value = [newValue retain];
}

[args removeAllObjects]; // We don't want these objects to be in autorelease pool
}

    if (self.value == nil)
        [self writeZerosToInvocationReturnValue:anInvocation];
    else if ([self.value isKindOfClass:[KWValue class]])
        [self writeWrappedValueToInvocationReturnValue:anInvocation];
    else
        [self writeObjectValueToInvocationReturnValue:anInvocation];

    return YES;
}

#pragma mark -
#pragma mark Debugging

- (NSString *)description {
    return [NSString stringWithFormat:@"messagePattern: %@\nvalue: %@", self.messagePattern, self.value];
}

@end
Something went wrong with that request. Please try again.