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

Report reject macro failure properly #430

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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