Skip to content

Commit

Permalink
Add ComponentFactoryTests directly exercising definition inheritance.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhgills committed Nov 15, 2013
1 parent cce18f8 commit ce4e4e8
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Tests/Factory/ClassWithConstructor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by Robert Gilliam on 11/15/13.
//


#import <Foundation/Foundation.h>


@interface ClassWithConstructor : NSObject

+ (instancetype)constructorWithString:(NSString*)string;

- (instancetype)initWithString:(NSString*)string;

@property(readonly, nonatomic) NSString *string;

@end
27 changes: 27 additions & 0 deletions Tests/Factory/ClassWithConstructor.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by Robert Gilliam on 11/15/13.
//


#import "ClassWithConstructor.h"


@implementation ClassWithConstructor

+ (instancetype)constructorWithString:(NSString*)string
{
return [[self alloc] initWithString:string];
}

- (instancetype)initWithString:(NSString*)string
{
self = [super init];
if (self)
{
_string = string;
}

return self;
}

@end
117 changes: 116 additions & 1 deletion Tests/Factory/TyphoonComponentFactoryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#import "Harlot.h"
#import "TyphoonComponentFactoryPostProcessorMock.h"
#import "TyphoonPatcher.h"
#import "TyphoonComponentFactory+TyphoonDefinitionRegisterer.h"
#import "ClassWithConstructor.h"


static NSString* const DEFAULT_QUEST = @"quest";

Expand Down Expand Up @@ -322,5 +325,117 @@ - (void)test_load_singleton
assertThatUnsignedInteger([[_componentFactory singletons] count], is(@1));
}

#pragma mark - Definition Inheritance

- (void)test_child_missing_initializer_inherits_parent_initializer_by_definition
{
TyphoonDefinition* parentDefinition =
[self registerParentDefinitionWithClass:[ClassWithConstructor class] key:nil initializerString:@"parentArgument"];
TyphoonDefinition* childDefinition = [TyphoonDefinition withClass:[ClassWithConstructor class] properties:^(TyphoonDefinition* definition)
{
definition.parent = parentDefinition;
}];
[_componentFactory register:childDefinition];

ClassWithConstructor* child = [_componentFactory objectForDefinition:childDefinition];

assertThat([child string], equalTo(@"parentArgument"));
}

- (void)test_child_missing_initializer_inherits_parent_initializer_by_ref
{
[self registerParentDefinitionWithClass:[ClassWithConstructor class] key:@"parentRef" initializerString:@"parentArgument"];
TyphoonDefinition* childDefinition = [TyphoonDefinition withClass:[ClassWithConstructor class] properties:^(TyphoonDefinition* definition)
{
definition.parentRef = @"parentRef";
}];
[_componentFactory register:childDefinition];

ClassWithConstructor* child = [_componentFactory objectForDefinition:childDefinition];

assertThat([child string], equalTo(@"parentArgument"));
}

- (void)test_child_initializer_overrides_parent_initializer_by_definition
{
TyphoonDefinition* parentDefinition =
[self registerParentDefinitionWithClass:[ClassWithConstructor class] initializerString:@"parentArgument"];
TyphoonDefinition* childDefinition =
[self registerChildDefinitionWithClass:[ClassWithConstructor class] parentDefinition:parentDefinition initializerString:@"childArgument"];

ClassWithConstructor* child = [_componentFactory objectForDefinition:childDefinition];

assertThat([child string], equalTo(@"childArgument"));
}

- (void)test_child_initializer_overrides_parent_initializer_by_ref
{
[self registerParentDefinitionWithClass:[ClassWithConstructor class] key:@"parentRef" initializerString:@"parentArgument"];
TyphoonDefinition* childDefinition =
[self registerChildDefinitionWithClass:[ClassWithConstructor class] parentRef:@"parentRef" initializerString:@"childArgument"];

ClassWithConstructor* child = [_componentFactory objectForDefinition:childDefinition];

assertThat([child string], equalTo(@"childArgument"));
}

