Skip to content

Commit a7a5473

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 ff92e90 commit a7a5473

11 files changed

+378
-103
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;
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: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,20 @@ + (NSInvocation *)invocationForBlock:(id)block withArguments:(NSArray *)argument
5454
}
5555

5656

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

59-
- (void)retainObjectArgumentsExcludingObject:(id)objectToExclude
70+
- (void)applyConstraintOptionsFromStubInvocation:(NSInvocation *)stubInvocation excludingObject:(id)objectToExclude
6071
{
6172
if(objc_getAssociatedObject(self, OCMRetainedObjectArgumentsKey) != nil)
6273
{
@@ -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

Lines changed: 36 additions & 0 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,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

Lines changed: 59 additions & 10 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 *)setTo:(id)value
@@ -142,4 +182,13 @@ + (id)resolveSpecialValues:(NSValue *)value
142182
return value;
143183
}
144184

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

Source/OCMock/OCMConstraint.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,22 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19-
@interface OCMConstraint : NSObject
19+
20+
// See OCMArgOptions for documentation on options.
21+
typedef NS_OPTIONS(NSUInteger, OCMConstraintOptions) {
22+
OCMConstraintDefaultOptions = 0UL,
23+
OCMConstraintDoNotRetainStubArg = (1UL << 0),
24+
OCMConstraintDoNotRetainInvocationArg = (1UL << 1),
25+
OCMConstraintCopyInvocationArg = (1UL << 2),
26+
OCMConstraintNeverRetainArg = OCMConstraintDoNotRetainStubArg | OCMConstraintDoNotRetainInvocationArg,
27+
};
28+
29+
@interface OCMConstraint : NSObject
30+
31+
@property (readonly) OCMConstraintOptions constraintOptions;
32+
33+
- (instancetype)initWithOptions:(OCMConstraintOptions)options NS_DESIGNATED_INITIALIZER;
34+
- (instancetype)init NS_UNAVAILABLE;
2035

2136
- (BOOL)evaluate:(id)value;
2237

@@ -27,6 +42,8 @@
2742
+ (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject;
2843
+ (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject withValue:(id)aValue;
2944

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

3148
@end
3249

@@ -44,8 +61,8 @@
4461
id testValue;
4562
}
4663

47-
- (instancetype)initWithTestValue:(id)testValue NS_DESIGNATED_INITIALIZER;
48-
- (instancetype)init NS_UNAVAILABLE;
64+
- (instancetype)initWithTestValue:(id)testValue options:(OCMConstraintOptions)options NS_DESIGNATED_INITIALIZER;
65+
- (instancetype)initWithOptions:(OCMConstraintOptions)options NS_UNAVAILABLE;
4966

5067
@end
5168

@@ -60,8 +77,8 @@
6077
NSInvocation *invocation;
6178
}
6279

63-
- (instancetype)initWithInvocation:(NSInvocation *)invocation NS_DESIGNATED_INITIALIZER;
64-
- (instancetype)init NS_UNAVAILABLE;
80+
- (instancetype)initWithInvocation:(NSInvocation *)invocation options:(OCMConstraintOptions)options NS_DESIGNATED_INITIALIZER;
81+
- (instancetype)initWithOptions:(OCMConstraintOptions)options NS_UNAVAILABLE;
6582

6683
@end
6784

@@ -70,8 +87,8 @@
7087
BOOL (^block)(id);
7188
}
7289

73-
- (instancetype)initWithConstraintBlock:(BOOL (^)(id))block NS_DESIGNATED_INITIALIZER;
74-
- (instancetype)init NS_UNAVAILABLE;
90+
- (instancetype)initWithOptions:(OCMConstraintOptions)options block:(BOOL (^)(id))block NS_DESIGNATED_INITIALIZER;
91+
- (instancetype)initWithOptions:(OCMConstraintOptions)options NS_UNAVAILABLE;
7592

7693
@end
7794

0 commit comments

Comments
 (0)