Skip to content

Commit

Permalink
Factory Creator Implicit tests.
Browse files Browse the repository at this point in the history
Added a bunch of bogus init methods to the PaymentImpl, to better test the
guessing of the initializer for the factory method.

(appsquickly#102)
  • Loading branch information
drodriguez committed Dec 4, 2013
1 parent a2253dd commit 8a893c7
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Tests/Component/FactoryProvider/PaymentImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>)creditService startDate:(NSDate *)startDate amount:(NSUInteger)amount;

// Doesn't use as many arguments as the main one.
- (instancetype)initWithCreditService:(id<CreditService>)creditService authService:(id<AuthService>)authService startDate:(NSDate *)startDate;

// Has one argument the factory cannot fullfil
- (instancetype)initWithCreditService:(id<CreditService>)creditService
authService:(id<AuthService>)authService
startDate:(NSDate *)startDate
amount:(NSUInteger)amount
customerName:(NSString *)customerName;

@end
25 changes: 25 additions & 0 deletions Tests/Component/FactoryProvider/PaymentImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,29 @@ - (NSString *)description
_startDate, (unsigned long)_amount];
}

// Bogus implementations

- (void)initialize
{
}

- (instancetype)initWithCreditService:(id<CreditService>)creditService startDate:(NSDate *)startDate amount:(NSUInteger)amount
{
return nil;
}

- (instancetype)initWithCreditService:(id<CreditService>)creditService authService:(id<AuthService>)authService startDate:(NSDate *)startDate
{
return nil;
}

- (instancetype)initWithCreditService:(id<CreditService>)creditService
authService:(id<AuthService>)authService
startDate:(NSDate *)startDate
amount:(NSUInteger)amount
customerName:(NSString *)customerName
{
return nil;
}

@end
Original file line number Diff line number Diff line change
@@ -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 <SenTestingKit/SenTestingKit.h>
#include <objc/runtime.h>

#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> _creditService;
id<AuthService> _authService;
}

- (void)setUp
{
_creditService = (id<CreditService>)[[NSObject alloc] init];
_authService = (id<AuthService>)[[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<PaymentFactory> 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<PaymentFactory> 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<PaymentFactory> factory = [[klass alloc] init];

[(NSObject *)factory setValue:_creditService forKey:@"creditService"];
[(NSObject *)factory setValue:_authService forKey:@"authService"];

NSDate *now = [NSDate date];
id<Payment> 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
8 changes: 8 additions & 0 deletions Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -436,6 +439,7 @@
656A57651848DD020081F72B /* TyphoonFactoryProviderTestHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonFactoryProviderTestHelper.m; sourceTree = "<group>"; };
656A57691848DD260081F72B /* TyphoonFactoryProviderTestHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TyphoonFactoryProviderTestHelper.h; sourceTree = "<group>"; };
656A576A1848E0B70081F72B /* TyphoonAssistedFactoryCreatorManyFactoriesTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryCreatorManyFactoriesTest.m; sourceTree = "<group>"; };
656A576E184901010081F72B /* TyphoonAssistedFactoryCreatorImplicitTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryCreatorImplicitTest.m; sourceTree = "<group>"; };
6570968818313C3500C10DA4 /* TyphoonAssistedFactoryBaseTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryBaseTest.m; sourceTree = "<group>"; };
6570968C18313F2A00C10DA4 /* TyphoonAssistedFactoryDefinitionTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryDefinitionTest.m; sourceTree = "<group>"; };
657096901831449000C10DA4 /* TyphoonFactoryProviderTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonFactoryProviderTest.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down

0 comments on commit 8a893c7

Please sign in to comment.