Skip to content

Commit

Permalink
added feature as requested in ticket #224
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarbarev committed May 30, 2014
1 parent 475d041 commit 815ad64
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Source/Definition/Injections/TyphoonInjectionByType.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ - (void)valueToInjectWithContext:(TyphoonInjectionContext *)context completion:(

if (!classOrProtocol) {
if (self.type == TyphoonInjectionTypeProperty) {
[NSException raise:NSInternalInconsistencyException format:@"Can't recognize type for property '%@' of class '%@'. Make sure that property exists and has correct type.", self.propertyName, context.destinationInstanceClass];
[NSException raise:NSInternalInconsistencyException format:@"Can't recognize type for property '%@' of class '%@'. Make sure that @property exists and has correct type.", self.propertyName, context.destinationInstanceClass];
} else {
[NSException raise:NSInternalInconsistencyException format:@"Only property injection support InjectionByType"];

Expand Down
8 changes: 7 additions & 1 deletion Source/Utils/TyphoonIntrospectionUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ + (SEL)setterForPropertyWithName:(NSString *)propertyName inClass:(Class)clazz

objc_property_t property = class_getProperty(clazz, [propertyName cStringUsingEncoding:NSASCIIStringEncoding]);
if (property) {

const char *attributes = property_getAttributes(property);

NSString *attributesString = [NSString stringWithCString:attributes encoding:NSASCIIStringEncoding];
Expand All @@ -65,6 +64,13 @@ + (SEL)setterForPropertyWithName:(NSString *)propertyName inClass:(Class)clazz
setterSelector = NSSelectorFromString(selectorString);
}
}
else if (propertyName.length > 0) {
NSString *selectorString = [self defaultSetterForPropertyWithName:propertyName];
SEL aSelector = NSSelectorFromString(selectorString);
if (class_getInstanceMethod(clazz, aSelector)) {
setterSelector = aSelector;
}
}

return setterSelector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ - (void)test_method_injection

}

- (void)test_fake_property_injection
{
Knight *knight = [(MiddleAgesAssembly *) _componentFactory knightWithFakePropertyQuest];

XCTAssertNotNil(knight.favoriteQuest);
}

- (void)test_fake_property_injection_by_type
{
XCTAssertThrows([(MiddleAgesAssembly *) _componentFactory knightWithFakePropertyQuestByType]);
}

/* ====================================================================================================================================== */
#pragma mark Class Method Initializer

Expand Down
3 changes: 3 additions & 0 deletions Tests/Factory/Block/TestAssemblies/MiddleAgesAssembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@

- (id)knightWithFoobar:(NSString *)foobar;

- (id)knightWithFakePropertyQuest;

- (id)knightWithFakePropertyQuestByType;
@end
14 changes: 14 additions & 0 deletions Tests/Factory/Block/TestAssemblies/MiddleAgesAssembly.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,18 @@ - (id)knightWithFoobar:(NSString *)foobar
}];
}

- (id)knightWithFakePropertyQuest
{
return [TyphoonDefinition withClass:[Knight class] configuration:^(TyphoonDefinition *definition) {
[definition injectProperty:@selector(favoriteQuest) with:[self defaultQuest]];
}];
}

- (id)knightWithFakePropertyQuestByType
{
return [TyphoonDefinition withClass:[Knight class] configuration:^(TyphoonDefinition *definition) {
[definition injectProperty:@selector(favoriteQuest)];
}];
}

@end
5 changes: 5 additions & 0 deletions Tests/Model/Knight.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@

+ (id)knightWithDamselsRescued:(NSUInteger)damselsRescued;

- (void)setFavoriteQuest:(id<Quest>)quest;

- (id<Quest>)favoriteQuest;


@end
13 changes: 13 additions & 0 deletions Tests/Model/Knight.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@


@implementation Knight
{
id <Quest> _favoriteQuest;
}

/* ============================================================ Initializers ============================================================ */
- (id)initWithQuest:(id <Quest>)quest
Expand Down Expand Up @@ -66,6 +69,16 @@ + (id)knightWithDamselsRescued:(NSUInteger)damselsRescued
return [[[self class] alloc] initWithDamselsRescued:damselsRescued foo:nil];
}

- (void)setFavoriteQuest:(id <Quest>)quest
{
_favoriteQuest = quest;
}

- (id <Quest>)favoriteQuest
{
return _favoriteQuest;
}

/* ========================================================== Interface Methods ========================================================= */

- (void)setQuest:(id<Quest>)quest andDamselsRescued:(NSUInteger)damselsRescued
Expand Down

0 comments on commit 815ad64

Please sign in to comment.