Skip to content

Commit f916e9b

Browse files
committed
Merge branch 'piotr-tobolski-piotr-tobolski/ocmreject'
2 parents bf6f59a + e90d2cb commit f916e9b

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

Source/Changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ OCMock 3.3 (unreleased)
55

66
* Made the use of mock objects thread safe. You still have to setup the mocks
77
and verify them from a main thread (Ian Anderson)
8+
* Added modern syntax for reject (Piotr Tobolski)
89

910

1011
OCMock 3.2.2 (2016-01-20)

Source/OCMock/OCMMacroState.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
+ (void)beginExpectMacro;
3434
+ (OCMStubRecorder *)endExpectMacro;
3535

36+
+ (void)beginRejectMacro;
37+
+ (OCMStubRecorder *)endRejectMacro;
38+
3639
+ (void)beginVerifyMacroAtLocation:(OCMLocation *)aLocation;
3740
+ (void)endVerifyMacro;
3841

Source/OCMock/OCMMacroState.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ + (OCMStubRecorder *)endExpectMacro
6060
}
6161

6262

63+
+ (void)beginRejectMacro
64+
{
65+
OCMExpectationRecorder *recorder = [[[OCMExpectationRecorder alloc] init] autorelease];
66+
[recorder never];
67+
OCMMacroState *macroState = [[OCMMacroState alloc] initWithRecorder:recorder];
68+
[NSThread currentThread].threadDictionary[OCMGlobalStateKey] = macroState;
69+
[macroState release];
70+
}
71+
72+
+ (OCMStubRecorder *)endRejectMacro
73+
{
74+
return [self endStubMacro];
75+
}
76+
77+
6378
+ (void)beginVerifyMacroAtLocation:(OCMLocation *)aLocation
6479
{
6580
OCMVerifier *recorder = [[[OCMVerifier alloc] init] autorelease];

Source/OCMock/OCMock.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@
6666
); \
6767
})
6868

69+
#define OCMReject(invocation) \
70+
({ \
71+
_OCMSilenceWarnings( \
72+
[OCMMacroState beginRejectMacro]; \
73+
OCMStubRecorder *recorder = nil; \
74+
@try{ \
75+
invocation; \
76+
}@finally{ \
77+
recorder = [OCMMacroState endRejectMacro]; \
78+
} \
79+
recorder; \
80+
); \
81+
})
82+
6983
#define ClassMethod(invocation) \
7084
_OCMSilenceWarnings( \
7185
[[OCMMacroState globalState] switchToClassMethod]; \

Source/OCMockTests/OCMockObjectMacroTests.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,18 @@ - (void)testSetsUpExpectations
268268
}
269269

270270

271+
- (void)testSetsUpReject
272+
{
273+
id mock = OCMClassMock([TestClassForMacroTesting class]);
274+
275+
OCMReject([mock stringValue]);
276+
277+
XCTAssertNoThrow([mock verify], @"Should have accepted invocation rejected method not being invoked");
278+
XCTAssertThrows([mock stringValue], @"Should have complained during rejected method being invoked");
279+
XCTAssertThrows([mock verify], @"Should have complained about rejected method being invoked");
280+
}
281+
282+
271283
- (void)testShouldNotReportErrorWhenMethodWasInvoked
272284
{
273285
id mock = OCMClassMock([NSString class]);

0 commit comments

Comments
 (0)