Skip to content

Commit

Permalink
Add support for .andFulfill(expectation)
Browse files Browse the repository at this point in the history
Suggested implementation for #420
  • Loading branch information
dmaclach committed May 24, 2020
1 parent 9b0e314 commit fddd11c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Source/OCMock.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
03C7BF11195DAB5300A545DD /* OCMExpectationRecorder.h in Headers */ = {isa = PBXBuildFile; fileRef = 03C7BF0E195DAB5300A545DD /* OCMExpectationRecorder.h */; };
03C7BF12195DAB5300A545DD /* OCMExpectationRecorder.m in Sources */ = {isa = PBXBuildFile; fileRef = 03C7BF0F195DAB5300A545DD /* OCMExpectationRecorder.m */; };
03C7BF13195DAB5300A545DD /* OCMExpectationRecorder.m in Sources */ = {isa = PBXBuildFile; fileRef = 03C7BF0F195DAB5300A545DD /* OCMExpectationRecorder.m */; };
03C7BF1619606E7A00A545DD /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03565A1D18F05626003AE91E /* XCTest.framework */; };
03C7BF1719606EFD00A545DD /* OCMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 030EF0A814632FD000B04273 /* OCMock.framework */; };
03C9CA1D18F05A75006DF94D /* OCMockObjectProtocolMocksTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2FA286BFBD8B9D068B41E7EF /* OCMockObjectProtocolMocksTests.m */; };
03C9CA1E18F05A84006DF94D /* OCMArgTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2FA28EDBF243639C57F88A1B /* OCMArgTests.m */; };
Expand Down Expand Up @@ -281,6 +280,12 @@
817EB1661BD7674D0047E85A /* OCMFunctionsPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 03F370CA1BAA1DE800CAD3E8 /* OCMFunctionsPrivate.h */; };
8BF73E53246CA75E00B9A52C /* OCMNoEscapeBlockTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BF73E52246CA75E00B9A52C /* OCMNoEscapeBlockTests.m */; settings = {COMPILER_FLAGS = "-Xclang -fexperimental-optimized-noescape"; }; };
8BF73E54246CA75E00B9A52C /* OCMNoEscapeBlockTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BF73E52246CA75E00B9A52C /* OCMNoEscapeBlockTests.m */; settings = {COMPILER_FLAGS = "-Xclang -fexperimental-optimized-noescape"; }; };
8BF7401E24772AEA00B9A52C /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03565A1D18F05626003AE91E /* XCTest.framework */; };
8BF7402024772B0500B9A52C /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03565A1D18F05626003AE91E /* XCTest.framework */; };
8BF7402124772B0600B9A52C /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03565A1D18F05626003AE91E /* XCTest.framework */; };
8BF7402224772B0800B9A52C /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03565A1D18F05626003AE91E /* XCTest.framework */; };
8BF7402324772B0800B9A52C /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03565A1D18F05626003AE91E /* XCTest.framework */; };
8BF7402424772B0900B9A52C /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03565A1D18F05626003AE91E /* XCTest.framework */; };
8DE97C5522B43EE60098C63F /* OCMockObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 03B3159E146333BF0052CD09 /* OCMockObject.m */; };
8DE97C5622B43EE60098C63F /* OCClassMockObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 03B3158C146333BF0052CD09 /* OCClassMockObject.m */; };
8DE97C5722B43EE60098C63F /* OCPartialMockObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 03B315AA146333BF0052CD09 /* OCPartialMockObject.m */; };
Expand Down Expand Up @@ -589,22 +594,24 @@
buildActionMask = 2147483647;
files = (
033E1FF414FEF5E0004456B0 /* Foundation.framework in Frameworks */,
8BF7402124772B0600B9A52C /* XCTest.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
030EF0D914632FF700B04273 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
8BF7401E24772AEA00B9A52C /* XCTest.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
03565A2E18F0566E003AE91E /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
03C7BF1619606E7A00A545DD /* XCTest.framework in Frameworks */,
03C7BF1719606EFD00A545DD /* OCMock.framework in Frameworks */,
8BF7402024772B0500B9A52C /* XCTest.framework in Frameworks */,
03CED2E32390770C001845CC /* OCHamcrest.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -614,6 +621,7 @@
buildActionMask = 2147483647;
files = (
817EB1391BD765130047E85A /* Foundation.framework in Frameworks */,
8BF7402324772B0800B9A52C /* XCTest.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -622,6 +630,7 @@
buildActionMask = 2147483647;
files = (
8DE97C7722B43EE60098C63F /* Foundation.framework in Frameworks */,
8BF7402424772B0900B9A52C /* XCTest.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -638,6 +647,7 @@
buildActionMask = 2147483647;
files = (
F0B9510B1B0080D500942C38 /* Foundation.framework in Frameworks */,
8BF7402224772B0800B9A52C /* XCTest.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
6 changes: 6 additions & 0 deletions Source/OCMock/OCMStubRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#import <OCMock/OCMFunctions.h>
#import <objc/runtime.h>

@class XCTestExpectation;

@interface OCMStubRecorder : OCMRecorder

- (id)andReturn:(id)anObject;
Expand All @@ -27,6 +29,7 @@
- (id)andCall:(SEL)selector onObject:(id)anObject;
- (id)andDo:(void (^)(NSInvocation *invocation))block;
- (id)andForwardToRealObject;
- (id)andFulfill:(XCTestExpectation *)expectation;

@end

Expand Down Expand Up @@ -58,6 +61,9 @@
#define andForwardToRealObject() _andForwardToRealObject()
@property (nonatomic, readonly) OCMStubRecorder *(^ _andForwardToRealObject)(void);

#define andFulfill(anExpectation) _andFulfill(anExpectation)
@property(nonatomic, readonly) OCMStubRecorder * (^_andFulfill)(XCTestExpectation *);

@property (nonatomic, readonly) OCMStubRecorder *(^ _ignoringNonObjectArgs)(void);

@end
Expand Down
17 changes: 17 additions & 0 deletions Source/OCMock/OCMStubRecorder.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import "OCMRealObjectForwarder.h"
#import "OCMInvocationStub.h"

#import <XCTest/XCTest.h>

@implementation OCMStubRecorder

Expand Down Expand Up @@ -99,6 +100,12 @@ - (id)andForwardToRealObject
return self;
}

- (id)andFulfill:(XCTestExpectation *)expectation {
return [self andDo:^(NSInvocation *invocation) {
[expectation fulfill];
}];
}


#pragma mark Finishing recording

Expand Down Expand Up @@ -194,5 +201,15 @@ @implementation OCMStubRecorder (Properties)
return [[theBlock copy] autorelease];
}

@dynamic _andFulfill;

- (OCMStubRecorder * (^)(XCTestExpectation *))_andFulfill {
id (^theBlock)(XCTestExpectation *) = ^ (XCTestExpectation *expectation)
{
return [self andFulfill:expectation];
};
return [[theBlock copy] autorelease];
}


@end
10 changes: 10 additions & 0 deletions Source/OCMockTests/OCMockObjectMacroTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ - (void)testSetsUpSubstituteCall
XCTAssertEqualObjects([mock stringValue], @"TEST_STRING_FROM_TESTCASE", @"Should have called method from test case");
}

- (void)testFulfillsExpectation
{
id mock = OCMStrictClassMock([NSString class]);

OCMStub([mock boolValue]).andFulfill([self expectationWithDescription:@"Expectation Called"]);
[mock boolValue];
[self waitForExpectationsWithTimeout:0 handler:nil];
}

- (NSString *)stringValueForTesting
{
return @"TEST_STRING_FROM_TESTCASE";
Expand Down Expand Up @@ -493,6 +502,7 @@ - (void)testReturnsCorrectObjectFromInitMethodCalledOnRecorderInsideMacro
OCMStub([[mock andThrow:nil] initWithString:OCMOCK_ANY]);
OCMStub([[mock andPost:nil] initWithString:OCMOCK_ANY]);
OCMStub([[mock andCall:nil onObject:nil] initWithString:OCMOCK_ANY]);
OCMStub([[mock andFulfill:nil] initWithString:OCMOCK_ANY]);
OCMStub([[mock andDo:nil] initWithString:OCMOCK_ANY]);
OCMStub([[mock andForwardToRealObject] initWithString:OCMOCK_ANY]);
OCMExpect([[mock never] initWithString:OCMOCK_ANY]);
Expand Down

0 comments on commit fddd11c

Please sign in to comment.