From 8b27b5f52ac1d7bd4507e5415420182d1a85e4ef Mon Sep 17 00:00:00 2001 From: bryn Date: Thu, 9 May 2013 11:47:04 +0100 Subject: [PATCH] Fix injectProperties for subclasses and added unit test --- Source/Factory/TyphoonComponentFactory.m | 2 +- Tests/Factory/TyphoonComponentFactoryTests.m | 26 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Source/Factory/TyphoonComponentFactory.m b/Source/Factory/TyphoonComponentFactory.m index 00ff3563c..b72795760 100644 --- a/Source/Factory/TyphoonComponentFactory.m +++ b/Source/Factory/TyphoonComponentFactory.m @@ -168,7 +168,7 @@ - (void)injectProperties:(id)instance { Class class = [instance class]; for (TyphoonDefinition* definition in _registry) { - if(definition.type == class || [definition.type isSubclassOfClass:class]) + if(definition.type == class || [class isSubclassOfClass:definition.type]) { [self injectPropertyDependenciesOn:instance withDefinition:definition]; } diff --git a/Tests/Factory/TyphoonComponentFactoryTests.m b/Tests/Factory/TyphoonComponentFactoryTests.m index e6652f41f..7534a1847 100644 --- a/Tests/Factory/TyphoonComponentFactoryTests.m +++ b/Tests/Factory/TyphoonComponentFactoryTests.m @@ -221,4 +221,30 @@ - (void)test_injectProperties } +- (void)test_injectProperties_subclassing +{ + [_componentFactory register:[TyphoonDefinition withClass:[Knight class] properties:^(TyphoonDefinition* definition) + { + [definition injectProperty:@selector(quest)]; + }]]; + [_componentFactory register:[TyphoonDefinition withClass:[CavalryMan class] properties:^(TyphoonDefinition* definition) + { + [definition injectProperty:@selector(hitRatio) withValueAsText:@"3.0"]; + }]]; + [_componentFactory register:[TyphoonDefinition withClass:[CampaignQuest class] key:@"quest"]]; + + CavalryMan* cavelryMan = [[CavalryMan alloc] init]; + [_componentFactory injectProperties:cavelryMan]; + + assertThat(cavelryMan.quest, notNilValue()); + assertThatFloat(cavelryMan.hitRatio, equalToFloat(3.0f)); + + Knight* knight = [[Knight alloc] init]; + [_componentFactory injectProperties:knight]; + + assertThat(knight.quest, notNilValue()); + +} + + @end \ No newline at end of file