1515 */
1616
1717#import < OCMock/OCMConstraint.h>
18-
18+ # import " OCMFunctions.h "
1919
2020@implementation OCMConstraint
2121
@@ -34,26 +34,30 @@ - (id)copyWithZone:(struct _NSZone *)zone
3434 return [self retain ];
3535}
3636
37+ + (NSInvocation *)invocationWithSelector : (SEL )aSelector onObject : (id )anObject
38+ {
39+ NSMethodSignature *signature = [anObject methodSignatureForSelector: aSelector];
40+ if (signature == nil )
41+ [NSException raise: NSInvalidArgumentException format: @" Unknown selector %@ used in constraint." , NSStringFromSelector (aSelector)];
42+ NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: signature];
43+ [invocation setTarget: anObject];
44+ [invocation setSelector: aSelector];
45+ return invocation;
46+ }
47+
3748+ (instancetype )constraintWithSelector : (SEL )aSelector onObject : (id )anObject
3849{
39- OCMInvocationConstraint *constraint = [OCMInvocationConstraint constraint ];
40- NSMethodSignature *signature = [anObject methodSignatureForSelector: aSelector];
41- if (signature == nil )
42- [NSException raise: NSInvalidArgumentException format: @" Unkown selector %@ used in constraint." , NSStringFromSelector (aSelector)];
43- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: signature];
44- [invocation setTarget: anObject];
45- [invocation setSelector: aSelector];
46- constraint->invocation = invocation;
47- return constraint;
50+ NSInvocation *invocation = [self invocationWithSelector: aSelector onObject: anObject];
51+ return [[[OCMInvocationConstraint alloc ] initWithInvocation: invocation] autorelease ];
4852}
4953
5054+ (instancetype )constraintWithSelector : (SEL )aSelector onObject : (id )anObject withValue : (id )aValue
5155{
52- OCMInvocationConstraint *constraint = (OCMInvocationConstraint *) [self constraintWithSelector : aSelector onObject: anObject];
53- if ([[constraint-> invocation methodSignature ] numberOfArguments ] < 4 )
54- [NSException raise: NSInvalidArgumentException format: @" Constraint with value requires selector with two arguments." ];
55- [constraint-> invocation setArgument: &aValue atIndex: 3 ];
56- return constraint ;
56+ NSInvocation *invocation = [self invocationWithSelector : aSelector onObject: anObject];
57+ if ([[invocation methodSignature ] numberOfArguments ] < 4 )
58+ [NSException raise: NSInvalidArgumentException format: @" Constraint with value requires selector with two arguments." ];
59+ [ invocation setArgument: &aValue atIndex: 3 ];
60+ return [[[OCMInvocationConstraint alloc ] initWithInvocation: invocation] autorelease ] ;
5761}
5862
5963
@@ -125,6 +129,33 @@ - (BOOL)evaluate:(id)value
125129
126130@implementation OCMInvocationConstraint
127131
132+ - (instancetype )initWithInvocation : (NSInvocation *)anInvocation {
133+ if ((self = [super init ]))
134+ {
135+ NSMethodSignature *signature = [anInvocation methodSignature ];
136+ if ([signature numberOfArguments ] < 3 )
137+ {
138+ [NSException raise: NSInvalidArgumentException format: @" invocation must take at least one argument (other than _cmd and self)" ];
139+ }
140+ if (!(OCMIsObjectType ([signature getArgumentTypeAtIndex: 2 ])))
141+ {
142+ [NSException raise: NSInvalidArgumentException format: @" invocation's second argument must be an object type" ];
143+ }
144+ if (strcmp ([signature methodReturnType ], @encode (BOOL )))
145+ {
146+ [NSException raise: NSInvalidArgumentException format: @" invocation must return BOOL" ];
147+ }
148+ invocation = [anInvocation retain ];
149+ }
150+ return self;
151+ }
152+
153+ - (void )dealloc
154+ {
155+ [invocation release ];
156+ [super dealloc ];
157+ }
158+
128159- (BOOL )evaluate : (id )value
129160{
130161 [invocation setArgument: &value atIndex: 2 ]; // should test if constraint takes arg
0 commit comments