Skip to content

Commit

Permalink
Added ability to disable "verify after run" for specific mocks as a w…
Browse files Browse the repository at this point in the history
…orkaround for runtime performance issue (erikdoe#253).
  • Loading branch information
BohdanOrlov committed Feb 17, 2016
1 parent ee7ec22 commit c6d4e6c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Source/OCMock/OCClassMockObject.m
Expand Up @@ -118,7 +118,8 @@ - (void)prepareClassForClassMethodMocking
IMP myForwardIMP = method_getImplementation(myForwardMethod);
class_addMethod(newMetaClass, @selector(forwardInvocation:), myForwardIMP, method_getTypeEncoding(myForwardMethod));


if (self->verifyAfterRunDisabled)
return;
/* adding forwarder for most class methods (instance methods on meta class) to allow for verify after run */
NSArray *methodBlackList = @[@"class", @"forwardingTargetForSelector:", @"methodSignatureForSelector:", @"forwardInvocation:", @"isBlock",
@"instanceMethodForwarderForSelector:", @"instanceMethodSignatureForSelector:"];
Expand Down
4 changes: 4 additions & 0 deletions Source/OCMock/OCMockObject.h
Expand Up @@ -27,6 +27,7 @@
{
BOOL isNice;
BOOL expectationOrderMatters;
BOOL verifyAfterRunDisabled;
NSMutableArray *stubs;
NSMutableArray *expectations;
NSMutableArray *exceptions;
Expand All @@ -42,6 +43,9 @@

+ (id)observerMock;

+ (BOOL)verifyAfterRunDisabledForNewMocks;
+ (void)setVerifyAfterRunDisabledForNewMocks:(BOOL)flag;

- (instancetype)init;

- (void)setExpectationOrderMatters:(BOOL)flag;
Expand Down
17 changes: 17 additions & 0 deletions Source/OCMock/OCMockObject.m
Expand Up @@ -33,6 +33,8 @@

@implementation OCMockObject

static BOOL globalVerifyAfterRunDisabled;

#pragma mark Class initialisation

+ (void)initialize
Expand Down Expand Up @@ -83,12 +85,23 @@ + (id)observerMock
return [[[OCObserverMockObject alloc] init] autorelease];
}

+ (BOOL)verifyAfterRunDisabledForNewMocks
{
return globalVerifyAfterRunDisabled;
}

+ (void)setVerifyAfterRunDisabledForNewMocks:(BOOL)flag
{
globalVerifyAfterRunDisabled = flag;
}


#pragma mark Initialisers, description, accessors, etc.

- (instancetype)init
{
// no [super init], we're inheriting from NSProxy
verifyAfterRunDisabled = globalVerifyAfterRunDisabled;
expectationOrderMatters = NO;
stubs = [[NSMutableArray alloc] init];
expectations = [[NSMutableArray alloc] init];
Expand Down Expand Up @@ -218,6 +231,10 @@ - (void)verifyInvocation:(OCMInvocationMatcher *)matcher

- (void)verifyInvocation:(OCMInvocationMatcher *)matcher atLocation:(OCMLocation *)location
{
if (self->verifyAfterRunDisabled) {
OCMReportFailure(location, [NSString stringWithFormat:@"%@: Verify after run was disabled", [self description]]);
return;
}
for(NSInvocation *invocation in invocations)
{
if([matcher matchesInvocation:invocation])
Expand Down
2 changes: 2 additions & 0 deletions Source/OCMock/OCPartialMockObject.m
Expand Up @@ -127,6 +127,8 @@ - (void)prepareObjectForInstanceMethodMocking
IMP myObjectClassImp = method_getImplementation(myObjectClassMethod);
class_addMethod(subclass, @selector(class), myObjectClassImp, objectClassTypes);

if (self->verifyAfterRunDisabled)
return;
/* Adding forwarder for most instance methods to allow for verify after run */
NSArray *methodBlackList = @[@"class", @"forwardingTargetForSelector:", @"methodSignatureForSelector:", @"forwardInvocation:",
@"allowsWeakReference", @"retainWeakReference", @"isBlock"];
Expand Down
25 changes: 24 additions & 1 deletion Source/OCMockTests/OCMockObjectVerifyAfterRunTests.m
Expand Up @@ -114,7 +114,30 @@ - (void)testThrowsWhenClassMethodWasNotInvoked
{
id mock = [OCMockObject niceMockForClass:[TestBaseClassForVerifyAfterRun class]];

XCTAssertThrows([[mock verify] classMethod1], @"Should not have thrown an exception for class method that was called.");
XCTAssertThrows([[mock verify] classMethod1], @"Should have thrown an exception for class method that was called.");
}

- (void)testThrowsWhenClassMethodWasInvokedOnMockWithDisabledVerifyAfterRun
{
[OCMockObject setVerifyAfterRunDisabledForNewMocks:YES];
id mock = [OCMockObject niceMockForClass:[NSString class]];
[OCMockObject setVerifyAfterRunDisabledForNewMocks:NO];

[mock lowercaseString];

XCTAssertThrows([[mock verify] lowercaseString], @"Should have thrown an exception due to disabled verify after run");
}

- (void)testThrowWhenMethodWasInvokedOnPartialMockWithDisabledVerifyAfterRun
{
[OCMockObject setVerifyAfterRunDisabledForNewMocks:YES];
TestClassForVerifyAfterRun *testObject = [[TestClassForVerifyAfterRun alloc] init];
id mock = [OCMockObject partialMockForObject:testObject];
[OCMockObject setVerifyAfterRunDisabledForNewMocks:NO];

[mock method2];

XCTAssertThrows([[mock verify] method2], @"Should have thrown an exception due to disabled verify after run");
}

@end

0 comments on commit c6d4e6c

Please sign in to comment.