Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ab441818fa
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 137 lines (89 sloc) 2.559 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
//---------------------------------------------------------------------------------------
// $Id: OCMConstraint.m 57 2010-07-19 06:14:27Z erik $
// Copyright (c) 2007-2010 by Mulle Kybernetik. See License file for details.
//---------------------------------------------------------------------------------------

#import <OCMock/OCMConstraint.h>


@implementation OCMConstraint

+ (id)constraint
{
return [[[self alloc] init] autorelease];
}

- (BOOL)evaluate:(id)value
{
return NO;
}


+ (id)constraintWithSelector:(SEL)aSelector onObject:(id)anObject
{
OCMInvocationConstraint *constraint = [OCMInvocationConstraint constraint];
NSMethodSignature *signature = [anObject methodSignatureForSelector:aSelector];
if(signature == nil)
[NSException raise:NSInvalidArgumentException format:@"Unkown selector %@ used in constraint.", NSStringFromSelector(aSelector)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:anObject];
[invocation setSelector:aSelector];
constraint->invocation = invocation;
return constraint;
}

+ (id)constraintWithSelector:(SEL)aSelector onObject:(id)anObject withValue:(id)aValue
{
OCMInvocationConstraint *constraint = [self constraintWithSelector:aSelector onObject:anObject];
if([[constraint->invocation methodSignature] numberOfArguments] < 4)
[NSException raise:NSInvalidArgumentException format:@"Constraint with value requires selector with two arguments."];
[constraint->invocation setArgument:&aValue atIndex:3];
return constraint;
}


@end



#pragma mark -

@implementation OCMAnyConstraint

- (BOOL)evaluate:(id)value
{
return YES;
}

@end



#pragma mark -

@implementation OCMIsNilConstraint

- (BOOL)evaluate:(id)value
{
return value == nil;
}

@end



#pragma mark -

@implementation OCMIsNotNilConstraint

- (BOOL)evaluate:(id)value
{
return value != nil;
}

@end



#pragma mark -

@implementation OCMIsNotEqualConstraint

- (BOOL)evaluate:(id)value
{
return ![value isEqual:testValue];
}

@end



#pragma mark -

@implementation OCMInvocationConstraint

- (BOOL)evaluate:(id)value
{
[invocation setArgument:&value atIndex:2]; // should test if constraint takes arg
[invocation invoke];
BOOL returnValue;
[invocation getReturnValue:&returnValue];
return returnValue;
}

@end

#pragma mark -

#if NS_BLOCKS_AVAILABLE

@implementation OCMBlockConstraint

- (id)initWithConstraintBlock:(BOOL (^)(id))aBlock;
{
[super init];
block = aBlock;
return self;
}

- (BOOL)evaluate:(id)value
{
return block(value);
}

@end

#endif
Something went wrong with that request. Please try again.