#pragma mark - Test Utility Methods
- (TyphoonDefinition*)registerParentDefinitionWithClass:(Class)pClass initializerString:(NSString*)string
{
TyphoonDefinition* parentDefinition = [TyphoonDefinition withClass:pClass key:nil];

TyphoonInitializer* initializer = [[TyphoonInitializer alloc] init];
initializer.selector = @selector(initWithString:);
[initializer injectWithValueAsText:string requiredTypeOrNil:[NSString class]];
parentDefinition.initializer = initializer;

[_componentFactory register:parentDefinition];

return parentDefinition;
}

- (TyphoonDefinition*)registerParentDefinitionWithClass:(Class)pClass key:(NSString*)key initializerString:(NSString*)string
{
TyphoonDefinition* parentDefinition = [TyphoonDefinition withClass:pClass key:key];

TyphoonInitializer* initializer = [[TyphoonInitializer alloc] init];
initializer.selector = @selector(initWithString:);
[initializer injectWithValueAsText:string requiredTypeOrNil:[NSString class]];
parentDefinition.initializer = initializer;

[_componentFactory register:parentDefinition];

return parentDefinition;
}

- (TyphoonDefinition*)registerChildDefinitionWithClass:(Class)pClass parentDefinition:(TyphoonDefinition*)parentDefinition initializerString:(NSString*)string
{
return [self registerChildDefinitionWithClass:pClass parentDefinition:parentDefinition parentRef:nil initializerString:string];
}

@end
- (TyphoonDefinition*)registerChildDefinitionWithClass:(Class)pClass parentRef:(NSString *)parentRef initializerString:(NSString*)string
{
return [self registerChildDefinitionWithClass:pClass parentDefinition:nil parentRef:parentRef initializerString:string];
}

- (TyphoonDefinition*)registerChildDefinitionWithClass:(Class)pClass parentDefinition:(TyphoonDefinition*)parentDefinition parentRef:(NSString *)parentRef initializerString:(NSString*)string
{
TyphoonDefinition* childDefinition = [TyphoonDefinition withClass:pClass initialization:^(TyphoonInitializer* initializer) {
initializer.selector = @selector(initWithString:);

[initializer injectWithValueAsText:string requiredTypeOrNil:[NSString class]];
} properties:^(TyphoonDefinition* definition)
{
if (parentDefinition) {
definition.parent = parentDefinition;
}else if (parentRef) {
definition.parentRef = parentRef;
}

}];
[_componentFactory register:childDefinition];

return childDefinition;
}

