Skip to content

Commit 9d27214

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 a41d9df commit 9d27214

12 files changed

+315
-210
lines changed

Source/OCMock/NSInvocation+OCMAdditions.h

Lines changed: 1 addition & 2 deletions
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 excludingObjectsAtIndexes:(NSIndexSet *)indexes;
24-
23+
- (void)applyConstraintOptionsFromStubInvocation:(NSInvocation *)stubInvocation excludingObject:(id)objectToExclude;
2524
- (id)getArgumentAtIndexAsObject:(NSInteger)argIndex;
2625

2726
- (NSString *)invocationDescription;

Source/OCMock/NSInvocation+OCMAdditions.m

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,20 @@ + (NSInvocation *)invocationForBlock:(id)block withArguments:(NSArray *)argument
5353
}
5454

5555

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

58-
- (void)retainObjectArgumentsExcludingObject:(id)objectToExclude excludingObjectsAtIndexes:(NSIndexSet *)indexes;
69+
- (void)applyConstraintOptionsFromStubInvocation:(NSInvocation *)stubInvocation excludingObject:(id)objectToExclude
5970
{
6071
if(objc_getAssociatedObject(self, OCMRetainedObjectArgumentsKey) != nil)
6172
{
@@ -80,16 +91,7 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude excludingObject
8091
for(NSUInteger index = 2; index < numberOfArguments; index++)
8192
{
8293
const char *argumentType = [[self methodSignature] getArgumentTypeAtIndex:index];
83-
BOOL isObjectType = OCMIsObjectType(argumentType);
84-
if ([indexes containsIndex:index])
85-
{
86-
if (!isObjectType)
87-
{
88-
[NSException raise:NSInternalInconsistencyException format:@"Argument at %d is not an object", (int)index];
89-
}
90-
continue;
91-
}
92-
if (isObjectType)
94+
if (OCMIsObjectType(argumentType))
9395
{
9496
id argument;
9597
[self getArgument:&argument atIndex:index];
@@ -118,7 +120,21 @@ - (void)retainObjectArgumentsExcludingObject:(id)objectToExclude excludingObject
118120
}
119121
else
120122
{
121-
[retainedArguments addObject:argument];
123+
// Conform to the constraintOptions in the stub (if any).
124+
OCMConstraintOptions constraintOptions = [stubInvocation getArgumentContraintOptionsForArgumentAtIndex:index];
125+
if((constraintOptions & OCMConstraintCopyInvocationArg))
126+
{
127+
// Copy not only retains the copy in our array
128+
// but updates the arg in the invocation that we store.
129+
id argCopy = [argument copy];
130+
[retainedArguments addObject:argCopy];
131+
[self setArgument:&argCopy atIndex:index];
132+
[argCopy release];
133+
}
134+
else if(!(constraintOptions & OCMConstraintDoNotRetainInvocationArg))
135+
{
136+
[retainedArguments addObject:argument];
137+
}
122138
}
123139
}
124140
}

Source/OCMock/OCMArg.h

Lines changed: 35 additions & 18 deletions
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,21 +59,14 @@
3259
+ (id)checkWithSelector:(SEL)selector onObject:(id)anObject;
3360
+ (id)checkWithBlock:(BOOL (^)(id obj))block;
3461

35-
// Unretained object arguments are not retained by invocations on the mock, but are retained by the
36-
// stub itself. A use case for this is when you are stubbing an argument to a method that does not
37-
// retain its argument using an `OCMArg` variant that you do not want to keep a reference to.
38-
// See `OCMOCK_ANY_UNRETAINED`.
39-
+ (id)unretainedObject:(id)anObject;
40-
41-
// Unsafe unretained object arguments are not retained by invocations on the mock or by the stub.
42-
// A potential use case for this is when you are stubbing methods that do not retain their
43-
// arguments and you want to verify dealloc conditions. An example of this would be verifying
44-
// KVO registration/deregistration that occurs in the init/dealloc of an object. If the object were
45-
// retained by the mocking system in any way you would never see the deregistration.
46-
// Note that you *must* keep a reference to anObject outside this call or you will crash.
47-
// Something like `[OCMArg unsafeUnretainedObject:[[Foo alloc] init]]` under ARC is a guaranteed
48-
// dangling pointer problem.
49-
+ (id)unsafeUnretainedObject:(id)anObject;
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;
5070

5171
// manipulating arguments
5272

@@ -61,9 +81,6 @@
6181

6282
+ (id)resolveSpecialValues:(NSValue *)value;
6383

64-
// Return YES if `object` is either an unretained or an unsafe unretained object.
65-
+ (BOOL)isUnretained:(id)object;
66-
6784
@end
6885

6986
#define OCMOCK_ANY [OCMArg any]

Source/OCMock/OCMArg.m

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ @implementation OCMArg
2525

2626
+ (id)any
2727
{
28-
return [[[OCMAnyConstraint alloc] init] autorelease];
28+
return [self anyWithOptions:OCMArgDefaultOptions];
2929
}
3030

3131
+ (void *)anyPointer
@@ -45,39 +45,79 @@ + (SEL)anySelector
4545

4646
+ (id)isNil
4747
{
48-
return [OCMIsNilConstraint constraint];
48+
return [self isNilWithOptions:OCMArgDefaultOptions];
4949
}
5050

5151
+ (id)isNotNil
5252
{
53-
return [OCMIsNotNilConstraint constraint];
53+
return [self isNotNilWithOptions:OCMArgDefaultOptions];
5454
}
5555

5656
+ (id)isEqual:(id)value
5757
{
58-
return [[[OCMIsEqualConstraint alloc] initWithTestValue:value] autorelease];
58+
return [self isEqual:value options:OCMArgDefaultOptions];
5959
}
6060

6161
+ (id)isNotEqual:(id)value
6262
{
63-
return [[[OCMIsNotEqualConstraint alloc] initWithTestValue:value] autorelease];
63+
return [self isNotEqual:value options:OCMArgDefaultOptions];
6464
}
6565

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

7371
+ (id)checkWithSelector:(SEL)selector onObject:(id)anObject
7472
{
75-
return [OCMConstraint constraintWithSelector:selector onObject:anObject];
73+
return [self checkWithSelector:selector onObject:anObject options:OCMArgDefaultOptions];
7674
}
7775

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

83123
+ (id)unretainedObject:(id)anObject
@@ -152,9 +192,13 @@ + (id)resolveSpecialValues:(NSValue *)value
152192
return value;
153193
}
154194

155-
+ (BOOL)isUnretained:(id)object
195+
+ (OCMConstraintOptions)constraintOptionsFromArgOptions:(OCMArgOptions)argOptions
156196
{
157-
return object_getClass(object) == [OCMUnretainedArgument class];
197+
OCMConstraintOptions constraintOptions = 0;
198+
if(argOptions & OCMArgDoNotRetainStubArg) constraintOptions |= OCMConstraintDoNotRetainStubArg;
199+
if(argOptions & OCMArgDoNotRetainInvocationArg) constraintOptions |= OCMConstraintDoNotRetainInvocationArg;
200+
if(argOptions & OCMArgCopyInvocationArg) constraintOptions |= OCMConstraintCopyInvocationArg;
201+
return constraintOptions;
158202
}
159203

160204
@end

Source/OCMock/OCMConstraint.h

Lines changed: 22 additions & 7 deletions
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)