From 8a893c75f527dde0e6163e4d07d2e2ebb3693db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Rodri=CC=81guez=20Troitin=CC=83o?= Date: Fri, 29 Nov 2013 19:25:18 +0100 Subject: [PATCH] Factory Creator Implicit tests. Added a bunch of bogus init methods to the PaymentImpl, to better test the guessing of the initializer for the factory method. (jasperblues/Typhoon#102) --- Tests/Component/FactoryProvider/PaymentImpl.h | 18 +++ Tests/Component/FactoryProvider/PaymentImpl.m | 25 ++++ ...yphoonAssistedFactoryCreatorImplicitTest.m | 117 ++++++++++++++++++ Tests/Tests.xcodeproj/project.pbxproj | 8 ++ 4 files changed, 168 insertions(+) create mode 100644 Tests/Component/FactoryProvider/TyphoonAssistedFactoryCreatorImplicitTest.m diff --git a/Tests/Component/FactoryProvider/PaymentImpl.h b/Tests/Component/FactoryProvider/PaymentImpl.h index 526a45372..e965f334a 100644 --- a/Tests/Component/FactoryProvider/PaymentImpl.h +++ b/Tests/Component/FactoryProvider/PaymentImpl.h @@ -21,4 +21,22 @@ startDate:(NSDate *)startDate amount:(NSUInteger)amount; +// This methods are to check some parts of the implementation + +// Starts by init, but not by init + upper case. +- (void)initialize; + +// Doesn't use as many properties as the main one. +- (instancetype)initWithCreditService:(id)creditService startDate:(NSDate *)startDate amount:(NSUInteger)amount; + +// Doesn't use as many arguments as the main one. +- (instancetype)initWithCreditService:(id)creditService authService:(id)authService startDate:(NSDate *)startDate; + +// Has one argument the factory cannot fullfil +- (instancetype)initWithCreditService:(id)creditService + authService:(id)authService + startDate:(NSDate *)startDate + amount:(NSUInteger)amount + customerName:(NSString *)customerName; + @end diff --git a/Tests/Component/FactoryProvider/PaymentImpl.m b/Tests/Component/FactoryProvider/PaymentImpl.m index 1ba47f66c..feab3b78b 100644 --- a/Tests/Component/FactoryProvider/PaymentImpl.m +++ b/Tests/Component/FactoryProvider/PaymentImpl.m @@ -38,4 +38,29 @@ - (NSString *)description _startDate, (unsigned long)_amount]; } +// Bogus implementations + +- (void)initialize +{ +} + +- (instancetype)initWithCreditService:(id)creditService startDate:(NSDate *)startDate amount:(NSUInteger)amount +{ + return nil; +} + +- (instancetype)initWithCreditService:(id)creditService authService:(id)authService startDate:(NSDate *)startDate +{ + return nil; +} + +- (instancetype)initWithCreditService:(id)creditService + authService:(id)authService + startDate:(NSDate *)startDate + amount:(NSUInteger)amount + customerName:(NSString *)customerName +{ + return nil; +} + @end diff --git a/Tests/Component/FactoryProvider/TyphoonAssistedFactoryCreatorImplicitTest.m b/Tests/Component/FactoryProvider/TyphoonAssistedFactoryCreatorImplicitTest.m new file mode 100644 index 000000000..573b388a6 --- /dev/null +++ b/Tests/Component/FactoryProvider/TyphoonAssistedFactoryCreatorImplicitTest.m @@ -0,0 +1,117 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// TYPHOON FRAMEWORK +// Copyright 2013, Jasper Blues & Contributors +// All Rights Reserved. +// +// NOTICE: The authors permit you to use, modify, and distribute this file +// in accordance with the terms of the license agreement accompanying it. +// +//////////////////////////////////////////////////////////////////////////////// + +#import "TyphoonAssistedFactoryCreatorImplicit.h" + +#import +#include + +#import "TyphoonFactoryProviderTestHelper.h" + +#import "AuthService.h" +#import "CreditService.h" +#import "PaymentFactory.h" +#import "PaymentImpl.h" +#import "TyphoonAssistedFactoryBase.h" +#import "TyphoonDefinition.h" + + +@interface TyphoonAssistedFactoryCreatorImplicitTest : SenTestCase +@end + +@implementation TyphoonAssistedFactoryCreatorImplicitTest +{ + Protocol *_paymentFactoryProtocol; + Class _paymentFactoryClass; + + id _creditService; + id _authService; +} + +- (void)setUp +{ + _creditService = (id)[[NSObject alloc] init]; + _authService = (id)[[NSObject alloc] init]; +} + +- (Protocol *)paymentFactoryProtocol +{ + if (!_paymentFactoryProtocol) + { + _paymentFactoryProtocol = protocol_clone(@protocol(PaymentFactory)); + } + + return _paymentFactoryProtocol; +} + +- (Class)paymentFactoryClass +{ + if (!_paymentFactoryClass) + { + _paymentFactoryClass = [[[TyphoonAssistedFactoryCreatorImplicit alloc] + initWithProtocol:[self paymentFactoryProtocol] + returns:[PaymentImpl class]] + factoryClass]; + } + + return _paymentFactoryClass; +} + +- (void)test_factory_class_should_implement_protocol +{ + Class klass = [self paymentFactoryClass]; + + assertThatBool(class_conformsToProtocol(klass, [self paymentFactoryProtocol]), is(equalToBool(YES))); + + Class superklass = class_getSuperclass(klass); + assertThat(superklass, is([TyphoonAssistedFactoryBase class])); +} + +- (void)test_factory_should_respond_to_properties +{ + Class klass = [self paymentFactoryClass]; + id factory = [[klass alloc] init]; + + assertThatBool([factory respondsToSelector:@selector(creditService)], is(equalToBool(YES))); + assertThatBool([factory respondsToSelector:@selector(setCreditService:)], is(equalToBool(YES))); + assertThatBool([factory respondsToSelector:@selector(authService)], is(equalToBool(YES))); + assertThatBool([factory respondsToSelector:@selector(setAuthService:)], is(equalToBool(YES))); +} + +- (void)test_factory_should_implement_properties +{ + Class klass = [self paymentFactoryClass]; + id factory = [[klass alloc] init]; + + [(NSObject *)factory setValue:_creditService forKey:@"creditService"]; + [(NSObject *)factory setValue:_authService forKey:@"authService"]; + assertThat(factory.creditService, is(_creditService)); + assertThat(factory.authService, is(_authService)); +} + +- (void)test_factory_should_invoke_correct_method_blocks +{ + Class klass = [self paymentFactoryClass]; + id factory = [[klass alloc] init]; + + [(NSObject *)factory setValue:_creditService forKey:@"creditService"]; + [(NSObject *)factory setValue:_authService forKey:@"authService"]; + + NSDate *now = [NSDate date]; + id payment = [factory paymentWithStartDate:now amount:456]; + + assertThat(payment.creditService, is(_creditService)); + assertThat(payment.authService, is(_authService)); + assertThat(payment.startDate, is(now)); + assertThatInteger(payment.amount, is(equalToInteger(456))); +} + +@end diff --git a/Tests/Tests.xcodeproj/project.pbxproj b/Tests/Tests.xcodeproj/project.pbxproj index 0f750349a..8a5ccebf5 100644 --- a/Tests/Tests.xcodeproj/project.pbxproj +++ b/Tests/Tests.xcodeproj/project.pbxproj @@ -222,6 +222,9 @@ 656A576B1848E0B70081F72B /* TyphoonAssistedFactoryCreatorManyFactoriesTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 656A576A1848E0B70081F72B /* TyphoonAssistedFactoryCreatorManyFactoriesTest.m */; }; 656A576C1848E0B70081F72B /* TyphoonAssistedFactoryCreatorManyFactoriesTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 656A576A1848E0B70081F72B /* TyphoonAssistedFactoryCreatorManyFactoriesTest.m */; }; 656A576D1848E0B70081F72B /* TyphoonAssistedFactoryCreatorManyFactoriesTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 656A576A1848E0B70081F72B /* TyphoonAssistedFactoryCreatorManyFactoriesTest.m */; }; + 656A576F184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 656A576E184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m */; }; + 656A5770184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 656A576E184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m */; }; + 656A5771184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 656A576E184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m */; }; 6570968918313C3500C10DA4 /* TyphoonAssistedFactoryBaseTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 6570968818313C3500C10DA4 /* TyphoonAssistedFactoryBaseTest.m */; }; 6570968A18313C3500C10DA4 /* TyphoonAssistedFactoryBaseTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 6570968818313C3500C10DA4 /* TyphoonAssistedFactoryBaseTest.m */; }; 6570968B18313C3500C10DA4 /* TyphoonAssistedFactoryBaseTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 6570968818313C3500C10DA4 /* TyphoonAssistedFactoryBaseTest.m */; }; @@ -436,6 +439,7 @@ 656A57651848DD020081F72B /* TyphoonFactoryProviderTestHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonFactoryProviderTestHelper.m; sourceTree = ""; }; 656A57691848DD260081F72B /* TyphoonFactoryProviderTestHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TyphoonFactoryProviderTestHelper.h; sourceTree = ""; }; 656A576A1848E0B70081F72B /* TyphoonAssistedFactoryCreatorManyFactoriesTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryCreatorManyFactoriesTest.m; sourceTree = ""; }; + 656A576E184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryCreatorImplicitTest.m; sourceTree = ""; }; 6570968818313C3500C10DA4 /* TyphoonAssistedFactoryBaseTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryBaseTest.m; sourceTree = ""; }; 6570968C18313F2A00C10DA4 /* TyphoonAssistedFactoryDefinitionTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryDefinitionTest.m; sourceTree = ""; }; 657096901831449000C10DA4 /* TyphoonFactoryProviderTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonFactoryProviderTest.m; sourceTree = ""; }; @@ -881,6 +885,7 @@ 656A57651848DD020081F72B /* TyphoonFactoryProviderTestHelper.m */, 656A57611848DCB60081F72B /* TyphoonAssistedFactoryCreatorOneFactoryTest.m */, 656A576A1848E0B70081F72B /* TyphoonAssistedFactoryCreatorManyFactoriesTest.m */, + 656A576E184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m */, 6570968C18313F2A00C10DA4 /* TyphoonAssistedFactoryDefinitionTest.m */, 6570968818313C3500C10DA4 /* TyphoonAssistedFactoryBaseTest.m */, 65FB9411183BD5330038FCA5 /* TyphoonAssistedFactoryMethodInitializerTest.m */, @@ -1271,6 +1276,7 @@ 4E08794717B6762E004B86C9 /* NotSingletonA.m in Sources */, 46877CFA17D70B5900910D6A /* TyphoonParameterInjectedAsCollectionTests.m in Sources */, 6570969E183146E800C10DA4 /* AuthServiceImpl.m in Sources */, + 656A576F184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m in Sources */, 4E47A45817D8E1C700F967CA /* PrototypeInitInjected.m in Sources */, 65DD3035183D352300FD1134 /* TyphoonAssistedFactoryMethodClosureTest.m in Sources */, 4E47A45D17D8E1DC00F967CA /* PrototypePropertyInjected.m in Sources */, @@ -1363,6 +1369,7 @@ 4EFAF60A17E0D4EF00E93ACF /* CROSingletonA.m in Sources */, 4EFAF60F17E0D50B00E93ACF /* CROPrototypeA.m in Sources */, 4EFAF61417E0D54700E93ACF /* CROPrototypeB.m in Sources */, + 656A5770184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m in Sources */, 65709699183146A800C10DA4 /* CreditServiceImpl.m in Sources */, 65DD3036183D352300FD1134 /* TyphoonAssistedFactoryMethodClosureTest.m in Sources */, 6570968A18313C3500C10DA4 /* TyphoonAssistedFactoryBaseTest.m in Sources */, @@ -1433,6 +1440,7 @@ 4E08794917B6762E004B86C9 /* NotSingletonA.m in Sources */, 46877CFC17D70B5900910D6A /* TyphoonParameterInjectedAsCollectionTests.m in Sources */, 657096A0183146E800C10DA4 /* AuthServiceImpl.m in Sources */, + 656A5771184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m in Sources */, 4E47A45A17D8E1C700F967CA /* PrototypeInitInjected.m in Sources */, 65DD3037183D352300FD1134 /* TyphoonAssistedFactoryMethodClosureTest.m in Sources */, 4E47A45F17D8E1DC00F967CA /* PrototypePropertyInjected.m in Sources */,