diff --git a/Source/OCMock.xcodeproj/project.pbxproj b/Source/OCMock.xcodeproj/project.pbxproj index 4d7e0413..868eb9f9 100644 --- a/Source/OCMock.xcodeproj/project.pbxproj +++ b/Source/OCMock.xcodeproj/project.pbxproj @@ -7,6 +7,10 @@ objects = { /* Begin PBXBuildFile section */ + 012412861D5A0B31003B75B8 /* OCProtocolsProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 012412851D5A0B28003B75B8 /* OCProtocolsProxy.m */; }; + 012412871D5A0B32003B75B8 /* OCProtocolsProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 012412851D5A0B28003B75B8 /* OCProtocolsProxy.m */; }; + 012412881D5A0B32003B75B8 /* OCProtocolsProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 012412851D5A0B28003B75B8 /* OCProtocolsProxy.m */; }; + 012412891D5A0B32003B75B8 /* OCProtocolsProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 012412851D5A0B28003B75B8 /* OCProtocolsProxy.m */; }; 030EF0B614632FD000B04273 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 030EF0B414632FD000B04273 /* InfoPlist.strings */; }; 031E50581BB4A56300E257C3 /* OCMBoxedReturnValueProviderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 031E50571BB4A56300E257C3 /* OCMBoxedReturnValueProviderTests.m */; }; 031E50591BB4A56300E257C3 /* OCMBoxedReturnValueProviderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 031E50571BB4A56300E257C3 /* OCMBoxedReturnValueProviderTests.m */; }; @@ -346,6 +350,9 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 012412841D5A0B28003B75B8 /* OCProtocolsProxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OCProtocolsProxy.h; sourceTree = ""; }; + 012412851D5A0B28003B75B8 /* OCProtocolsProxy.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OCProtocolsProxy.m; sourceTree = ""; }; + 019DD2401D6F4753001055C3 /* TestProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestProtocol.h; sourceTree = ""; }; 030EF0A814632FD000B04273 /* OCMock.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OCMock.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 030EF0B314632FD000B04273 /* OCMock-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "OCMock-Info.plist"; sourceTree = ""; }; 030EF0B514632FD000B04273 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; @@ -598,6 +605,7 @@ 03565A3318F0566F003AE91E /* OCMockTests */ = { isa = PBXGroup; children = ( + 019DD2401D6F4753001055C3 /* TestProtocol.h */, 2FA2822E19948FC997965267 /* OCMockObjectTests.m */, 03AC5C1416DF9FA500D82ECD /* OCMockObjectPartialMocksTests.m */, 039F91C516EFB493006C3D70 /* OCMockObjectClassMethodMockingTests.m */, @@ -660,6 +668,8 @@ 03B31619146334040052CD09 /* Invocation Actions */, 03B3161A146334320052CD09 /* Argument Constraints and Actions */, 037ECD5618FB0D2E00AF0E4C /* Helper */, + 012412841D5A0B28003B75B8 /* OCProtocolsProxy.h */, + 012412851D5A0B28003B75B8 /* OCProtocolsProxy.m */, ); name = "Core Mocks"; sourceTree = ""; @@ -1182,6 +1192,7 @@ 03B315E3146333BF0052CD09 /* OCMNotificationPoster.m in Sources */, 03B315E8146333BF0052CD09 /* OCMObserverRecorder.m in Sources */, 03B315ED146333C00052CD09 /* OCMockObject.m in Sources */, + 012412861D5A0B31003B75B8 /* OCProtocolsProxy.m in Sources */, 03B315F2146333C00052CD09 /* OCMStubRecorder.m in Sources */, 03B315F7146333C00052CD09 /* OCMPassByRefSetter.m in Sources */, 03B315FC146333C00052CD09 /* OCMRealObjectForwarder.m in Sources */, @@ -1222,6 +1233,7 @@ 03B315E0146333BF0052CD09 /* OCMIndirectReturnValueProvider.m in Sources */, 03B315E5146333BF0052CD09 /* OCMNotificationPoster.m in Sources */, 03B315EA146333BF0052CD09 /* OCMObserverRecorder.m in Sources */, + 012412871D5A0B32003B75B8 /* OCProtocolsProxy.m in Sources */, 03B315EF146333C00052CD09 /* OCMockObject.m in Sources */, 03B315F4146333C00052CD09 /* OCMStubRecorder.m in Sources */, 03B315F9146333C00052CD09 /* OCMPassByRefSetter.m in Sources */, @@ -1288,6 +1300,7 @@ 817EB1241BD765130047E85A /* OCMBoxedReturnValueProvider.m in Sources */, 817EB1251BD765130047E85A /* OCMExceptionReturnValueProvider.m in Sources */, 817EB1261BD765130047E85A /* OCMIndirectReturnValueProvider.m in Sources */, + 012412891D5A0B32003B75B8 /* OCProtocolsProxy.m in Sources */, 817EB1271BD765130047E85A /* OCMNotificationPoster.m in Sources */, 817EB1281BD765130047E85A /* OCMReturnValueProvider.m in Sources */, 817EB1291BD765130047E85A /* OCMLocation.m in Sources */, @@ -1353,6 +1366,7 @@ F0B951191B0080EC00942C38 /* OCMBoxedReturnValueProvider.m in Sources */, F0B9511A1B0080EC00942C38 /* OCMExceptionReturnValueProvider.m in Sources */, F0B9511B1B0080EC00942C38 /* OCMIndirectReturnValueProvider.m in Sources */, + 012412881D5A0B32003B75B8 /* OCProtocolsProxy.m in Sources */, F0B9511C1B0080EC00942C38 /* OCMNotificationPoster.m in Sources */, F0B9511D1B0080EC00942C38 /* OCMReturnValueProvider.m in Sources */, F0B9511E1B0080EC00942C38 /* OCMLocation.m in Sources */, diff --git a/Source/OCMock/OCClassMockObject.h b/Source/OCMock/OCClassMockObject.h index 29c67f1a..3a9d7390 100644 --- a/Source/OCMock/OCClassMockObject.h +++ b/Source/OCMock/OCClassMockObject.h @@ -22,7 +22,7 @@ Class originalMetaClass; } -- (id)initWithClass:(Class)aClass; +- (id)initWithClass:(Class)aClass protocols:(NSArray *)protocols; - (Class)mockedClass; - (Class)mockObjectClass; // since -class returns the mockedClass diff --git a/Source/OCMock/OCClassMockObject.m b/Source/OCMock/OCClassMockObject.m index 8770a632..e60ca875 100644 --- a/Source/OCMock/OCClassMockObject.m +++ b/Source/OCMock/OCClassMockObject.m @@ -20,29 +20,44 @@ #import "OCMFunctionsPrivate.h" #import "OCMInvocationStub.h" #import "NSMethodSignature+OCMAdditions.h" +#import "OCProtocolsProxy.h" @implementation OCClassMockObject +{ + OCProtocolsProxy *protocolsProxy; +} #pragma mark Initialisers, description, accessors, etc. -- (id)initWithClass:(Class)aClass +- (id)initWithClass:(Class)aClass protocols:(NSArray *)protocols { NSParameterAssert(aClass != nil); - [super init]; - mockedClass = aClass; + [super init]; + mockedClass = aClass; + protocolsProxy = [[OCProtocolsProxy alloc] initWithProtocols:protocols]; + [self prepareClassForClassMethodMocking]; - return self; + return self; } - (void)dealloc { [self stopMocking]; + [protocolsProxy release]; [super dealloc]; } - (NSString *)description { - return [NSString stringWithFormat:@"OCMockObject(%@)", NSStringFromClass(mockedClass)]; + NSArray *protocolNames = [protocolsProxy protocolNames]; + + if (protocolNames) { + return [NSString stringWithFormat:@"OCMockObject(%@ <%@>)", + NSStringFromClass(mockedClass), + [protocolNames componentsJoinedByString:@", "]]; + } + + return [NSString stringWithFormat:@"OCMockObject(%@)", NSStringFromClass(mockedClass)]; } - (Class)mockedClass @@ -186,10 +201,17 @@ - (void)initializeForClassObject - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { NSMethodSignature *signature = [mockedClass instanceMethodSignatureForSelector:aSelector]; + if(signature == nil) { signature = [NSMethodSignature signatureForDynamicPropertyAccessedWithSelector:aSelector inClass:mockedClass]; } + + if(signature == nil) + { + signature = [protocolsProxy methodSignatureForSelector:aSelector]; + } + return signature; } @@ -205,7 +227,7 @@ - (Class)class - (BOOL)respondsToSelector:(SEL)selector { - return [mockedClass instancesRespondToSelector:selector]; + return [mockedClass instancesRespondToSelector:selector] || [protocolsProxy respondsToSelector:selector]; } - (BOOL)isKindOfClass:(Class)aClass @@ -215,7 +237,7 @@ - (BOOL)isKindOfClass:(Class)aClass - (BOOL)conformsToProtocol:(Protocol *)aProtocol { - return class_conformsToProtocol(mockedClass, aProtocol); + return class_conformsToProtocol(mockedClass, aProtocol) || [protocolsProxy conformsToProtocol:aProtocol]; } @end diff --git a/Source/OCMock/OCMock.h b/Source/OCMock/OCMock.h index 9d558135..51b0e3bd 100644 --- a/Source/OCMock/OCMock.h +++ b/Source/OCMock/OCMock.h @@ -24,14 +24,22 @@ #import #import +#define _OCMNilProtocol() nil +#define _OCM_OCMProtocols(...) __VA_ARGS__, nil -#define OCMClassMock(cls) [OCMockObject niceMockForClass:cls] +#define _OCMConcatFirstTwoArgs(arg1, arg2, ...) arg1 ## arg2 +#define _OCMCountProtocolsArgChooser(...) _OCMConcatFirstTwoArgs(__VA_ARGS__) -#define OCMStrictClassMock(cls) [OCMockObject mockForClass:cls] +#define _OCMProtocols() NilProtocol +#define _OCMProtocolsArgChooser(...) _OCMCountProtocolsArgChooser(_OCM, _OCMProtocols , ##__VA_ARGS__ ()) -#define OCMProtocolMock(protocol) [OCMockObject niceMockForProtocol:protocol] +#define OCMClassMock(cls, protocol...) [OCMockObject niceMockForClass:cls protocols:_OCMProtocolsArgChooser(protocol)(protocol)] -#define OCMStrictProtocolMock(protocol) [OCMockObject mockForProtocol:protocol] +#define OCMStrictClassMock(cls, protocol...) [OCMockObject mockForClass:cls protocols:_OCMProtocolsArgChooser(protocol)(protocol)] + +#define OCMProtocolMock(protocol...) [OCMockObject niceMockForProtocols:protocol, nil] + +#define OCMStrictProtocolMock(protocol...) [OCMockObject mockForProtocols:protocol, nil] #define OCMPartialMock(obj) [OCMockObject partialMockForObject:obj] diff --git a/Source/OCMock/OCMockObject.h b/Source/OCMock/OCMockObject.h index 31f7ac41..d7eac23a 100644 --- a/Source/OCMock/OCMockObject.h +++ b/Source/OCMock/OCMockObject.h @@ -33,12 +33,12 @@ NSMutableArray *invocations; } -+ (id)mockForClass:(Class)aClass; -+ (id)mockForProtocol:(Protocol *)aProtocol; ++ (id)mockForClass:(Class)aClass protocols:(Protocol *)aProtocol, ... NS_REQUIRES_NIL_TERMINATION; ++ (id)mockForProtocols:(Protocol *)aProtocol, ... NS_REQUIRES_NIL_TERMINATION; + (id)partialMockForObject:(NSObject *)anObject; -+ (id)niceMockForClass:(Class)aClass; -+ (id)niceMockForProtocol:(Protocol *)aProtocol; ++ (id)niceMockForClass:(Class)aClass protocols:(Protocol *)aProtocol, ... NS_REQUIRES_NIL_TERMINATION; ++ (id)niceMockForProtocols:(Protocol *)aProtocol, ... NS_REQUIRES_NIL_TERMINATION; + (id)observerMock; diff --git a/Source/OCMock/OCMockObject.m b/Source/OCMock/OCMockObject.m index b3e38730..bab8be09 100644 --- a/Source/OCMock/OCMockObject.m +++ b/Source/OCMock/OCMockObject.m @@ -44,14 +44,28 @@ + (void)initialize #pragma mark Factory methods -+ (id)mockForClass:(Class)aClass ++ (id)mockForClass:(Class)aClass protocols:(Protocol *)aProtocol, ... { - return [[[OCClassMockObject alloc] initWithClass:aClass] autorelease]; + va_list protocolsList; + va_start(protocolsList, aProtocol); + + NSArray *protocols = [self _arrayOfProtocol:aProtocol otherProtocols:protocolsList]; + + va_end(protocolsList); + + return [[[OCClassMockObject alloc] initWithClass:aClass protocols:protocols] autorelease]; } -+ (id)mockForProtocol:(Protocol *)aProtocol ++ (id)mockForProtocols:(Protocol *)aProtocol, ... { - return [[[OCProtocolMockObject alloc] initWithProtocol:aProtocol] autorelease]; + va_list protocolsList; + va_start(protocolsList, aProtocol); + + NSArray *protocols = [self _arrayOfProtocol:aProtocol otherProtocols:protocolsList]; + + va_end(protocolsList); + + return [[[OCProtocolMockObject alloc] initWithProtocols:protocols] autorelease]; } + (id)partialMockForObject:(NSObject *)anObject @@ -59,17 +73,50 @@ + (id)partialMockForObject:(NSObject *)anObject return [[[OCPartialMockObject alloc] initWithObject:anObject] autorelease]; } - -+ (id)niceMockForClass:(Class)aClass ++ (id)niceMockForClass:(Class)aClass protocols:(Protocol *)aProtocol, ... { - return [self _makeNice:[self mockForClass:aClass]]; + va_list protocolsList; + va_start(protocolsList, aProtocol); + + NSArray *protocols = [self _arrayOfProtocol:aProtocol otherProtocols:protocolsList]; + + va_end(protocolsList); + + id mock = [[[OCClassMockObject alloc] initWithClass:aClass protocols:protocols] autorelease]; + return [self _makeNice:mock]; } -+ (id)niceMockForProtocol:(Protocol *)aProtocol ++ (id)niceMockForProtocols:(Protocol *)aProtocol, ... { - return [self _makeNice:[self mockForProtocol:aProtocol]]; + va_list protocolsList; + va_start(protocolsList, aProtocol); + + NSArray *protocols = [self _arrayOfProtocol:aProtocol otherProtocols:protocolsList]; + + va_end(protocolsList); + + id mock = [[[OCProtocolMockObject alloc] initWithProtocols:protocols] autorelease]; + + return [self _makeNice:mock]; } ++ (NSArray *)_arrayOfProtocol:(Protocol *)aProtocol otherProtocols:(va_list)protocolsList +{ + if (!aProtocol) + { + return nil; + } + + NSMutableArray *protocols = [NSMutableArray new]; + + while(aProtocol) + { + [protocols addObject:aProtocol]; + aProtocol = va_arg(protocolsList, typeof(aProtocol)); + } + + return [protocols autorelease]; +} + (id)_makeNice:(OCMockObject *)mock { diff --git a/Source/OCMock/OCPartialMockObject.m b/Source/OCMock/OCPartialMockObject.m index 991e994f..99ad970e 100644 --- a/Source/OCMock/OCPartialMockObject.m +++ b/Source/OCMock/OCPartialMockObject.m @@ -31,7 +31,7 @@ - (id)initWithObject:(NSObject *)anObject { NSParameterAssert(anObject != nil); [self assertClassIsSupported:[anObject class]]; - [super initWithClass:[anObject class]]; + [super initWithClass:[anObject class] protocols:nil]; realObject = [anObject retain]; [self prepareObjectForInstanceMethodMocking]; return self; diff --git a/Source/OCMock/OCProtocolMockObject.h b/Source/OCMock/OCProtocolMockObject.h index 0b3f6b12..80f3f126 100644 --- a/Source/OCMock/OCProtocolMockObject.h +++ b/Source/OCMock/OCProtocolMockObject.h @@ -17,11 +17,8 @@ #import @interface OCProtocolMockObject : OCMockObject -{ - Protocol *mockedProtocol; -} -- (id)initWithProtocol:(Protocol *)aProtocol; +- (id)initWithProtocols:(NSArray *)protocols; @end diff --git a/Source/OCMock/OCProtocolMockObject.m b/Source/OCMock/OCProtocolMockObject.m index 55dc7b52..c886abac 100644 --- a/Source/OCMock/OCProtocolMockObject.m +++ b/Source/OCMock/OCProtocolMockObject.m @@ -17,47 +17,53 @@ #import #import "NSMethodSignature+OCMAdditions.h" #import "OCProtocolMockObject.h" +#import "OCProtocolsProxy.h" @implementation OCProtocolMockObject +{ + OCProtocolsProxy *protocolsProxy; +} #pragma mark Initialisers, description, accessors, etc. -- (id)initWithProtocol:(Protocol *)aProtocol +- (id)initWithProtocols:(NSArray *)protocols { - NSParameterAssert(aProtocol != nil); + NSCParameterAssert(protocols != nil); + [super init]; - mockedProtocol = aProtocol; + + protocolsProxy = [[OCProtocolsProxy alloc] initWithProtocols:protocols]; + return self; } +- (void)dealloc +{ + [protocolsProxy release]; + [super dealloc]; +} + - (NSString *)description { - const char* name = protocol_getName(mockedProtocol); - return [NSString stringWithFormat:@"OCMockObject(%s)", name]; + NSArray *protocolNames = [protocolsProxy protocolNames]; + return [NSString stringWithFormat:@"OCMockObject(%@)", [protocolNames componentsJoinedByString:@", "]]; } #pragma mark Proxy API - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { - struct { BOOL isRequired; BOOL isInstance; } opts[4] = { {YES, YES}, {NO, YES}, {YES, NO}, {NO, NO} }; - for(int i = 0; i < 4; i++) - { - struct objc_method_description methodDescription = protocol_getMethodDescription(mockedProtocol, aSelector, opts[i].isRequired, opts[i].isInstance); - if(methodDescription.name != NULL) - return [NSMethodSignature signatureWithObjCTypes:methodDescription.types]; - } - return nil; + return [protocolsProxy methodSignatureForSelector:aSelector]; } - (BOOL)conformsToProtocol:(Protocol *)aProtocol { - return protocol_conformsToProtocol(mockedProtocol, aProtocol); + return [protocolsProxy conformsToProtocol:aProtocol]; } - (BOOL)respondsToSelector:(SEL)selector { - return ([self methodSignatureForSelector:selector] != nil); + return [protocolsProxy respondsToSelector:selector]; } @end diff --git a/Source/OCMock/OCProtocolsProxy.h b/Source/OCMock/OCProtocolsProxy.h new file mode 100644 index 00000000..1de2e2b4 --- /dev/null +++ b/Source/OCMock/OCProtocolsProxy.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2005-2016 Erik Doernenburg and contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use these files except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +#import + +@interface OCProtocolsProxy : NSObject + +- (id)initWithProtocols:(NSArray *)protocols; + +- (NSArray *)protocolNames; + +@end + + diff --git a/Source/OCMock/OCProtocolsProxy.m b/Source/OCMock/OCProtocolsProxy.m new file mode 100644 index 00000000..70ae0e57 --- /dev/null +++ b/Source/OCMock/OCProtocolsProxy.m @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2005-2016 Erik Doernenburg and contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use these files except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +#import +#import "NSMethodSignature+OCMAdditions.h" +#import "OCProtocolsProxy.h" + +@interface OCProtocolProxy : NSObject + +- (id)initWithProtocol:(Protocol *)aProtocol; + +- (NSString *)protocolName; + +@end + +@implementation OCProtocolProxy +{ + Protocol *mockedProtocol; +} + +#pragma mark Initialisers, description, accessors, etc. + +- (id)initWithProtocol:(Protocol *)aProtocol +{ + NSParameterAssert(aProtocol != nil); + self = [super init]; + if(self) + { + mockedProtocol = aProtocol; + } + + return self; +} + +- (NSString *)protocolName +{ + const char* name = protocol_getName(mockedProtocol); + return [NSString stringWithUTF8String:name]; +} + +#pragma mark Proxy API + +- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector +{ + struct { BOOL isRequired; BOOL isInstance; } opts[4] = { {YES, YES}, {NO, YES}, {YES, NO}, {NO, NO} }; + for(int i = 0; i < 4; i++) + { + struct objc_method_description methodDescription = protocol_getMethodDescription(mockedProtocol, aSelector, opts[i].isRequired, opts[i].isInstance); + if(methodDescription.name != NULL) + return [NSMethodSignature signatureWithObjCTypes:methodDescription.types]; + } + return nil; +} + +- (BOOL)conformsToProtocol:(Protocol *)aProtocol +{ + return protocol_conformsToProtocol(mockedProtocol, aProtocol); +} + +@end + + +@implementation OCProtocolsProxy +{ + NSArray *protocolProxies; +} + +- (instancetype)initWithProtocols:(NSArray *)protocols +{ + self = [super init]; + + if (self && protocols) + { + NSMutableArray *proxies = [NSMutableArray new]; + + for(Protocol *aProtocol in protocols) + { + OCProtocolProxy *protocolProxy = [[OCProtocolProxy alloc] initWithProtocol:aProtocol]; + [proxies addObject:protocolProxy]; + [protocolProxy release]; + } + + protocolProxies = proxies; + } + return self; +} + +- (void)dealloc +{ + [protocolProxies release]; + [super dealloc]; +} + +- (NSArray *)protocolNames +{ + return [protocolProxies valueForKey:NSStringFromSelector(@selector(protocolName))]; +} + + +#pragma mark Proxy API + +- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector +{ + for(OCProtocolProxy *protocolProxy in protocolProxies) + { + NSMethodSignature *signature = [protocolProxy methodSignatureForSelector:aSelector]; + + if(signature) + { + return signature; + } + } + return nil; +} + +- (BOOL)conformsToProtocol:(Protocol *)aProtocol +{ + for(OCProtocolProxy *protocolProxy in protocolProxies) + { + if([protocolProxy conformsToProtocol:aProtocol]) + { + return YES; + } + } + return NO; +} + +- (BOOL)respondsToSelector:(SEL)selector +{ + return ([self methodSignatureForSelector:selector] != nil); +} + +@end diff --git a/Source/OCMockTests/OCMStubRecorderTests.m b/Source/OCMockTests/OCMStubRecorderTests.m index b2de0222..bf5ac5c7 100644 --- a/Source/OCMockTests/OCMStubRecorderTests.m +++ b/Source/OCMockTests/OCMStubRecorderTests.m @@ -33,7 +33,7 @@ - (void)testCreatesInvocationMatcher { NSString *arg = @"I love mocks."; - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; OCMStubRecorder *recorder = [[OCMStubRecorder alloc] initWithMockObject:mock]; [(id)recorder stringByAppendingString:arg]; @@ -46,7 +46,7 @@ - (void)testCreatesInvocationMatcher - (void)testAddsReturnValueProvider { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; OCMStubRecorder *recorder = [[OCMStubRecorder alloc] initWithMockObject:mock]; [recorder andReturn:@"foo"]; NSArray *actionList = [(OCMInvocationStub *)[recorder invocationMatcher] invocationActions]; @@ -57,7 +57,7 @@ - (void)testAddsReturnValueProvider - (void)testAddsExceptionReturnValueProvider { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; OCMStubRecorder *recorder = [[OCMStubRecorder alloc] initWithMockObject:mock]; [recorder andThrow:[NSException exceptionWithName:@"TestException" reason:@"A reason" userInfo:nil]]; NSArray *actionList = [(OCMInvocationStub *)[recorder invocationMatcher] invocationActions]; diff --git a/Source/OCMockTests/OCMockObjectClassMethodMockingTests.m b/Source/OCMockTests/OCMockObjectClassMethodMockingTests.m index 5cfe8d56..6677b534 100644 --- a/Source/OCMockTests/OCMockObjectClassMethodMockingTests.m +++ b/Source/OCMockTests/OCMockObjectClassMethodMockingTests.m @@ -80,7 +80,7 @@ @implementation OCMockObjectClassMethodMockingTests - (void)testCanStubClassMethod { - id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[[mock stub] classMethod] andReturn:@"mocked"] foo]; @@ -89,7 +89,7 @@ - (void)testCanStubClassMethod - (void)testCanExpectTheSameClassMethodMoreThanOnce { - id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[[mock expect] classMethod] andReturn:@"mocked-foo"] foo]; [[[[mock expect] classMethod] andReturn:@"mocked-foo2"] foo]; @@ -99,7 +99,7 @@ - (void)testCanExpectTheSameClassMethodMoreThanOnce - (void)testClassReceivesMethodsAfterStopWasCalled { - id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[[mock stub] classMethod] andReturn:@"mocked"] foo]; [mock stopMocking]; @@ -109,7 +109,7 @@ - (void)testClassReceivesMethodsAfterStopWasCalled - (void)testClassReceivesMethodAgainWhenExpectedCallOccurred { - id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[[mock expect] classMethod] andReturn:@"mocked"] foo]; @@ -119,7 +119,7 @@ - (void)testClassReceivesMethodAgainWhenExpectedCallOccurred - (void)testCanStubClassMethodFromMockForSubclass { - id subclassMock = [OCMockObject mockForClass:[TestSubclassWithClassMethods class]]; + id subclassMock = [OCMockObject mockForClass:[TestSubclassWithClassMethods class] protocols:nil]; [[[[subclassMock stub] classMethod] andReturn:@"mocked-subclass"] foo]; XCTAssertEqualObjects(@"mocked-subclass", [TestSubclassWithClassMethods foo], @"Should have stubbed method."); @@ -128,7 +128,7 @@ - (void)testCanStubClassMethodFromMockForSubclass - (void)testSuperclassReceivesMethodsAfterStopWasCalled { - id mock = [OCMockObject mockForClass:[TestSubclassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestSubclassWithClassMethods class] protocols:nil]; [[[[mock stub] classMethod] andReturn:@"mocked"] foo]; [mock stopMocking]; @@ -138,8 +138,8 @@ - (void)testSuperclassReceivesMethodsAfterStopWasCalled - (void)testCanReplaceSameMethodInSubclassAfterSuperclassMockWasStopped { - id superclassMock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; - id subclassMock = [OCMockObject mockForClass:[TestSubclassWithClassMethods class]]; + id superclassMock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; + id subclassMock = [OCMockObject mockForClass:[TestSubclassWithClassMethods class] protocols:nil]; [[[[superclassMock stub] classMethod] andReturn:@"mocked-superclass"] foo]; [superclassMock stopMocking]; @@ -150,8 +150,8 @@ - (void)testCanReplaceSameMethodInSubclassAfterSuperclassMockWasStopped - (void)testCanReplaceSameMethodInSuperclassAfterSubclassMockWasStopped { - id superclassMock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; - id subclassMock = [OCMockObject mockForClass:[TestSubclassWithClassMethods class]]; + id superclassMock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; + id subclassMock = [OCMockObject mockForClass:[TestSubclassWithClassMethods class] protocols:nil]; [[[[subclassMock stub] classMethod] andReturn:@"mocked-subclass"] foo]; [subclassMock stopMocking]; @@ -163,21 +163,21 @@ - (void)testCanReplaceSameMethodInSuperclassAfterSubclassMockWasStopped - (void)testStubbingIsOnlyActiveAtTheClassItWasAdded { // stage 1: stub in superclass affects only superclass - id superclassMock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id superclassMock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[[superclassMock stub] classMethod] andReturn:@"mocked-superclass"] foo]; XCTAssertEqualObjects(@"mocked-superclass", [TestClassWithClassMethods foo], @"Should have stubbed method"); XCTAssertEqualObjects(@"Foo-ClassMethod", [TestSubclassWithClassMethods foo], @"Should NOT have stubbed method"); [superclassMock stopMocking]; // stage 2: stub in subclass affects only subclass - id subclassMock = [OCMockObject mockForClass:[TestSubclassWithClassMethods class]]; + id subclassMock = [OCMockObject mockForClass:[TestSubclassWithClassMethods class] protocols:nil]; [[[[subclassMock stub] classMethod] andReturn:@"mocked-subclass"] foo]; XCTAssertEqualObjects(@"Foo-ClassMethod", [TestClassWithClassMethods foo], @"Should NOT have stubbed method"); XCTAssertEqualObjects(@"mocked-subclass", [TestSubclassWithClassMethods foo], @"Should have stubbed method"); [subclassMock stopMocking]; // stage 3: like stage 1; also demonstrates that subclass cleared all stubs - id superclassMock2 = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id superclassMock2 = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[[superclassMock2 stub] classMethod] andReturn:@"mocked-superclass"] foo]; XCTAssertEqualObjects(@"mocked-superclass", [TestClassWithClassMethods foo], @"Should have stubbed method"); XCTAssertEqualObjects(@"Foo-ClassMethod", [TestSubclassWithClassMethods foo], @"Should NOT have stubbed method"); @@ -185,7 +185,7 @@ - (void)testStubbingIsOnlyActiveAtTheClassItWasAdded - (void)testStubsOnlyClassMethodWhenInstanceMethodWithSameNameExists { - id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[[mock stub] classMethod] andReturn:@"mocked"] bar]; @@ -195,7 +195,7 @@ - (void)testStubsOnlyClassMethodWhenInstanceMethodWithSameNameExists - (void)testStubsClassMethodWhenNoInstanceMethodExistsWithName { - id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[mock stub] andReturn:@"mocked"] foo]; @@ -204,7 +204,7 @@ - (void)testStubsClassMethodWhenNoInstanceMethodExistsWithName - (void)testStubsCanDistinguishInstanceAndClassMethods { - id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[[mock stub] classMethod] andReturn:@"mocked-class"] bar]; [[[mock stub] andReturn:@"mocked-instance"] bar]; @@ -215,7 +215,7 @@ - (void)testStubsCanDistinguishInstanceAndClassMethods - (void)testRevertsAllStubbedMethodsOnDealloc { - id mock = [[OCClassMockObject alloc] initWithClass:[TestClassWithClassMethods class]]; + id mock = [[OCClassMockObject alloc] initWithClass:[TestClassWithClassMethods class] protocols:nil]; [[[[mock stub] classMethod] andReturn:@"mocked-foo"] foo]; [[[[mock stub] classMethod] andReturn:@"mocked-bar"] bar]; @@ -231,7 +231,7 @@ - (void)testRevertsAllStubbedMethodsOnDealloc - (void)testRevertsAllStubbedMethodsOnPartialMockDealloc { - id mock = [[OCPartialMockObject alloc] initWithClass:[TestClassWithClassMethods class]]; + id mock = [[OCPartialMockObject alloc] initWithClass:[TestClassWithClassMethods class] protocols:nil]; [[[[mock stub] classMethod] andReturn:@"mocked-foo"] foo]; [[[[mock stub] classMethod] andReturn:@"mocked-bar"] bar]; @@ -247,10 +247,10 @@ - (void)testRevertsAllStubbedMethodsOnPartialMockDealloc - (void)testSecondClassMockDeactivatesFirst { - id mock1 = [[OCClassMockObject alloc] initWithClass:[TestClassWithClassMethods class]]; + id mock1 = [[OCClassMockObject alloc] initWithClass:[TestClassWithClassMethods class] protocols:nil]; [[[mock1 stub] andReturn:@"mocked-foo-1"] foo]; - id mock2 = [[OCClassMockObject alloc] initWithClass:[TestClassWithClassMethods class]]; + id mock2 = [[OCClassMockObject alloc] initWithClass:[TestClassWithClassMethods class] protocols:nil]; XCTAssertEqualObjects(@"Foo-ClassMethod", [TestClassWithClassMethods foo]); [mock2 stopMocking]; @@ -261,7 +261,7 @@ - (void)testForwardToRealObject { NSString *classFooValue = [TestClassWithClassMethods foo]; NSString *classBarValue = [TestClassWithClassMethods bar]; - id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[[mock expect] classMethod] andForwardToRealObject] foo]; NSString *result = [TestClassWithClassMethods foo]; @@ -290,14 +290,14 @@ - (void)testForwardToRealObject - (void)testRefusesToCreateClassMockForNilClass { - XCTAssertThrows(OCMClassMock(nil)); + XCTAssertThrows([OCMockObject mockForClass:nil protocols:nil]); } - (void)testInitializeIsNotCalledOnMockedClass { NSUInteger countBefore = [TestClassWithClassMethods initializeCallCount]; - id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [TestClassWithClassMethods foo]; [[mock verify] foo]; @@ -310,7 +310,7 @@ - (void)testCanStubNSObjectClassMethodsIncludingAlloc { TestClassWithClassMethods *dummyObject = [[TestClassWithClassMethods alloc] init]; - id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class]]; + id mock = [OCMockObject mockForClass:[TestClassWithClassMethods class] protocols:nil]; [[[mock stub] andReturn:dummyObject] new]; id newObject = [TestClassWithClassMethods new]; diff --git a/Source/OCMockTests/OCMockObjectDynamicPropertyMockingTests.m b/Source/OCMockTests/OCMockObjectDynamicPropertyMockingTests.m index 484c54a9..a8861658 100644 --- a/Source/OCMockTests/OCMockObjectDynamicPropertyMockingTests.m +++ b/Source/OCMockTests/OCMockObjectDynamicPropertyMockingTests.m @@ -46,7 +46,7 @@ @implementation OCMockObjectDynamicPropertyMockingTests - (void)testCanStubDynamicPropertiesWithIdType { - id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class]]; + id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class] protocols:nil]; NSDictionary *testDict = @{@"test-key" : @"test-value"}; [[[mock stub] andReturn:testDict] anObject]; XCTAssertEqualObjects(testDict, [mock anObject]); @@ -54,7 +54,7 @@ - (void)testCanStubDynamicPropertiesWithIdType - (void)testCanStubDynamicPropertiesWithUIntType { - id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class]]; + id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class] protocols:nil]; NSUInteger someUInt = 5; [[[mock stub] andReturnValue:OCMOCK_VALUE(someUInt)] aUInt]; XCTAssertEqual(5, [mock aUInt]); @@ -62,7 +62,7 @@ - (void)testCanStubDynamicPropertiesWithUIntType - (void)testCanStubDynamicPropertiesWithIntType { - id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class]]; + id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class] protocols:nil]; NSInteger someInt = -10; [[[mock stub] andReturnValue:OCMOCK_VALUE(someInt)] __aPrivateInt]; XCTAssertEqual(-10, [mock __aPrivateInt]); @@ -70,7 +70,7 @@ - (void)testCanStubDynamicPropertiesWithIntType - (void)testCanStubDynamicPropertiesWithCustomGetter { - id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class]]; + id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class] protocols:nil]; NSDictionary *testDict = @{@"test-key" : @"test-value"}; [[[mock stub] andReturn:testDict] customGetter]; XCTAssertEqualObjects(testDict, [mock customGetter]); @@ -78,7 +78,7 @@ - (void)testCanStubDynamicPropertiesWithCustomGetter - (void)testCanMockSetterForDynamicProperty { - id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class]]; + id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class] protocols:nil]; NSDictionary *dummyObject = @{@"test-key" : @"test-value"}; [[mock expect] setAnObject:dummyObject]; @@ -88,7 +88,7 @@ - (void)testCanMockSetterForDynamicProperty - (void)testCanMockSetterForDynamicPropertyWithCustomSetter { - id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class]]; + id mock = [OCMockObject mockForClass:[TestClassWithDynamicProperties class] protocols:nil]; NSDictionary *dummyObject = @{@"test-key" : @"test-value"}; [[mock expect] customSetter:dummyObject]; diff --git a/Source/OCMockTests/OCMockObjectHamcrestTests.m b/Source/OCMockTests/OCMockObjectHamcrestTests.m index 6d84d762..ed29ae7e 100644 --- a/Source/OCMockTests/OCMockObjectHamcrestTests.m +++ b/Source/OCMockTests/OCMockObjectHamcrestTests.m @@ -30,7 +30,7 @@ @implementation OCMockObjectHamcrestTests - (void)testAcceptsStubbedMethodWithHamcrestConstraint { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; [[mock stub] hasSuffix:(id)startsWith(@"foo")]; [mock hasSuffix:@"foobar"]; } @@ -38,7 +38,7 @@ - (void)testAcceptsStubbedMethodWithHamcrestConstraint - (void)testRejectsUnstubbedMethodWithHamcrestConstraint { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; [[mock stub] hasSuffix:(id)anyOf(equalTo(@"foo"), equalTo(@"bar"), NULL)]; XCTAssertThrows([mock hasSuffix:@"foobar"], @"Should have raised an exception."); } diff --git a/Source/OCMockTests/OCMockObjectMacroTests.m b/Source/OCMockTests/OCMockObjectMacroTests.m index 88506d1c..a85bbac7 100644 --- a/Source/OCMockTests/OCMockObjectMacroTests.m +++ b/Source/OCMockTests/OCMockObjectMacroTests.m @@ -386,4 +386,101 @@ - (void)testCanUseMacroToStubMethodWithDecimalReturnValue XCTAssertEqualObjects([mock method], [NSDecimalNumber decimalNumberWithDecimal:[@0 decimalValue]]); } +- (void)testCanUseMacroToCreateMockForClassAndSingleProtocol +{ + id mock = OCMClassMock([NSObject class], @protocol(TestProtocolForMacroTesting)); + XCTAssertNotNil(mock); + + id Value = [NSObject new]; + OCMExpect([mock stringValue]).andReturn(Value); + + XCTAssertEqual(Value, [mock stringValue]); + + OCMVerifyAll(mock); + + OCMExpect([mock stringValue]).andReturn(Value); + + XCTAssertThrows([mock verify], @"Should have complained about expected method not being invoked"); +} + +- (void)testCanUseMacroToCreateMockForClassAndMultipleProtocols +{ + id mock = OCMClassMock([NSObject class], @protocol(TestProtocolForMacroTesting), @protocol(NSLocking)); + XCTAssertNotNil(mock); + + id Value = [NSObject new]; + OCMExpect([mock stringValue]).andReturn(Value); + + OCMExpect([mock lock]); + + XCTAssertEqual(Value, [mock stringValue]); + + [mock lock]; + + OCMVerifyAll(mock); + + OCMExpect([mock stringValue]).andReturn(Value); + + OCMExpect([mock lock]); + + XCTAssertThrows([mock verify], @"Should have complained about expected method not being invoked"); +} + +- (void)testCanUseMacroToCreateStrictMockForClassAndSingleProtocol +{ + id mock = OCMStrictClassMock([NSObject class], @protocol(TestProtocolForMacroTesting)); + XCTAssertNotNil(mock); + + id Value = [NSObject new]; + OCMExpect([mock stringValue]).andReturn(Value); + + XCTAssertEqual(Value, [mock stringValue]); + + OCMVerifyAll(mock); + + XCTAssertThrows([mock stringValue]); + + OCMExpect([mock stringValue]).andReturn(Value); + + XCTAssertThrows([mock verify], @"Should have complained about expected method not being invoked"); +} + +- (void)testCanUseMacroToCreateStrictMockForClassAndMultipleProtocols +{ + id mock = OCMStrictClassMock([NSObject class], @protocol(TestProtocolForMacroTesting), @protocol(NSLocking)); + XCTAssertNotNil(mock); + + id Value = [NSObject new]; + OCMExpect([mock stringValue]).andReturn(Value); + + OCMExpect([mock lock]); + + XCTAssertEqual(Value, [mock stringValue]); + + [mock lock]; + + OCMVerifyAll(mock); + + XCTAssertThrows([mock stringValue]); + XCTAssertThrows([mock lock]); + + OCMExpect([mock stringValue]).andReturn(Value); + + OCMExpect([mock lock]); + + XCTAssertThrows([mock verify], @"Should have complained about expected method not being invoked"); +} + +- (void)testTwoClassMocksWithProtocolsInARow { + // May break incorrect variadics + + id mock1 = OCMClassMock([NSObject class]); + id mock2 = OCMClassMock([NSObject class], @protocol(TestProtocolForMacroTesting), @protocol(NSLocking)); + id mock3 = OCMProtocolMock(@protocol(TestProtocolForMacroTesting), @protocol(NSLocking)); + + XCTAssertNotNil(mock1); + XCTAssertNotNil(mock2); + XCTAssertNotNil(mock3); +} + @end diff --git a/Source/OCMockTests/OCMockObjectProtocolMocksTests.m b/Source/OCMockTests/OCMockObjectProtocolMocksTests.m index 4fa44774..00d254e2 100644 --- a/Source/OCMockTests/OCMockObjectProtocolMocksTests.m +++ b/Source/OCMockTests/OCMockObjectProtocolMocksTests.m @@ -16,19 +16,13 @@ #import #import +#import "TestProtocol.h" // -------------------------------------------------------------------------------------- // Helper classes and protocols for testing // -------------------------------------------------------------------------------------- -@protocol TestProtocol -+ (NSString *)stringValueClassMethod; -- (int)primitiveValue; -@optional -- (id)objectValue; -@end - @interface InterfaceForTypedef : NSObject { int prop1; NSObject *prop2; @@ -62,86 +56,117 @@ @implementation OCMockObjectProtocolMocksTests - (void)testCanMockFormalProtocol { - id mock = [OCMockObject mockForProtocol:@protocol(NSLocking)]; + id mock = [OCMockObject mockForProtocols:@protocol(NSLocking), nil]; + [[mock expect] lock]; + + [mock lock]; + + [mock verify]; +} + +- (void)testCanMockMultipleProtocols +{ + id mock = OCMProtocolMock(@protocol(NSLocking), @protocol(TestProtocol)); [[mock expect] lock]; + [[mock expect] primitiveValue]; [mock lock]; + [mock primitiveValue]; [mock verify]; } - (void)testSetsCorrectNameForProtocolMockObjects { - id mock = [OCMockObject mockForProtocol:@protocol(NSLocking)]; + id mock = [OCMockObject mockForProtocols:@protocol(NSLocking), nil]; XCTAssertEqualObjects(@"OCMockObject(NSLocking)", [mock description], @"Should have returned correct description."); } -- (void)testRaisesWhenUnknownMethodIsCalledOnProtocol +- (void)testSetsCorrectNameForMultipleProtocolsMockObjects { - id mock = [OCMockObject mockForProtocol:@protocol(NSLocking)]; + id mock = [OCMockObject mockForProtocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + XCTAssertEqualObjects(@"OCMockObject(NSLocking, TestProtocol)", [mock description], @"Should have returned correct description."); +} + +- (void)testRaisesWhenUnknownMethodIsCalledOnProtocolsMock +{ + id mock = [OCMockObject mockForProtocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; XCTAssertThrows([mock lowercaseString], @"Should have raised an exception."); } -- (void)testConformsToMockedProtocol +- (void)testConformsToMockedProtocols { - id mock = [OCMockObject mockForProtocol:@protocol(NSLocking)]; + id mock = [OCMockObject mockForProtocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; XCTAssertTrue([mock conformsToProtocol:@protocol(NSLocking)]); + XCTAssertTrue([mock conformsToProtocol:@protocol(TestProtocol)]); } - (void)testRespondsToValidProtocolRequiredSelector { - id mock = [OCMockObject mockForProtocol:@protocol(TestProtocol)]; + id mock = [OCMockObject mockForProtocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; XCTAssertTrue([mock respondsToSelector:@selector(primitiveValue)]); } - (void)testRespondsToValidProtocolOptionalSelector { - id mock = [OCMockObject mockForProtocol:@protocol(TestProtocol)]; + id mock = [OCMockObject mockForProtocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; XCTAssertTrue([mock respondsToSelector:@selector(objectValue)]); } - (void)testDoesNotRespondToInvalidProtocolSelector { - id mock = [OCMockObject mockForProtocol:@protocol(TestProtocol)]; + id mock = [OCMockObject mockForProtocols:@protocol(TestProtocol), nil]; XCTAssertFalse([mock respondsToSelector:@selector(testDoesNotRespondToInvalidProtocolSelector)]); } - (void)testWithTypedefReturnType { - id mock = [OCMockObject mockForProtocol:@protocol(ProtocolWithTypedefs)]; + id mock = [OCMockObject mockForProtocols:@protocol(ProtocolWithTypedefs), nil]; XCTAssertNoThrow([[[mock stub] andReturn:[TypedefInterface new]] typedefReturnValue1], @"Should accept a typedefed return-type"); XCTAssertNoThrow([mock typedefReturnValue1]); } - (void)testWithTypedefPointerReturnType { - id mock = [OCMockObject mockForProtocol:@protocol(ProtocolWithTypedefs)]; + id mock = [OCMockObject mockForProtocols:@protocol(ProtocolWithTypedefs), nil]; XCTAssertNoThrow([[[mock stub] andReturn:[TypedefInterface new]] typedefReturnValue2], @"Should accept a typedefed return-type"); XCTAssertNoThrow([mock typedefReturnValue2]); } - (void)testWithTypedefParameter { - id mock = [OCMockObject mockForProtocol:@protocol(ProtocolWithTypedefs)]; + id mock = [OCMockObject mockForProtocols:@protocol(ProtocolWithTypedefs), nil]; XCTAssertNoThrow([[mock stub] typedefParameter:nil], @"Should accept a typedefed parameter-type"); XCTAssertNoThrow([mock typedefParameter:nil]); } -- (void)testReturnDefaultValueWhenUnknownMethodIsCalledOnNiceProtocolMock +- (void)testCanMockMultipleProtocolsNicely +{ + id mock = [OCMockObject niceMockForProtocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + [[mock expect] lock]; + [[mock expect] primitiveValue]; + + [mock lock]; + [mock primitiveValue]; + + [mock verify]; +} + +- (void)testReturnDefaultValueWhenUnknownMethodIsCalledOnNiceProtocolsMock { - id mock = [OCMockObject niceMockForProtocol:@protocol(TestProtocol)]; + id mock = [OCMockObject niceMockForProtocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; XCTAssertTrue(0 == [mock primitiveValue], @"Should return 0 on unexpected method call (for nice mock)."); [mock verify]; } -- (void)testRaisesAnExceptionWenAnExpectedMethodIsNotCalledOnNiceProtocolMock +- (void)testRaisesAnExceptionWhenAnExpectedMethodIsNotCalledOnNiceProtocolsMock { - id mock = [OCMockObject niceMockForProtocol:@protocol(TestProtocol)]; + id mock = [OCMockObject niceMockForProtocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; [[mock expect] primitiveValue]; XCTAssertThrows([mock verify], @"Should have raised an exception because method was not called."); } - (void)testProtocolClassMethod { - id mock = OCMProtocolMock(@protocol(TestProtocol)); + id mock = OCMProtocolMock(@protocol(TestProtocol), @protocol(NSLocking)); OCMStub([mock stringValueClassMethod]).andReturn(@"stubbed"); id result = [mock stringValueClassMethod]; XCTAssertEqual(@"stubbed", result, @"Should have stubbed the class method."); @@ -149,7 +174,7 @@ - (void)testProtocolClassMethod - (void)testRefusesToCreateProtocolMockForNilProtocol { - XCTAssertThrows(OCMProtocolMock(nil)); + XCTAssertThrows([OCMockObject mockForProtocols:nil]); } @end diff --git a/Source/OCMockTests/OCMockObjectRuntimeTests.m b/Source/OCMockTests/OCMockObjectRuntimeTests.m index e60a2ccf..f53d7a02 100644 --- a/Source/OCMockTests/OCMockObjectRuntimeTests.m +++ b/Source/OCMockTests/OCMockObjectRuntimeTests.m @@ -16,6 +16,7 @@ #import #import +#import "TestProtocol.h" #pragma mark Helper classes @@ -83,7 +84,6 @@ - (void)run @end - #pragma mark Tests for interaction with runtime and foundation conventions @interface OCMockObjectRuntimeTests : XCTestCase @@ -92,24 +92,53 @@ @interface OCMockObjectRuntimeTests : XCTestCase @implementation OCMockObjectRuntimeTests -- (void)testRespondsToValidSelector +- (void)testRespondsToValidSelectorForClass { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] + protocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + XCTAssertTrue([mock respondsToSelector:@selector(lowercaseString)]); } +- (void)testRespondsToValidSelectorForProtocols +{ + id mock = [OCMockObject mockForClass:[NSString class] + protocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + + XCTAssertTrue([mock respondsToSelector:@selector(lock)]); + XCTAssertTrue([mock respondsToSelector:@selector(primitiveValue)]); +} - (void)testDoesNotRespondToInvalidSelector { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] + protocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + // We use a selector that's not implemented by the mock XCTAssertFalse([mock respondsToSelector:@selector(arrayWithArray:)]); } +- (void)testConformsToProtocolsFromList +{ + id mock = [OCMockObject mockForClass:[NSString class] + protocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + + XCTAssertTrue([mock conformsToProtocol:@protocol(NSLocking)]); + XCTAssertTrue([mock conformsToProtocol:@protocol(TestProtocol)]); +} + +- (void)testDoesNotConformToInvalidProtocol +{ + id mock = [OCMockObject mockForClass:[NSString class] + protocols:@protocol(TestProtocol), nil]; + + // We use a protocol that's not implemented by the mock + XCTAssertFalse([mock conformsToProtocol:@protocol(NSLocking)]); +} - (void)testCanStubValueForKeyMethod { - id mock = [OCMockObject mockForClass:[NSObject class]]; + id mock = [OCMockObject mockForClass:[NSObject class] protocols:nil]; [[[mock stub] andReturn:@"SomeValue"] valueForKey:@"SomeKey"]; id returnValue = [mock valueForKey:@"SomeKey"]; @@ -120,21 +149,21 @@ - (void)testCanStubValueForKeyMethod - (void)testCanMockNSMutableArray { - id mock = [OCMockObject niceMockForClass:[NSMutableArray class]]; + id mock = [OCMockObject niceMockForClass:[NSMutableArray class] protocols:nil]; id anArray = [[NSMutableArray alloc] init]; } - (void)testForwardsIsKindOfClass { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; XCTAssertTrue([mock isKindOfClass:[NSString class]], @"Should have pretended to be the mocked class."); } - (void)testWorksWithTypeQualifiers { - id myMock = [OCMockObject mockForClass:[TestClassWithTypeQualifierMethod class]]; + id myMock = [OCMockObject mockForClass:[TestClassWithTypeQualifierMethod class] protocols:nil]; XCTAssertNoThrow([[myMock expect] aSpecialMethod:"foo"], @"Should not complain about method with type qualifiers."); XCTAssertNoThrow([myMock aSpecialMethod:"foo"], @"Should not complain about method with type qualifiers."); @@ -142,7 +171,7 @@ - (void)testWorksWithTypeQualifiers - (void)testWorksWithTypedefsToObjects { - id myMock = [OCMockObject mockForClass:[TestClassWithTypedefObjectArgument class]]; + id myMock = [OCMockObject mockForClass:[TestClassWithTypedefObjectArgument class] protocols:nil]; [[[myMock stub] andReturn:@"stubbed"] stringForTypedef:[OCMArg any]]; id actualReturn = [myMock stringForTypedef:@"Some arg that shouldn't matter"]; XCTAssertEqualObjects(actualReturn, @"stubbed", @"Should have matched invocation."); @@ -152,7 +181,7 @@ - (void)testWorksWithTypedefsToObjects #if 0 // can't test this with ARC - (void)testAdjustsRetainCountWhenStubbingMethodsThatCreateObjects { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; NSString *objectToReturn = [NSString stringWithFormat:@"This is not a %@.", @"string constant"]; #pragma clang diagnostic push #pragma ide diagnostic ignored "NotReleasedValue" @@ -171,26 +200,26 @@ - (void)testAdjustsRetainCountWhenStubbingMethodsThatCreateObjects - (void)testComplainsWhenUnimplementedMethodIsCalled { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; XCTAssertThrowsSpecificNamed([mock performSelector:@selector(sortedArrayHint)], NSException, NSInvalidArgumentException); } - (void)testComplainsWhenAttemptIsMadeToStubInitMethod { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; XCTAssertThrows([[[mock stub] init] andReturn:nil]); } - (void)testComplainsWhenAttemptIsMadeToStubInitMethodViaMacro { - id mock = [OCMockObject mockForClass:[NSString class]]; + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; XCTAssertThrows(OCMStub([mock init])); } - (void)testMockShouldNotRaiseWhenDescribing { - id mock = [OCMockObject mockForClass:[NSObject class]]; + id mock = [OCMockObject mockForClass:[NSObject class] protocols:nil]; XCTAssertNoThrow(NSLog(@"Testing description handling dummy methods... %@ %@ %@ %@ %@", @{@"foo": mock}, @@ -239,12 +268,90 @@ - (void)testDynamicSubclassesShouldBeDisposed { int numClassesBefore = objc_getClassList(NULL, 0); - id mock = [OCMockObject mockForClass:[TestDelegate class]]; + id mock = [OCMockObject mockForClass:[TestDelegate class] protocols:nil]; [mock stopMocking]; int numClassesAfter = objc_getClassList(NULL, 0); XCTAssertEqual(numClassesBefore, numClassesAfter, @"Should have disposed dynamically generated classes."); } +- (void)testDescriptionForClassMockObjectForNoProtocols +{ + id mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; + XCTAssertEqualObjects(@"OCMockObject(NSString)", [mock description], @"Should have returned correct description."); +} + +- (void)testDescriptionForClassMockObjectForSingleProtocol +{ + id mock = [OCMockObject mockForClass:[NSString class] + protocols:@protocol(TestProtocol), nil]; + XCTAssertEqualObjects(@"OCMockObject(NSString )", [mock description], @"Should have returned correct description."); +} + +- (void)testDescriptionForClassMockObjectForMultipleProtocols +{ + id mock = [OCMockObject mockForClass:[NSString class] + protocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + XCTAssertEqualObjects(@"OCMockObject(NSString )", [mock description], @"Should have returned correct description."); +} + +- (void)testCanMockFormalProtocol +{ + id mock = [OCMockObject mockForProtocols:@protocol(NSLocking), nil]; + [[mock expect] lock]; + + [mock lock]; + + [mock verify]; +} + +- (void)testCanMockMultipleProtocols +{ + id mock = OCMProtocolMock(@protocol(NSLocking), @protocol(TestProtocol)); + [[mock expect] lock]; + [[mock expect] primitiveValue]; + + [mock lock]; + [mock primitiveValue]; + + [mock verify]; +} + +- (void)testRaisesWhenUnknownMethodIsCalledOnProtocolsMock +{ + id mock = [OCMockObject mockForClass:[NSObject class] + protocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + XCTAssertThrows([mock lowercaseString], @"Should have raised an exception."); +} + +- (void)testCanMockMultipleProtocolsNicely +{ + id mock = [OCMockObject niceMockForClass:[NSObject class] + protocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + [[mock expect] lock]; + [[mock expect] primitiveValue]; + + [mock lock]; + [mock primitiveValue]; + + [mock verify]; +} + +- (void)testRaisesAnExceptionWhenAnExpectedMethodIsNotCalledOnNiceProtocolsMock +{ + id mock = [OCMockObject niceMockForClass:[NSObject class] + protocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + [[mock expect] primitiveValue]; + XCTAssertThrows([mock verify], @"Should have raised an exception because method was not called."); +} + +- (void)testProtocolClassMethod +{ + id mock = [OCMockObject mockForClass:[NSObject class] + protocols:@protocol(NSLocking), @protocol(TestProtocol), nil]; + OCMStub([mock stringValueClassMethod]).andReturn(@"stubbed"); + id result = [mock stringValueClassMethod]; + XCTAssertEqual(@"stubbed", result, @"Should have stubbed the class method."); +} @end diff --git a/Source/OCMockTests/OCMockObjectTests.m b/Source/OCMockTests/OCMockObjectTests.m index 1dfb0f1e..75c35d59 100644 --- a/Source/OCMockTests/OCMockObjectTests.m +++ b/Source/OCMockTests/OCMockObjectTests.m @@ -168,7 +168,7 @@ @implementation OCMockObjectTests - (void)setUp { - mock = [OCMockObject mockForClass:[NSString class]]; + mock = [OCMockObject mockForClass:[NSString class] protocols:nil]; } @@ -205,7 +205,7 @@ - (void)testAcceptsStubbedMethodWithConstraint - (void)testAcceptsStubbedMethodWithBlockArgument { - mock = [OCMockObject mockForClass:[NSArray class]]; + mock = [OCMockObject mockForClass:[NSArray class] protocols:nil]; [[mock stub] indexesOfObjectsPassingTest:[OCMArg any]]; [mock indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) { return YES; }]; } @@ -248,21 +248,21 @@ - (void)testRaisesExceptionWhenMethodWithOneWrongScalarArgumentIsCalled - (void)testAcceptsStubbedMethodWithSelectorArgument { - mock = [OCMockObject mockForClass:[TestClassWithSelectorMethod class]]; + mock = [OCMockObject mockForClass:[TestClassWithSelectorMethod class] protocols:nil]; [[mock stub] doWithSelector:@selector(allKeys)]; [mock doWithSelector:@selector(allKeys)]; } - (void)testRaisesExceptionWhenMethodWithWrongSelectorArgumentIsCalled { - mock = [OCMockObject mockForClass:[TestClassWithSelectorMethod class]]; + mock = [OCMockObject mockForClass:[TestClassWithSelectorMethod class] protocols:nil]; [[mock stub] doWithSelector:@selector(allKeys)]; XCTAssertThrows([mock doWithSelector:@selector(allValues)]); } - (void)testAcceptsStubbedMethodWithAnySelectorArgument { - mock = [OCMockObject mockForClass:[TestClassWithSelectorMethod class]]; + mock = [OCMockObject mockForClass:[TestClassWithSelectorMethod class] protocols:nil]; [[mock stub] doWithSelector:[OCMArg anySelector]]; [mock doWithSelector:@selector(allKeys)]; } @@ -329,7 +329,7 @@ - (void)testAcceptsStubbedMethodWithAnyObjectRefArgument - (void)testAcceptsStubbedMethodWithVoidPointerArgument { char bytes[8]; - mock = [OCMockObject mockForClass:[NSMutableData class]]; + mock = [OCMockObject mockForClass:[NSMutableData class] protocols:nil]; [[mock stub] appendBytes:bytes length:8]; [mock appendBytes:bytes length:8]; } @@ -337,7 +337,7 @@ - (void)testAcceptsStubbedMethodWithVoidPointerArgument - (void)testRaisesExceptionWhenMethodWithWrongVoidPointerArgumentIsCalled { - mock = [OCMockObject mockForClass:[NSMutableData class]]; + mock = [OCMockObject mockForClass:[NSMutableData class] protocols:nil]; [[mock stub] appendBytes:"foo" length:3]; XCTAssertThrows([mock appendBytes:"bar" length:3], @"Should have raised an exception."); } @@ -378,29 +378,29 @@ - (void)testRaisesExceptionWhenMethodWithWrongStructArgumentIsCalled - (void)testCanPassMocksAsArguments { - id mockArg = [OCMockObject mockForClass:[NSString class]]; + id mockArg = [OCMockObject mockForClass:[NSString class] protocols:nil]; [[mock stub] stringByAppendingString:[OCMArg any]]; [mock stringByAppendingString:mockArg]; } - (void)testCanStubWithMockArguments { - id mockArg = [OCMockObject mockForClass:[NSString class]]; + id mockArg = [OCMockObject mockForClass:[NSString class] protocols:nil]; [[mock stub] stringByAppendingString:mockArg]; [mock stringByAppendingString:mockArg]; } - (void)testRaisesExceptionWhenStubbedMockArgIsNotUsed { - id mockArg = [OCMockObject mockForClass:[NSString class]]; + id mockArg = [OCMockObject mockForClass:[NSString class] protocols:nil]; [[mock stub] stringByAppendingString:mockArg]; XCTAssertThrows([mock stringByAppendingString:@"foo"], @"Should have raised an exception."); } - (void)testRaisesExceptionWhenDifferentMockArgumentIsPassed { - id expectedArg = [OCMockObject mockForClass:[NSString class]]; - id otherArg = [OCMockObject mockForClass:[NSString class]]; + id expectedArg = [OCMockObject mockForClass:[NSString class] protocols:nil]; + id otherArg = [OCMockObject mockForClass:[NSString class] protocols:nil]; [[mock stub] stringByAppendingString:otherArg]; XCTAssertThrows([mock stringByAppendingString:expectedArg], @"Should have raised an exception."); } @@ -451,7 +451,7 @@ - (void)testReturnsStubbedIntReturnValue - (void)testReturnsStubbedUnsignedLongReturnValue { - mock = [OCMockObject mockForClass:[NSNumber class]]; + mock = [OCMockObject mockForClass:[NSNumber class] protocols:nil]; [[[mock expect] andReturnValue:@42LU] unsignedLongValue]; unsigned long returnValue = [mock unsignedLongValue]; XCTAssertEqual(returnValue, 42LU, @"Should have returned stubbed value."); @@ -527,7 +527,7 @@ - (void)testReturnsStubbedNilReturnValue - (void)testReturnsStubbedValueForProperty { - TestClassWithProperty *myMock = [OCMockObject mockForClass:[TestClassWithProperty class]]; + TestClassWithProperty *myMock = [OCMockObject mockForClass:[TestClassWithProperty class] protocols:nil]; [[[(id)myMock stub] andReturn:@"stubbed title"] title]; @@ -645,7 +645,7 @@ - (void)testReturnsValuesInPassByReferenceArguments - (void)testReturnsValuesInNonObjectPassByReferenceArguments { - mock = [OCMockObject mockForClass:[TestClassWithIntPointerMethod class]]; + mock = [OCMockObject mockForClass:[TestClassWithIntPointerMethod class] protocols:nil]; [[mock stub] returnValueInPointer:[OCMArg setToValue:@1234]]; int actualValue = 0; @@ -766,7 +766,7 @@ - (void)testInvokesBlockWithAllDefaultArgs - (void)testOnlyInvokesBlockWhenInvocationMatches { - mock = [OCMockObject mockForClass:[TestClassWithBlockArgMethod class]]; + mock = [OCMockObject mockForClass:[TestClassWithBlockArgMethod class] protocols:nil]; [[mock stub] doStuffWithBlock:[OCMArg invokeBlock] andString:@"foo"]; [[mock stub] doStuffWithBlock:[OCMArg any] andString:@"bar"]; __block BOOL blockWasInvoked = NO; @@ -968,21 +968,21 @@ - (void)testRejectThenExpectWithExpectationOrdering - (void)testReturnsDefaultValueWhenUnknownMethodIsCalledOnNiceClassMock { - mock = [OCMockObject niceMockForClass:[NSString class]]; + mock = [OCMockObject niceMockForClass:[NSString class] protocols:nil]; XCTAssertNil([mock lowercaseString], @"Should return nil on unexpected method call (for nice mock)."); [mock verify]; } - (void)testRaisesAnExceptionWhenAnExpectedMethodIsNotCalledOnNiceClassMock { - mock = [OCMockObject niceMockForClass:[NSString class]]; + mock = [OCMockObject niceMockForClass:[NSString class] protocols:nil]; [[[mock expect] andReturn:@"HELLO!"] uppercaseString]; XCTAssertThrows([mock verify], @"Should have raised an exception because method was not called."); } - (void)testThrowsWhenRejectedMethodIsCalledOnNiceMock { - mock = [OCMockObject niceMockForClass:[NSString class]]; + mock = [OCMockObject niceMockForClass:[NSString class] protocols:nil]; [[mock reject] uppercaseString]; XCTAssertThrows([mock uppercaseString], @"Should have complained about rejected method being called."); @@ -990,7 +990,7 @@ - (void)testThrowsWhenRejectedMethodIsCalledOnNiceMock - (void)testUncalledRejectStubDoesNotCountAsExpectation { - mock = [OCMockObject niceMockForClass:[NSString class]]; + mock = [OCMockObject niceMockForClass:[NSString class] protocols:nil]; [[mock expect] lowercaseString]; [[mock reject] uppercaseString]; @@ -1036,7 +1036,7 @@ - (void)testDoesNotReRaiseStubbedExceptions - (void)testReRaisesRejectExceptionsOnVerify { - mock = [OCMockObject niceMockForClass:[NSString class]]; + mock = [OCMockObject niceMockForClass:[NSString class] protocols:nil]; [[mock reject] uppercaseString]; @try { @@ -1073,7 +1073,7 @@ - (void)testArgumentConstraintsAreOnlyCalledAsOftenAsTheMethodIsCalled - (void)testVerifyWithDelayDoesNotWaitForRejects { - mock = [OCMockObject niceMockForClass:[NSString class]]; + mock = [OCMockObject niceMockForClass:[NSString class] protocols:nil]; [[mock reject] hasSuffix:OCMOCK_ANY]; [[mock expect] hasPrefix:OCMOCK_ANY]; diff --git a/Source/OCMockTests/OCMockObjectVerifyAfterRunTests.m b/Source/OCMockTests/OCMockObjectVerifyAfterRunTests.m index bc17fd46..86108102 100644 --- a/Source/OCMockTests/OCMockObjectVerifyAfterRunTests.m +++ b/Source/OCMockTests/OCMockObjectVerifyAfterRunTests.m @@ -64,7 +64,7 @@ @implementation OCMockObjectVerifyAfterRunTests - (void)testDoesNotThrowWhenMethodWasInvoked { - id mock = [OCMockObject niceMockForClass:[NSString class]]; + id mock = [OCMockObject niceMockForClass:[NSString class] protocols:nil]; [mock lowercaseString]; @@ -73,7 +73,7 @@ - (void)testDoesNotThrowWhenMethodWasInvoked - (void)testThrowsWhenMethodWasNotInvoked { - id mock = [OCMockObject niceMockForClass:[NSString class]]; + id mock = [OCMockObject niceMockForClass:[NSString class] protocols:nil]; [mock lowercaseString]; @@ -103,7 +103,7 @@ - (void)testDoesNotThrowWhenMethodWasInvokedOnRealObjectEvenInSuperclass - (void)testDoesNotThrowWhenClassMethodWasInvoked { - id mock = [OCMockObject niceMockForClass:[TestBaseClassForVerifyAfterRun class]]; + id mock = [OCMockObject niceMockForClass:[TestBaseClassForVerifyAfterRun class] protocols:nil]; [TestBaseClassForVerifyAfterRun classMethod1]; @@ -112,7 +112,7 @@ - (void)testDoesNotThrowWhenClassMethodWasInvoked - (void)testThrowsWhenClassMethodWasNotInvoked { - id mock = [OCMockObject niceMockForClass:[TestBaseClassForVerifyAfterRun class]]; + id mock = [OCMockObject niceMockForClass:[TestBaseClassForVerifyAfterRun class] protocols:nil]; XCTAssertThrows([[mock verify] classMethod1], @"Should not have thrown an exception for class method that was called."); } diff --git a/Source/OCMockTests/TestProtocol.h b/Source/OCMockTests/TestProtocol.h new file mode 100644 index 00000000..5b555b18 --- /dev/null +++ b/Source/OCMockTests/TestProtocol.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2005-2016 Erik Doernenburg and contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use these files except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +#import + +@protocol TestProtocol ++ (NSString *)stringValueClassMethod; +- (int)primitiveValue; +@optional +- (id)objectValue; +@end