Skip to content

Commit 3612a15

Browse files
committed
Add support for controlling retain/copy semantics for arguments to stubs.
Allows marking an argument in a stub as having various semantics: - is not retained by invocations Object arguments are retained by default in OCMock. In some cases to avoid retain loops you need to mark an argument as unretained. - is not retained by stub Stub arguments are retained by default in OCMock. In some specialized cases you do not want the stub arguments retained - is copied by invocation Some arguments have copy semantics and we need the invocation to copy the argument instead of retain it.
1 parent 645be99 commit 3612a15

11 files changed

+375
-103
lines changed

Source/OCMock/NSInvocation+OCMAdditions.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
+ (NSInvocation *)invocationForBlock:(id)block withArguments:(NSArray *)arguments;
2222

23-
- (void)retainObjectArgumentsExcludingObject:(id)objectToExclude;
24-
23+
- (void)applyConstraintOptionsFromStubInvocation:(NSInvocation *)stubInvocation excludingObject:(id)objectToExclude;
2524
- (id)getArgumentAtIndexAsObject:(NSInteger)argIndex;
2625

2726
- (NSString *)invocationDescription;

Source/OCMock/NSInvocation+OCMAdditions.m

+27-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,20 @@ + (NSInvocation *)invocationForBlock:(id)block withArguments:(NSArray *)argument
5656
}
5757

5858

59+
- (OCMConstraintOptions)getArgumentContraintOptionsForArgumentAtIndex:(NSUInteger)index
60+
{
61+
id argument;
62+
[self getArgument:&argument atIndex:index];
63+
if(![argument isProxy] && [argument isKindOfClass:[OCMConstraint class]])
64+
{
65+
return [(OCMConstraint *)argument constraintOptions];
66+
}
67+
return OCMConstraintDefaultOptions;
68+
}
69+
5970
static NSString *const OCMRetainedObjectArgumentsKey = @"OCMRetainedObjectArgumentsKey";
6071

61-
- (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
72+
- (void)applyConstraintOptionsFromStubInvocation:(NSInvocation *)stubInvocation excludingObject:(id)objectToExclude
6273
{
6374
if(objc_getAssociatedObject(self, OCMRetainedObjectArgumentsKey) != nil)
6475
{
@@ -112,7 +123,21 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
112123
}
113124
else
114125
{
115-
[retainedArguments addObject:argument];
126+
// Conform to the constraintOptions in the stub (if any).
127+
OCMConstraintOptions constraintOptions = [stubInvocation getArgumentContraintOptionsForArgumentAtIndex:index];
128+
if((constraintOptions & OCMConstraintCopyInvocationArg))
129+
{
130+
// Copy not only retains the copy in our array
131+
// but updates the arg in the invocation that we store.
132+
id argCopy = [argument copy];
133+
[retainedArguments addObject:argCopy];
134+
[self setArgument:&argCopy atIndex:index];
135+
[argCopy release];
136+
}
137+
else if(!(constraintOptions & OCMConstraintDoNotRetainInvocationArg))
138+
{
139+
[retainedArguments addObject:argument];
140+
}
116141
}
117142
}
118143
}

Source/OCMock/OCMArg.h

+36
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,37 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19+
// Options for controlling how OCMArgs function.
20+
typedef NS_OPTIONS(NSUInteger, OCMArgOptions) {
21+
// The OCMArg will retain/release the value passed to it, and invocations on a stub that has
22+
// arguments that the OCMArg is constraining will retain the values passed to them for the
23+
// arguments being constrained by the OCMArg.
24+
OCMArgDefaultOptions = 0UL,
25+
26+
// The OCMArg will not retain/release the value passed to it. Is only applicable for
27+
// `isEqual:options:` and `isNotEqual:options`. The caller is responsible for making sure that the
28+
// arg is valid for the required lifetime. Note that unless `OCMArgDoNotRetainInvocationArg` is
29+
// also specified, invocations of the stub that the OCMArg arg is constraining will retain values
30+
// passed to them for the arguments being constrained by the OCMArg. `OCMArgNeverRetainArg` is
31+
// usually what you want to use.
32+
OCMArgDoNotRetainStubArg = (1UL << 0),
33+
34+
// Invocations on a stub that has arguments that the OCMArg is constraining will retain/release
35+
// the values passed to them for the arguments being constrained by the OCMArg.
36+
OCMArgDoNotRetainInvocationArg = (1UL << 1),
37+
38+
// Invocations on a stub that has arguments that the OCMArg is constraining will copy/release
39+
// the values passed to them for the arguments being constrained by the OCMArg.
40+
OCMArgCopyInvocationArg = (1UL << 2),
41+
42+
OCMArgNeverRetainArg = OCMArgDoNotRetainStubArg | OCMArgDoNotRetainInvocationArg,
43+
};
44+
1945
@interface OCMArg : NSObject
2046