@end
10 changes: 10 additions & 0 deletions Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,17 @@
75AB524F2957B86EAAF2FA4C /* SomeResource in Resources */ = {isa = PBXBuildFile; fileRef = 75AB51A490B3C70B90B70509 /* SomeResource */; };
75AB5358FDF8574DDA34625D /* ErrandQuest.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5AABD65CAC43879BC428 /* ErrandQuest.m */; };
75AB53AE7325B44FF5D9BEB1 /* SomeResource in Resources */ = {isa = PBXBuildFile; fileRef = 75AB51A490B3C70B90B70509 /* SomeResource */; };
75AB5544FD01F8EAC430F2B1 /* ClassWithConstructor.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB57167D2468CBE686FFB4 /* ClassWithConstructor.m */; };
75AB5564135F1DD4F38FE7E3 /* DefinitionInheritanceAssembly.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5F9A45E27B20DA83B14C /* DefinitionInheritanceAssembly.m */; };
75AB556C969F1825EA08E75E /* DefinitionInheritanceAssembly.xml in Resources */ = {isa = PBXBuildFile; fileRef = 75AB518FA6E12B0CBF01B9F2 /* DefinitionInheritanceAssembly.xml */; };
75AB55C8D5B02E582100222D /* ErrandQuest.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5AABD65CAC43879BC428 /* ErrandQuest.m */; };
75AB59214CC8EE68422A4E55 /* Widget.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5D2539493CC97348B566 /* Widget.m */; };
75AB59FA0850F12E70FF6FED /* ClassWithConstructor.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB57167D2468CBE686FFB4 /* ClassWithConstructor.m */; };
75AB5BFACB0968C880D9BFAD /* DefinitionInheritanceAssembly.xml in Resources */ = {isa = PBXBuildFile; fileRef = 75AB518FA6E12B0CBF01B9F2 /* DefinitionInheritanceAssembly.xml */; };
75AB5C0DFEBEC0E66DB56A59 /* Widget.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5D2539493CC97348B566 /* Widget.m */; };
75AB5C9007480EF4EE9A9A4D /* DefinitionInheritanceAssembly.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5F9A45E27B20DA83B14C /* DefinitionInheritanceAssembly.m */; };
75AB5F3B06BCF48842608B0A /* ErrandQuest.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5AABD65CAC43879BC428 /* ErrandQuest.m */; };
75AB5F9077B5A6786531E545 /* ClassWithConstructor.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB57167D2468CBE686FFB4 /* ClassWithConstructor.m */; };
75AB5FE1CFE14C6E9CAA87A6 /* TyphoonXMLBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB58BCF87EC527F127BCA0 /* TyphoonXMLBuilder.m */; };
BA79824D0006B0F9E50A41D3 /* TyphoonStringUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = BA79854F29FE8A327A82E8BC /* TyphoonStringUtilsTests.m */; };
BA798380AD1E1F4160A4719C /* CollaboratingMiddleAgesAssembly.m in Sources */ = {isa = PBXBuildFile; fileRef = BA79870B653F1DB13DCE8543 /* CollaboratingMiddleAgesAssembly.m */; };
Expand Down Expand Up @@ -432,6 +435,8 @@
75AB50AE37854D669642EAB6 /* ErrandQuest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ErrandQuest.h; sourceTree = "<group>"; };
75AB518FA6E12B0CBF01B9F2 /* DefinitionInheritanceAssembly.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = DefinitionInheritanceAssembly.xml; sourceTree = "<group>"; };
75AB51A490B3C70B90B70509 /* SomeResource */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = SomeResource; sourceTree = "<group>"; };
75AB55CE0D48666BB5DCBA34 /* ClassWithConstructor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ClassWithConstructor.h; sourceTree = "<group>"; };
75AB57167D2468CBE686FFB4 /* ClassWithConstructor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ClassWithConstructor.m; sourceTree = "<group>"; };
75AB58BCF87EC527F127BCA0 /* TyphoonXMLBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonXMLBuilder.m; sourceTree = "<group>"; };
75AB5AABD65CAC43879BC428 /* ErrandQuest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ErrandQuest.m; sourceTree = "<group>"; };
75AB5D2539493CC97348B566 /* Widget.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Widget.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -650,6 +655,8 @@
4B8414B2179B0EA600916BF5 /* TyphoonComponentFactory+InstanceBuilderTests.m */,
4B8414B3179B0EA600916BF5 /* TyphoonComponentFactoryTests.m */,
4B8414B4179B0EA600916BF5 /* Xml */,
75AB57167D2468CBE686FFB4 /* ClassWithConstructor.m */,
75AB55CE0D48666BB5DCBA34 /* ClassWithConstructor.h */,
);
path = Factory;
sourceTree = "<group>";
Expand Down Expand Up @@ -1257,6 +1264,7 @@
75AB59214CC8EE68422A4E55 /* Widget.m in Sources */,
75AB5358FDF8574DDA34625D /* ErrandQuest.m in Sources */,
75AB500AD2436D305C0E1B37 /* TyphoonXMLBuilder.m in Sources */,
75AB5544FD01F8EAC430F2B1 /* ClassWithConstructor.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1331,6 +1339,7 @@
75AB507EAF7EB9829C0C60C5 /* Widget.m in Sources */,
75AB55C8D5B02E582100222D /* ErrandQuest.m in Sources */,
75AB5FE1CFE14C6E9CAA87A6 /* TyphoonXMLBuilder.m in Sources */,
75AB59FA0850F12E70FF6FED /* ClassWithConstructor.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1407,6 +1416,7 @@
75AB5C0DFEBEC0E66DB56A59 /* Widget.m in Sources */,
75AB5F3B06BCF48842608B0A /* ErrandQuest.m in Sources */,
75AB5157197D6CD701F214A6 /* TyphoonXMLBuilder.m in Sources */,
75AB5F9077B5A6786531E545 /* ClassWithConstructor.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit ce4e4e8

Please sign in to comment.