Skip to content

Commit 31c0871

Browse files
committed
Merge branch 'dmaclach-exception_passer'
2 parents 90693d3 + 5b3407d commit 31c0871

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

Source/OCMock/OCMMacroState.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
@interface OCMMacroState : NSObject
2727
{
2828
id recorder;
29+
BOOL invocationDidThrow;
2930
}
3031

3132
+ (void)beginStubMacro;
@@ -48,4 +49,7 @@
4849

4950
- (void)switchToClassMethod;
5051

52+
- (void)setInvocationDidThrow:(BOOL)flag;
53+
- (BOOL)invocationDidThrow;
54+
5155
@end

Source/OCMock/OCMMacroState.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ + (OCMStubRecorder *)endStubMacro
3838
NSMutableDictionary *threadDictionary = [NSThread currentThread].threadDictionary;
3939
OCMMacroState *globalState = threadDictionary[OCMGlobalStateKey];
4040
OCMStubRecorder *recorder = [[(OCMStubRecorder *)[globalState recorder] retain] autorelease];
41+
BOOL didThrow = [globalState invocationDidThrow];
4142
[threadDictionary removeObjectForKey:OCMGlobalStateKey];
42-
if([recorder wasUsed] == NO)
43+
if(didThrow == NO && [recorder wasUsed] == NO)
4344
{
4445
[NSException raise:NSInternalInconsistencyException
4546
format:@"Did not record an invocation in OCMStub/OCMExpect/OCMReject.\n"
@@ -103,8 +104,9 @@ + (void)endVerifyMacro
103104
NSMutableDictionary *threadDictionary = [NSThread currentThread].threadDictionary;
104105
OCMMacroState *globalState = threadDictionary[OCMGlobalStateKey];
105106
OCMVerifier *verifier = [[(OCMVerifier *)[globalState recorder] retain] autorelease];
107+
BOOL didThrow = [globalState invocationDidThrow];
106108
[threadDictionary removeObjectForKey:OCMGlobalStateKey];
107-
if([verifier wasUsed] == NO)
109+
if(didThrow == NO && [verifier wasUsed] == NO)
108110
{
109111
[NSException raise:NSInternalInconsistencyException
110112
format:@"Did not record an invocation in OCMVerify.\n"
@@ -127,7 +129,7 @@ + (OCMMacroState *)globalState
127129

128130
- (id)initWithRecorder:(OCMRecorder *)aRecorder
129131
{
130-
if ((self = [super init]))
132+
if((self = [super init]))
131133
{
132134
recorder = [aRecorder retain];
133135
}
@@ -153,6 +155,16 @@ - (OCMRecorder *)recorder
153155
return recorder;
154156
}
155157

158+
- (void)setInvocationDidThrow:(BOOL)flag
159+
{
160+
invocationDidThrow = flag;
161+
}
162+
163+
- (BOOL)invocationDidThrow
164+
{
165+
return invocationDidThrow;
166+
}
167+
156168

157169
#pragma mark Changing the recorder
158170

Source/OCMock/OCMVerifier.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ @implementation OCMVerifier
2323

2424
- (id)init
2525
{
26+
if(invocationMatcher != nil)
27+
[NSException raise:NSInternalInconsistencyException format:@"** Method init invoked twice on verifier. Are you trying to verify the init method? This is currently not supported."];
2628
if ((self = [super init]))
2729
{
2830
invocationMatcher = [[OCMInvocationMatcher alloc] init];

Source/OCMock/OCMock.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
OCMStubRecorder *recorder = nil; \
5353
@try{ \
5454
invocation; \
55+
}@catch(...){ \
56+
[[OCMMacroState globalState] setInvocationDidThrow:YES]; \
57+
@throw; \
5558
}@finally{ \
5659
recorder = [OCMMacroState endStubMacro]; \
5760
} \
@@ -66,6 +69,9 @@
6669
OCMStubRecorder *recorder = nil; \
6770
@try{ \
6871
invocation; \
72+
}@catch(...){ \
73+
[[OCMMacroState globalState] setInvocationDidThrow:YES]; \
74+
@throw; \
6975
}@finally{ \
7076
recorder = [OCMMacroState endExpectMacro]; \
7177
} \
@@ -80,6 +86,9 @@
8086
OCMStubRecorder *recorder = nil; \
8187
@try{ \
8288
invocation; \
89+
}@catch(...){ \
90+
[[OCMMacroState globalState] setInvocationDidThrow:YES]; \
91+
@throw; \
8392
}@finally{ \
8493
recorder = [OCMMacroState endRejectMacro]; \
8594
} \
@@ -111,6 +120,9 @@
111120
[OCMMacroState beginVerifyMacroAtLocation:OCMMakeLocation(self, __FILE__, __LINE__)]; \
112121
@try{ \
113122
invocation; \
123+
}@catch(...){ \
124+
[[OCMMacroState globalState] setInvocationDidThrow:YES]; \
125+
@throw; \
114126
}@finally{ \
115127
[OCMMacroState endVerifyMacro]; \
116128
} \
@@ -123,6 +135,9 @@
123135
[OCMMacroState beginVerifyMacroAtLocation:OCMMakeLocation(self, __FILE__, __LINE__) withQuantifier:quantifier]; \
124136
@try{ \
125137
invocation; \
138+
}@catch(...){ \
139+
[[OCMMacroState globalState] setInvocationDidThrow:YES]; \
140+
@throw; \
126141
}@finally{ \
127142
[OCMMacroState endVerifyMacro]; \
128143
} \

Source/OCMockTests/OCMockObjectMacroTests.m

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,49 @@ - (void)testReturnsCorrectObjectFromInitMethodCalledOnRecorderInsideMacro
503503
OCMStub([[[[mock ignoringNonObjectArgs] andReturn:nil] andThrow:nil] initWithString:OCMOCK_ANY]);
504504
}
505505

506+
- (void)testStubMacroPassesExceptionThrough
507+
{
508+
id mock = OCMClassMock([TestClassForMacroTesting class]);
509+
@try
510+
{
511+
OCMStub([mock init]).andReturn(mock);
512+
XCTFail(@"An exception should have been thrown.");
513+
}
514+
@catch(NSException *exception)
515+
{
516+
XCTAssertEqualObjects(exception.name, NSInternalInconsistencyException);
517+
XCTAssertTrue([exception.reason containsString:@"Method init invoked twice on stub recorder"]);
518+
}
519+
}
520+
521+
- (void)testExpectMacroPassesExceptionThrough
522+
{
523+
id mock = OCMClassMock([TestClassForMacroTesting class]);
524+
@try
525+
{
526+
OCMExpect([mock init]).andReturn(mock);
527+
XCTFail(@"An exception should have been thrown.");
528+
}
529+
@catch(NSException *exception)
530+
{
531+
XCTAssertEqualObjects(exception.name, NSInternalInconsistencyException);
532+
XCTAssertTrue([exception.reason containsString:@"Method init invoked twice on stub recorder"]);
533+
}
534+
}
535+
536+
- (void)testVerifyMacroPassExceptionsThrough
537+
{
538+
id mock = OCMClassMock([TestClassForMacroTesting class]);
539+
@try
540+
{
541+
OCMVerify([mock init]);
542+
XCTFail(@"An exception should have been thrown.");
543+
}
544+
@catch(NSException *exception)
545+
{
546+
XCTAssertEqualObjects(exception.name, NSInternalInconsistencyException);
547+
XCTAssertTrue([exception.reason containsString:@"Method init invoked twice on verifier"]);
548+
}
549+
}
550+
506551
@end

0 commit comments

Comments
 (0)