2147
// constraining arguments
2248

49+
// constrain using OCMArgDefaultOptions
2350
+ (id)any;
2451
+ (SEL)anySelector;
2552
+ (void *)anyPointer;
@@ -32,6 +59,15 @@
3259
+ (id)checkWithSelector:(SEL)selector onObject:(id)anObject;
3360
+ (id)checkWithBlock:(BOOL (^)(id obj))block;
3461

62+
+ (id)anyWithOptions:(OCMArgOptions)options;
63+
+ (id)isNilWithOptions:(OCMArgOptions)options;
64+
+ (id)isNotNilWithOptions:(OCMArgOptions)options;
65+
+ (id)isEqual:(id)value options:(OCMArgOptions)options;
66+
+ (id)isNotEqual:(id)value options:(OCMArgOptions)options;
67+
+ (id)isKindOfClass:(Class)cls options:(OCMArgOptions)options;
68+
+ (id)checkWithSelector:(SEL)selector onObject:(id)anObject options:(OCMArgOptions)options;
69+
+ (id)checkWithOptions:(OCMArgOptions)options withBlock:(BOOL (^)(id obj))block;
70+
3571
// manipulating arguments
3672

3773
+ (id *)setTo:(id)value;

Source/OCMock/OCMArg.m

+59-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ @implementation OCMArg
2424

2525
+ (id)any
2626
{
27-
return [[[OCMAnyConstraint alloc] init] autorelease];
27+
return [self anyWithOptions:OCMArgDefaultOptions];
2828
}
2929

3030
+ (void *)anyPointer
@@ -44,39 +44,79 @@ + (SEL)anySelector
4444

4545
+ (id)isNil
4646
{
47-
return [OCMIsNilConstraint constraint];
47+
return [self isNilWithOptions:OCMArgDefaultOptions];
4848
}
4949

5050
+ (id)isNotNil
5151
{
52-
return [OCMIsNotNilConstraint constraint];
52+
return [self isNotNilWithOptions:OCMArgDefaultOptions];
5353
}
5454

5555
+ (id)isEqual:(id)value
5656
{
57-
return [[[OCMIsEqualConstraint alloc] initWithTestValue:value] autorelease];
57+
return [self isEqual:value options:OCMArgDefaultOptions];
5858
}
5959

6060
+ (id)isNotEqual:(id)value
6161
{
62-
return [[[OCMIsNotEqualConstraint alloc] initWithTestValue:value] autorelease];
62+
return [self isNotEqual:value options:OCMArgDefaultOptions];
6363
}
6464

6565
+ (id)isKindOfClass:(Class)cls
6666
{
67-
return [[[OCMBlockConstraint alloc] initWithConstraintBlock:^BOOL(id obj) {
68-
return [obj isKindOfClass:cls];
69-
}] autorelease];
67+
return [self isKindOfClass:cls options:OCMArgDefaultOptions];
7068
}
7169

7270
+ (id)checkWithSelector:(SEL)selector onObject:(id)anObject
7371
{
74-
return [OCMConstraint constraintWithSelector:selector onObject:anObject];
72+
return [self checkWithSelector:selector onObject:anObject options:OCMArgDefaultOptions];
7573
}
7674

7775
+ (id)checkWithBlock:(BOOL (^)(id))block
7876
{
79-
return [[[OCMBlockConstraint alloc] initWithConstraintBlock:block] autorelease];
77+
return [self checkWithOptions:OCMArgDefaultOptions withBlock:block];
78+
}
79+
80+
+ (id)anyWithOptions:(OCMArgOptions)options
81+
{
82+
return [[[OCMAnyConstraint alloc] initWithOptions:[self constraintOptionsFromArgOptions:options]] autorelease];
83+
}
84+
85+
+ (id)isNilWithOptions:(OCMArgOptions)options
86+
{
87+
return [[[OCMIsEqualConstraint alloc] initWithTestValue:nil options:[self constraintOptionsFromArgOptions:options]] autorelease];
88+
}
89+
90+
+ (id)isNotNilWithOptions:(OCMArgOptions)options
91+
{
92+
return [[[OCMIsNotEqualConstraint alloc] initWithTestValue:nil options:[self constraintOptionsFromArgOptions:options]] autorelease];
93+
}
94+
95+
+ (id)isEqual:(id)value options:(OCMArgOptions)options
96+
{
97+
return [[[OCMIsEqualConstraint alloc] initWithTestValue:value options:[self constraintOptionsFromArgOptions:options]] autorelease];
98+
}
99+
100+
+ (id)isNotEqual:(id)value options:(OCMArgOptions)options
101+
{
102+
return [[[OCMIsNotEqualConstraint alloc] initWithTestValue:value options:[self constraintOptionsFromArgOptions:options]] autorelease];
103+
}
104+
105+
+ (id)isKindOfClass:(Class)cls options:(OCMArgOptions)options
106+
{
107+
return [[[OCMBlockConstraint alloc] initWithOptions:[self constraintOptionsFromArgOptions:options] block:^BOOL(id obj) {
108+
return [obj isKindOfClass:cls];
109+
}] autorelease];
110+
}
111+
112+
+ (id)checkWithSelector:(SEL)selector onObject:(id)anObject options:(OCMArgOptions)options
113+
{
114+
return [OCMConstraint constraintWithSelector:selector onObject:anObject options:[self constraintOptionsFromArgOptions:options]];
115+
}
116+
117+
+ (id)checkWithOptions:(OCMArgOptions)options withBlock:(BOOL (^)(id obj))block
118+
{
119+
return [[[OCMBlockConstraint alloc] initWithOptions:[self constraintOptionsFromArgOptions:options] block:block] autorelease];
80120
}
81121

