Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCMock Argument capture support #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions Source/OCMock.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
2FA281C641E64586FE9A506D /* NSMethodSignatureOCMAdditionsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2FA28DEDB9163597B7C49F3D /* NSMethodSignatureOCMAdditionsTests.m */; };
2FA2859DB0F5EF004A3FD0D3 /* OCMockObjectProtocolMocksTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2FA286BFBD8B9D068B41E7EF /* OCMockObjectProtocolMocksTests.m */; };
2FA28896D9AFD309D59C9CFB /* OCMArgTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2FA28EDBF243639C57F88A1B /* OCMArgTests.m */; };
4D4754FB17D663BE0060108B /* OCMArgumentCaptureConstraint.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D4754F917D663BE0060108B /* OCMArgumentCaptureConstraint.h */; };
4D4754FC17D663BE0060108B /* OCMArgumentCaptureConstraint.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D4754F917D663BE0060108B /* OCMArgumentCaptureConstraint.h */; settings = {ATTRIBUTES = (Public, ); }; };
4D4754FD17D663BE0060108B /* OCMArgumentCaptureConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D4754FA17D663BE0060108B /* OCMArgumentCaptureConstraint.m */; };
4D4754FE17D663BE0060108B /* OCMArgumentCaptureConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D4754FA17D663BE0060108B /* OCMArgumentCaptureConstraint.m */; };
4D4754FF17D663BE0060108B /* OCMArgumentCaptureConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D4754FA17D663BE0060108B /* OCMArgumentCaptureConstraint.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -214,6 +219,8 @@
2FA28CDB493350BF1DD26DC3 /* OCMockObjectProtocolMocksTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCMockObjectProtocolMocksTests.h; sourceTree = "<group>"; };
2FA28DEDB9163597B7C49F3D /* NSMethodSignatureOCMAdditionsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSMethodSignatureOCMAdditionsTests.m; sourceTree = "<group>"; };
2FA28EDBF243639C57F88A1B /* OCMArgTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCMArgTests.m; sourceTree = "<group>"; };
4D4754F917D663BE0060108B /* OCMArgumentCaptureConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCMArgumentCaptureConstraint.h; sourceTree = "<group>"; };
4D4754FA17D663BE0060108B /* OCMArgumentCaptureConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCMArgumentCaptureConstraint.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -430,6 +437,8 @@
03B3161A146334320052CD09 /* Argument Handling */ = {
isa = PBXGroup;
children = (
4D4754F917D663BE0060108B /* OCMArgumentCaptureConstraint.h */,
4D4754FA17D663BE0060108B /* OCMArgumentCaptureConstraint.m */,
03B3158D146333BF0052CD09 /* OCMArg.h */,
03B3158E146333BF0052CD09 /* OCMArg.m */,
03B31593146333BF0052CD09 /* OCMConstraint.h */,
Expand Down Expand Up @@ -492,6 +501,7 @@
03B315CD146333BF0052CD09 /* OCMBoxedReturnValueProvider.h in Headers */,
03B315D7146333BF0052CD09 /* OCMExceptionReturnValueProvider.h in Headers */,
03B315DC146333BF0052CD09 /* OCMIndirectReturnValueProvider.h in Headers */,
4D4754FB17D663BE0060108B /* OCMArgumentCaptureConstraint.h in Headers */,
03B315E1146333BF0052CD09 /* OCMNotificationPoster.h in Headers */,
03B315E6146333BF0052CD09 /* OCMObserverRecorder.h in Headers */,
03B315F5146333C00052CD09 /* OCMPassByRefSetter.h in Headers */,
Expand All @@ -513,6 +523,7 @@
03B315F1146333C00052CD09 /* OCMockRecorder.h in Headers */,
03B315C4146333BF0052CD09 /* OCMArg.h in Headers */,
03B315D3146333BF0052CD09 /* OCMConstraint.h in Headers */,
4D4754FC17D663BE0060108B /* OCMArgumentCaptureConstraint.h in Headers */,
03B315BA146333BF0052CD09 /* NSNotificationCenter+OCMAdditions.h in Headers */,
03B315B0146333BF0052CD09 /* NSInvocation+OCMAdditions.h in Headers */,
03B315B5146333BF0052CD09 /* NSMethodSignature+OCMAdditions.h in Headers */,
Expand Down Expand Up @@ -698,6 +709,7 @@
03B31606146333C00052CD09 /* OCObserverMockObject.m in Sources */,
03B3160B146333C00052CD09 /* OCPartialMockObject.m in Sources */,
03B31610146333C00052CD09 /* OCPartialMockRecorder.m in Sources */,
4D4754FD17D663BE0060108B /* OCMArgumentCaptureConstraint.m in Sources */,
03B31615146333C00052CD09 /* OCProtocolMockObject.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -706,6 +718,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4D4754FE17D663BE0060108B /* OCMArgumentCaptureConstraint.m in Sources */,
03B3162D1463350E0052CD09 /* NSInvocationOCMAdditionsTests.m in Sources */,
03B316321463350E0052CD09 /* OCMConstraintTests.m in Sources */,
03B316371463350E0052CD09 /* OCMockObjectHamcrestTests.mm in Sources */,
Expand Down Expand Up @@ -745,6 +758,7 @@
03B31608146333C00052CD09 /* OCObserverMockObject.m in Sources */,
03B3160D146333C00052CD09 /* OCPartialMockObject.m in Sources */,
03B31612146333C00052CD09 /* OCPartialMockRecorder.m in Sources */,
4D4754FF17D663BE0060108B /* OCMArgumentCaptureConstraint.m in Sources */,
03B31617146333C00052CD09 /* OCProtocolMockObject.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
16 changes: 16 additions & 0 deletions Source/OCMock/OCMArgumentCaptureConstraint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#import <Foundation/Foundation.h>
#import <OCMock/OCMock.h>

@interface OCMArg(OCMArgumentCaptureConstraint)

+ (id)capture:(void *)captor;

@end

@interface OCMArgumentCaptureConstraint : OCMConstraint {
id __strong *_captor;
}

- (id)initWithCaptor:(id __strong *)captor;

@end
53 changes: 53 additions & 0 deletions Source/OCMock/OCMArgumentCaptureConstraint.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#import "OCMArgumentCaptureConstraint.h"

@implementation OCMArg(OCMArgumentCaptureConstraint)

+ (id)capture:(void *)captor {
return [[OCMArgumentCaptureConstraint alloc] initWithCaptor:(id __strong *)captor];
}

@end

@interface OCMArgumentCaptureConstraint()

@end

@implementation OCMArgumentCaptureConstraint

- (id)initWithCaptor:(id __strong *)captor {
if (self = [super init]) {
_captor = captor;
}
return self;
}

- (BOOL)evaluate:(id)value {
if ([self isBlock:value]) {
*_captor = [value copy];
} else {
*_captor = value;
}
return YES;
}

- (BOOL)isBlock:(id)item {
BOOL isBlock = NO;

#if NS_BLOCKS_AVAILABLE
// find the block class at runtime in case it changes in a different OS version
static Class blockClass = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
id block = ^{};
blockClass = [block class];
while ([blockClass superclass] != [NSObject class]) {
blockClass = [blockClass superclass];
}
});

isBlock = [item isKindOfClass:blockClass];
#endif

return isBlock;
}
@end
1 change: 1 addition & 0 deletions Source/OCMock/OCMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
#import <OCMock/OCMConstraint.h>
#import <OCMock/OCMArg.h>
#import <OCMock/NSNotificationCenter+OCMAdditions.h>
#import <OCMock/OCMArgumentCaptureConstraint.h>