Skip to content

Commit

Permalink
Report reject macro failure properly
Browse files Browse the repository at this point in the history
Previously when messaging OCMReject only an exception was raised and the
failure was not associated with the line and file of declaration.
  • Loading branch information
tzvist committed May 30, 2020
1 parent 5cf74ba commit 65f58cd
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Source/OCMock/OCMExpectationRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

@interface OCMExpectationRecorder : OCMStubRecorder

@property(retain) OCMLocation *location;

- (id)never;

@end
6 changes: 6 additions & 0 deletions Source/OCMock/OCMExpectationRecorder.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ - (OCMInvocationExpectation *)expectation

- (id)never
{
[[self expectation] setLocation:[self location]];
[[self expectation] setMatchAndReject:YES];
return self;
}
Expand All @@ -52,5 +53,10 @@ - (void)forwardInvocation:(NSInvocation *)anInvocation
[mockObject addExpectation:[self expectation]];
}

- (void)dealloc
{
[_location release];
[super dealloc];
}

@end
4 changes: 4 additions & 0 deletions Source/OCMock/OCMInvocationExpectation.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

#import "OCMInvocationStub.h"

@class OCMLocation;

@interface OCMInvocationExpectation : OCMInvocationStub
{
BOOL matchAndReject;
BOOL isSatisfied;
}

@property(retain) OCMLocation *location;

- (void)setMatchAndReject:(BOOL)flag;
- (BOOL)isMatchAndReject;

Expand Down
12 changes: 10 additions & 2 deletions Source/OCMock/OCMInvocationExpectation.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#import "OCMInvocationExpectation.h"
#import "OCMFunctionsPrivate.h"
#import "NSInvocation+OCMAdditions.h"


Expand Down Expand Up @@ -44,13 +45,20 @@ - (void)handleInvocation:(NSInvocation *)anInvocation
if(matchAndReject)
{
isSatisfied = NO;
[NSException raise:NSInternalInconsistencyException format:@"%@: explicitly disallowed method invoked: %@",
[self description], [anInvocation invocationDescription]];
NSString *description = [NSString stringWithFormat:@"%@: explicitly disallowed method invoked: %@",
[self description], [anInvocation invocationDescription]];
OCMReportFailure([self location], description);
}
else
{
isSatisfied = YES;
}
}

- (void)dealloc
{
[_location release];
[super dealloc];
}

@end
2 changes: 1 addition & 1 deletion Source/OCMock/OCMMacroState.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
+ (void)beginExpectMacro;
+ (OCMStubRecorder *)endExpectMacro;

+ (void)beginRejectMacro;
+ (void)beginRejectMacroAtLocation:(OCMLocation *)aLocation;
+ (OCMStubRecorder *)endRejectMacro;

+ (void)beginVerifyMacroAtLocation:(OCMLocation *)aLocation;
Expand Down
4 changes: 3 additions & 1 deletion Source/OCMock/OCMMacroState.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ + (OCMStubRecorder *)endExpectMacro
}


+ (void)beginRejectMacro
+ (void)beginRejectMacroAtLocation:(OCMLocation *)aLocation
{
OCMExpectationRecorder *recorder = [[[OCMExpectationRecorder alloc] init] autorelease];
[recorder setLocation:aLocation];
[recorder never];
OCMMacroState *macroState = [[OCMMacroState alloc] initWithRecorder:recorder];
[NSThread currentThread].threadDictionary[OCMGlobalStateKey] = macroState;
[macroState release];
Expand Down
2 changes: 1 addition & 1 deletion Source/OCMock/OCMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
#define OCMReject(invocation) \
({ \
_OCMSilenceWarnings( \
[OCMMacroState beginRejectMacro]; \
[OCMMacroState beginRejectMacroAtLocation:OCMMakeLocation(self, __FILE__, __LINE__)]; \
OCMStubRecorder *recorder = nil; \
@try{ \
invocation; \
Expand Down
11 changes: 9 additions & 2 deletions Source/OCMockTests/OCMockObjectMacroTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,17 @@ - (void)testSetsUpReject
{
id mock = OCMClassMock([TestClassForMacroTesting class]);

OCMReject([mock stringValue]);
OCMReject([mock stringValue]); const char *expectedFile = __FILE__; int expectedLine = __LINE__;

XCTAssertNoThrow([mock verify], @"Should have accepted invocation rejected method not being invoked");
XCTAssertThrows([mock stringValue], @"Should have complained during rejected method being invoked");

shouldCaptureFailure = YES;
[mock stringValue];
shouldCaptureFailure = NO;
XCTAssertNotNil(reportedDescription, @"Should have recorded a failure with description.");
XCTAssertEqualObjects([NSString stringWithUTF8String:expectedFile], reportedFile, @"Should have reported correct file.");
XCTAssertEqual(expectedLine, (int)reportedLine, @"Should have reported correct line");

XCTAssertThrows([mock verify], @"Should have complained about rejected method being invoked");
}

Expand Down

0 comments on commit 65f58cd

Please sign in to comment.