Skip to content

Commit

Permalink
Allow injection of macro state class into OCM macros
Browse files Browse the repository at this point in the history
We have a need to inject a replacement for `OCMMacroState` for when we are doing cross
process testing using a library like https://github.com/google/EarlGrey/tree/earlgrey2.

This simple expansion to the basic macros will give us the flexibility we need to allow
us to use OCMock v3 syntax instead of falling back to OCMock v2 syntax when working
with EarlGrey tests.
  • Loading branch information
dmaclach committed Jan 16, 2021
1 parent 6358799 commit e2ee9bd
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions Source/OCMock/OCMockMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,68 +34,76 @@
#define OCMObserverMock() [OCMockObject observerMock]


#define OCMStub(invocation) \
#define OCMStubWithStateClass(MacroStateClass, invocation) \
({ \
_OCMSilenceWarnings( \
[OCMMacroState beginStubMacro]; \
[MacroStateClass beginStubMacro]; \
OCMStubRecorder *recorder = nil; \
@try{ \
invocation; \
}@catch(...){ \
[[OCMMacroState globalState] setInvocationDidThrow:YES]; \
[[MacroStateClass globalState] setInvocationDidThrow:YES]; \
/* NOLINTNEXTLINE(google-objc-avoid-throwing-exception) */ \
@throw; \
}@finally{ \
recorder = [OCMMacroState endStubMacro]; \
recorder = [MacroStateClass endStubMacro]; \
} \
recorder; \
); \
})

#define OCMExpect(invocation) \
#define OCMStub(invocation) OCMStubWithStateClass(OCMMacroState, invocation)

#define OCMExpectWithStateClass(MacroStateClass, invocation) \
({ \
_OCMSilenceWarnings( \
[OCMMacroState beginExpectMacro]; \
[MacroStateClass beginExpectMacro]; \
OCMStubRecorder *recorder = nil; \
@try{ \
invocation; \
}@catch(...){ \
[[OCMMacroState globalState] setInvocationDidThrow:YES]; \
[[MacroStateClass globalState] setInvocationDidThrow:YES]; \
/* NOLINTNEXTLINE(google-objc-avoid-throwing-exception) */ \
@throw; \
}@finally{ \
recorder = [OCMMacroState endExpectMacro]; \
recorder = [MacroStateClass endExpectMacro]; \
} \
recorder; \
); \
})

#define OCMReject(invocation) \
#define OCMExpect(invocation) OCMExpectWithStateClass(OCMMacroState, invocation)

#define OCMRejectWithStateClass(MacroStateClass, invocation) \
({ \
_OCMSilenceWarnings( \
[OCMMacroState beginRejectMacro]; \
[MacroStateClass beginRejectMacro]; \
OCMStubRecorder *recorder = nil; \
@try{ \
invocation; \
}@catch(...){ \
[[OCMMacroState globalState] setInvocationDidThrow:YES]; \
[[MacroStateClass globalState] setInvocationDidThrow:YES]; \
/* NOLINTNEXTLINE(google-objc-avoid-throwing-exception) */ \
@throw; \
}@finally{ \
recorder = [OCMMacroState endRejectMacro]; \
recorder = [MacroStateClass endRejectMacro]; \
} \
recorder; \
); \
})

#define OCMReject(invocation) OCMRejectWithStateClass(OCMMacroState, invocation)


#define OCMClassMethod(invocation) \

#define OCMClassMethodWithStateClass(MacroStateClass, invocation) \
_OCMSilenceWarnings( \
[[OCMMacroState globalState] switchToClassMethod]; \
[[MacroStateClass globalState] switchToClassMethod]; \
invocation; \
);

#define OCMClassMethod(invocation) OCMClassMethodWithStateClass(OCMMacroState, invocation)


#ifndef OCM_DISABLE_SHORT_SYNTAX
#define ClassMethod(invocation) OCMClassMethod(invocation)
Expand All @@ -106,38 +114,43 @@

#define OCMVerifyAllWithDelay(mock, delay) [(OCMockObject *)mock verifyWithDelay:delay atLocation:OCMMakeLocation(self, __FILE__, __LINE__)]

#define _OCMVerify(invocation) \
#define _OCMVerifyWithStateClass(MacroStateClass, invocation) \
({ \
_OCMSilenceWarnings( \
[OCMMacroState beginVerifyMacroAtLocation:OCMMakeLocation(self, __FILE__, __LINE__)]; \
[MacroStateClass beginVerifyMacroAtLocation:OCMMakeLocation(self, __FILE__, __LINE__)]; \
@try{ \
invocation; \
}@catch(...){ \
[[OCMMacroState globalState] setInvocationDidThrow:YES]; \
[[MacroStateClass globalState] setInvocationDidThrow:YES]; \
/* NOLINTNEXTLINE(google-objc-avoid-throwing-exception) */ \
@throw; \
}@finally{ \
[OCMMacroState endVerifyMacro]; \
[MacroStateClass endVerifyMacro]; \
} \
); \
})

#define _OCMVerifyWithQuantifier(quantifier, invocation) \
#define _OCMVerify(invocation) _OCMVerifyWithStateClass(OCMMacroState, invocation)


#define _OCMVerifyWithQuantifierAndStateClass(MacroStateClass, quantifier, invocation) \
({ \
_OCMSilenceWarnings( \
[OCMMacroState beginVerifyMacroAtLocation:OCMMakeLocation(self, __FILE__, __LINE__) withQuantifier:quantifier]; \
[MacroStateClass beginVerifyMacroAtLocation:OCMMakeLocation(self, __FILE__, __LINE__) withQuantifier:quantifier]; \
@try{ \
invocation; \
}@catch(...){ \
[[OCMMacroState globalState] setInvocationDidThrow:YES]; \
[[MacroStateClass globalState] setInvocationDidThrow:YES]; \
/* NOLINTNEXTLINE(google-objc-avoid-throwing-exception) */ \
@throw; \
}@finally{ \
[OCMMacroState endVerifyMacro]; \
[MacroStateClass endVerifyMacro]; \
} \
); \
})

#define _OCMVerifyWithQuantifier(quantifier, invocation) _OCMVerifyWithQuantifierAndStateClass(OCMMacroState, quantifier, invocation)

// explanation for macros below here: https://stackoverflow.com/questions/3046889/optional-parameters-with-c-macros

#define _OCMVerify_1(A) _OCMVerify(A)
Expand Down

0 comments on commit e2ee9bd

Please sign in to comment.