Created
August 7, 2014 16:12
-
-
Save lukeredpath/abada749cfdfdb879257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 1ad1e05f28a81cf7e2f3bc65d43b45b12e075742 Mon Sep 17 00:00:00 2001 | |
From: Luke Redpath <luke@lukeredpath.co.uk> | |
Date: Thu, 7 Aug 2014 17:09:50 +0100 | |
Subject: [PATCH] Allow protocol mocks to not implicitly handle optional | |
protocol methods. | |
--- | |
Source/OCMockito/MKTObjectAndProtocolMock.m | 2 +- | |
Source/OCMockito/MKTProtocolMock.h | 6 ++++-- | |
Source/OCMockito/MKTProtocolMock.m | 9 +++++---- | |
Source/OCMockito/OCMockito.h | 3 ++- | |
4 files changed, 12 insertions(+), 8 deletions(-) | |
diff --git a/Source/OCMockito/MKTObjectAndProtocolMock.m b/Source/OCMockito/MKTObjectAndProtocolMock.m | |
index 0862bc6..ef0b80a 100644 | |
--- a/Source/OCMockito/MKTObjectAndProtocolMock.m | |
+++ b/Source/OCMockito/MKTObjectAndProtocolMock.m | |
@@ -23,7 +23,7 @@ | |
- (instancetype)initWithClass:(Class)aClass protocol:(Protocol *)protocol | |
{ | |
- self = [super initWithProtocol:protocol]; | |
+ self = [super initWithProtocol:protocol implicitlyHandlesOptionalMethods:YES]; | |
if (self) | |
_mockedClass = aClass; | |
return self; | |
diff --git a/Source/OCMockito/MKTProtocolMock.h b/Source/OCMockito/MKTProtocolMock.h | |
index 1bbacfc..03cefe5 100644 | |
--- a/Source/OCMockito/MKTProtocolMock.h | |
+++ b/Source/OCMockito/MKTProtocolMock.h | |
@@ -17,7 +17,9 @@ | |
Protocol *_mockedProtocol; | |
} | |
-+ (instancetype)mockForProtocol:(Protocol *)aProtocol; | |
-- (instancetype)initWithProtocol:(Protocol *)aProtocol; | |
+@property (nonatomic, assign) BOOL implicitlyHandleOptionalMethods; | |
+ | |
++ (instancetype)mockForProtocol:(Protocol *)aProtocol implicitlyHandlesOptionalMethods:(BOOL)handleOptionals; | |
+- (instancetype)initWithProtocol:(Protocol *)aProtocol implicitlyHandlesOptionalMethods:(BOOL)handleOptionals; | |
@end | |
diff --git a/Source/OCMockito/MKTProtocolMock.m b/Source/OCMockito/MKTProtocolMock.m | |
index 6ec6d4d..c8d6bd4 100644 | |
--- a/Source/OCMockito/MKTProtocolMock.m | |
+++ b/Source/OCMockito/MKTProtocolMock.m | |
@@ -13,16 +13,17 @@ | |
@implementation MKTProtocolMock | |
-+ (instancetype)mockForProtocol:(Protocol *)aProtocol | |
++ (instancetype)mockForProtocol:(Protocol *)aProtocol implicitlyHandlesOptionalMethods:(BOOL)handleOptionals | |
{ | |
- return [[self alloc] initWithProtocol:aProtocol]; | |
+ return [[self alloc] initWithProtocol:aProtocol implicitlyHandlesOptionalMethods:handleOptionals]; | |
} | |
-- (instancetype)initWithProtocol:(Protocol *)aProtocol | |
+- (instancetype)initWithProtocol:(Protocol *)aProtocol implicitlyHandlesOptionalMethods:(BOOL)handleOptionals | |
{ | |
self = [super init]; | |
if (self) | |
_mockedProtocol = aProtocol; | |
+ _implicitlyHandleOptionalMethods = handleOptionals; | |
return self; | |
} | |
@@ -36,7 +37,7 @@ | |
{ | |
struct objc_method_description methodDescription = protocol_getMethodDescription(_mockedProtocol, aSelector, YES, YES); | |
if (!methodDescription.name) | |
- methodDescription = protocol_getMethodDescription(_mockedProtocol, aSelector, NO, YES); | |
+ methodDescription = protocol_getMethodDescription(_mockedProtocol, aSelector, !self.implicitlyHandleOptionalMethods, YES); | |
if (!methodDescription.name) | |
return nil; | |
return [NSMethodSignature signatureWithObjCTypes:methodDescription.types]; | |
diff --git a/Source/OCMockito/OCMockito.h b/Source/OCMockito/OCMockito.h | |
index a783a6a..395a76a 100644 | |
--- a/Source/OCMockito/OCMockito.h | |
+++ b/Source/OCMockito/OCMockito.h | |
@@ -42,7 +42,8 @@ | |
#endif | |
-#define MKTMockProtocol(aProtocol) (id)[MKTProtocolMock mockForProtocol:aProtocol] | |
+#define MKTMockProtocol(aProtocol) (id)[MKTProtocolMock mockForProtocol:aProtocol implicitlyHandlesOptionalMethods:YES] | |
+#define MKTMockProtocolWithOptionalMethods(aProtocol, handleOptionals) (id)[MKTProtocolMock mockForProtocol:aProtocol implicitlyHandlesOptionalMethods:handleOptionals] | |
/** | |
Returns a mock object implementing a given protocol. | |
-- | |
1.7.11.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment