Skip to content

Commit df40c4b

Browse files
committed
Report reject macro failure properly
Previously when messaging OCMReject only an exception was raised and the failure was not associated with the line and file of declaration.
1 parent 5cf74ba commit df40c4b

File tree

8 files changed

+25
-7
lines changed

8 files changed

+25
-7
lines changed

Source/OCMock/OCMExpectationRecorder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
@interface OCMExpectationRecorder : OCMStubRecorder
2020

21+
@property(retain) OCMLocation *location;
22+
2123
- (id)never;
2224

2325
@end

Source/OCMock/OCMExpectationRecorder.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ - (OCMInvocationExpectation *)expectation
3939

4040
- (id)never
4141
{
42+
[[self expectation] setLocation:[self location]];
4243
[[self expectation] setMatchAndReject:YES];
4344
return self;
4445
}

Source/OCMock/OCMInvocationExpectation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616

1717
#import "OCMInvocationStub.h"
1818

19+
@class OCMLocation;
20+
1921
@interface OCMInvocationExpectation : OCMInvocationStub
2022
{
2123
BOOL matchAndReject;
2224
BOOL isSatisfied;
2325
}
2426

27+
@property(retain) OCMLocation *location;
28+
2529
- (void)setMatchAndReject:(BOOL)flag;
2630
- (BOOL)isMatchAndReject;
2731

Source/OCMock/OCMInvocationExpectation.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#import "OCMInvocationExpectation.h"
18+
#import "OCMFunctionsPrivate.h"
1819
#import "NSInvocation+OCMAdditions.h"
1920

2021

@@ -44,8 +45,9 @@ - (void)handleInvocation:(NSInvocation *)anInvocation
4445
if(matchAndReject)
4546
{
4647
isSatisfied = NO;
47-
[NSException raise:NSInternalInconsistencyException format:@"%@: explicitly disallowed method invoked: %@",
48-
[self description], [anInvocation invocationDescription]];
48+
NSString *description = [NSString stringWithFormat:@"%@: explicitly disallowed method invoked: %@",
49+
[self description], [anInvocation invocationDescription]];
50+
OCMReportFailure([self location], description);
4951
}
5052
else
5153
{

Source/OCMock/OCMMacroState.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
+ (void)beginExpectMacro;
3636
+ (OCMStubRecorder *)endExpectMacro;
3737

38-
+ (void)beginRejectMacro;
38+
+ (void)beginRejectMacroAtLocation:(OCMLocation *)aLocation;
3939
+ (OCMStubRecorder *)endRejectMacro;
4040

4141
+ (void)beginVerifyMacroAtLocation:(OCMLocation *)aLocation;

Source/OCMock/OCMMacroState.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ + (OCMStubRecorder *)endExpectMacro
6666
}
6767

6868

69-
+ (void)beginRejectMacro
69+
+ (void)beginRejectMacroAtLocation:(OCMLocation *)aLocation
7070
{
7171
OCMExpectationRecorder *recorder = [[[OCMExpectationRecorder alloc] init] autorelease];
72+
[recorder setLocation:aLocation];
73+
[recorder never];
7274
OCMMacroState *macroState = [[OCMMacroState alloc] initWithRecorder:recorder];
7375
[NSThread currentThread].threadDictionary[OCMGlobalStateKey] = macroState;
7476
[macroState release];

Source/OCMock/OCMock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
#define OCMReject(invocation) \
8383
({ \
8484
_OCMSilenceWarnings( \
85-
[OCMMacroState beginRejectMacro]; \
85+
[OCMMacroState beginRejectMacroAtLocation:OCMMakeLocation(self, __FILE__, __LINE__)]; \
8686
OCMStubRecorder *recorder = nil; \
8787
@try{ \
8888
invocation; \

Source/OCMockTests/OCMockObjectMacroTests.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,17 @@ - (void)testSetsUpReject
284284
{
285285
id mock = OCMClassMock([TestClassForMacroTesting class]);
286286

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

289289
XCTAssertNoThrow([mock verify], @"Should have accepted invocation rejected method not being invoked");
290-
XCTAssertThrows([mock stringValue], @"Should have complained during rejected method being invoked");
290+
291+
shouldCaptureFailure = YES;
292+
[mock stringValue];
293+
shouldCaptureFailure = NO;
294+
XCTAssertNotNil(reportedDescription, @"Should have recorded a failure with description.");
295+
XCTAssertEqualObjects([NSString stringWithUTF8String:expectedFile], reportedFile, @"Should have reported correct file.");
296+
XCTAssertEqual(expectedLine, (int)reportedLine, @"Should have reported correct line");
297+
291298
XCTAssertThrows([mock verify], @"Should have complained about rejected method being invoked");
292299
}
293300

0 commit comments

Comments
 (0)