82122
+ (id *)setTo:(id)value
@@ -141,4 +181,13 @@ + (id)resolveSpecialValues:(NSValue *)value
141181
return value;
142182
}
143183

184+
+ (OCMConstraintOptions)constraintOptionsFromArgOptions:(OCMArgOptions)argOptions
185+
{
186+
OCMConstraintOptions constraintOptions = 0;
187+
if(argOptions & OCMArgDoNotRetainStubArg) constraintOptions |= OCMConstraintDoNotRetainStubArg;
188+
if(argOptions & OCMArgDoNotRetainInvocationArg) constraintOptions |= OCMConstraintDoNotRetainInvocationArg;
189+
if(argOptions & OCMArgCopyInvocationArg) constraintOptions |= OCMConstraintCopyInvocationArg;
190+
return constraintOptions;
191+
}
192+
144193
@end

Source/OCMock/OCMConstraint.h

+22-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,21 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19+
// See OCMArgOptions for documentation on options.
20+
typedef NS_OPTIONS(NSUInteger, OCMConstraintOptions) {
21+
OCMConstraintDefaultOptions = 0UL,
22+
OCMConstraintDoNotRetainStubArg = (1UL << 0),
23+
OCMConstraintDoNotRetainInvocationArg = (1UL << 1),
24+
OCMConstraintCopyInvocationArg = (1UL << 2),
25+
OCMConstraintNeverRetainArg = OCMConstraintDoNotRetainStubArg | OCMConstraintDoNotRetainInvocationArg,
26+
};
1927

20-
@interface OCMConstraint : NSObject
28+
@interface OCMConstraint : NSObject
29+
30+
@property (readonly) OCMConstraintOptions constraintOptions;
31+
32+
- (instancetype)initWithOptions:(OCMConstraintOptions)options NS_DESIGNATED_INITIALIZER;
33+
- (instancetype)init NS_UNAVAILABLE;
2134

2235
- (BOOL)evaluate:(id)value;
2336

@@ -28,6 +41,8 @@
2841
+ (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject;
2942
+ (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject withValue:(id)aValue;
3043

44+
+ (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject options:(OCMConstraintOptions)options;
45+
+ (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject withValue:(id)aValue options:(OCMConstraintOptions)options;
3146

3247
@end
3348

@@ -45,8 +60,8 @@
4560
id testValue;
4661
}
4762

48-
- (instancetype)initWithTestValue:(id)testValue NS_DESIGNATED_INITIALIZER;
49-
- (instancetype)init NS_UNAVAILABLE;
63+
- (instancetype)initWithTestValue:(id)testValue options:(OCMConstraintOptions)options NS_DESIGNATED_INITIALIZER;
64+
- (instancetype)initWithOptions:(OCMConstraintOptions)options NS_UNAVAILABLE;
5065

5166
@end
5267

@@ -61,8 +76,8 @@
6176
NSInvocation *invocation;
6277
}
6378

64-
- (instancetype)initWithInvocation:(NSInvocation *)invocation NS_DESIGNATED_INITIALIZER;
65-
- (instancetype)init NS_UNAVAILABLE;
79+
- (instancetype)initWithInvocation:(NSInvocation *)invocation options:(OCMConstraintOptions)options NS_DESIGNATED_INITIALIZER;
80+
- (instancetype)initWithOptions:(OCMConstraintOptions)options NS_UNAVAILABLE;
6681

6782
@end
6883

@@ -71,8 +86,8 @@
7186
BOOL (^block)(id);
7287
}
7388

74-
- (instancetype)initWithConstraintBlock:(BOOL (^)(id))block NS_DESIGNATED_INITIALIZER;
75-
- (instancetype)init NS_UNAVAILABLE;
89+
- (instancetype)initWithOptions:(OCMConstraintOptions)options block:(BOOL (^)(id))block NS_DESIGNATED_INITIALIZER;
90+
- (instancetype)initWithOptions:(OCMConstraintOptions)options NS_UNAVAILABLE;
7691

7792
@end
7893

0 commit comments

Comments
 (0)