Skip to content

Commit

Permalink
Only set properties on the TyphoonAssembly to TyphoonCollaboratingAss…
Browse files Browse the repository at this point in the history
…emblyProxy if they are of type TyphoonAssembly.

Improve TyphoonTypeDescriptor to correctly handle properties with both a non-id object type and a protocol.
  • Loading branch information
rhgills committed Jan 10, 2014
1 parent dec03f2 commit 58e5852
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 8 deletions.
4 changes: 0 additions & 4 deletions Source/Factory/Block/TyphoonAssembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ AnalyticsService* service = [assembly analyticsService];
* - Avoids the use of "magic strings" for component resolution and wiring
* - Allows the use of IDE features like refactoring and code completion.
*
* @warning
* The use of properties on an Assembly for any means other than collaborating assemblies is currently unsupported. Any user set property
* values may be overwritten.
*
*/
@interface TyphoonAssembly : NSObject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#import "TyphoonCollaboratingAssemblyPropertyEnumerator.h"
#import "TyphoonAssembly.h"
#import <objc/runtime.h>
#import <Typhoon/TyphoonTypeDescriptor.h>
#import <Typhoon/TyphoonIntrospectionUtils.h>
#import <Typhoon/TyphoonCollaboratingAssemblyProxy.h>

@implementation TyphoonCollaboratingAssemblyPropertyEnumerator
{
Expand All @@ -40,7 +43,11 @@ - (NSSet *)collaboratingAssemblyProperties
objc_property_t * properties = class_copyPropertyList(class, &count);
for (int propertyIndex = 0; propertyIndex < count; propertyIndex++) {
objc_property_t aProperty = properties[propertyIndex];
[propertyNames addObject:[self propertyNameForProperty:aProperty]];

NSString *propertyName = [self propertyNameForProperty:aProperty];
if ([self propertyForName:propertyName isCollaboratingAssemblyPropertyOnClass:class]) {
[propertyNames addObject:propertyName];
}
}

class = class_getSuperclass(class);
Expand All @@ -49,6 +56,12 @@ - (NSSet *)collaboratingAssemblyProperties
return propertyNames;
}

- (BOOL)propertyForName:(NSString*)propertyName isCollaboratingAssemblyPropertyOnClass:(Class)class
{
TyphoonTypeDescriptor* type = [TyphoonIntrospectionUtils typeForPropertyWithName:propertyName inClass:class];
return type.typeBeingDescribed == [TyphoonAssembly class];
}

- (BOOL)classNotRootAssemblyClass:(Class)class
{
return class != [TyphoonAssembly class];
Expand Down
17 changes: 16 additions & 1 deletion Source/TypeConversion/TyphoonTypeDescriptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ - (id)initWithTypeCode:(NSString*)typeCode
typeCode = [typeCode stringByReplacingOccurrencesOfString:@">" withString:@""];
_protocol = NSProtocolFromString(typeCode);
}
else if ([typeCode hasSuffix:@">"])
{
NSArray* components = [typeCode componentsSeparatedByString:@"<"];
NSString *protocol =
[components[1] stringByReplacingOccurrencesOfString:@">" withString:@""];
NSString *class = components[0];

_protocol = NSProtocolFromString(protocol);
_typeBeingDescribed = NSClassFromString(class);
}
else
{
_typeBeingDescribed = NSClassFromString(typeCode);
Expand Down Expand Up @@ -120,7 +130,12 @@ - (NSString*)description
else
{
Protocol* protocol = [self protocol];
if (protocol)
if (protocol && [self typeBeingDescribed])
{
return [NSString stringWithFormat:@"Type descriptor: %@<%@>", NSStringFromClass([self typeBeingDescribed]),
NSStringFromProtocol(protocol)];
}
else if (protocol)
{
return [NSString stringWithFormat:@"Type descriptor: id<%@>", NSStringFromProtocol(protocol)];
}
Expand Down
3 changes: 2 additions & 1 deletion Source/Utils/TyphoonIntrospectionUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ + (TyphoonTypeDescriptor*)typeForPropertyWithName:(NSString*)propertyName inClas
memcpy( buffer, attrs, len );
buffer[len] = '\0';

typeDescriptor = [TyphoonTypeDescriptor descriptorWithTypeCode:[NSString stringWithCString:buffer encoding:NSUTF8StringEncoding]];
NSString *typeCode = [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
typeDescriptor = [TyphoonTypeDescriptor descriptorWithTypeCode:typeCode];
}
return typeDescriptor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "CollaboratingMiddleAgesAssembly.h"
#import "ExtendedMiddleAgesAssembly.h"
#import "ExtendedSimpleAssembly.h"
#import "TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.h"

@interface TyphoonCollaboratingAssemblyPropertyEnumeratorTests : SenTestCase
@end
Expand All @@ -26,7 +27,7 @@ @implementation TyphoonCollaboratingAssemblyPropertyEnumeratorTests

}

- (void)test_assembly_property
- (void)test_assembly_property_implements_protocol
{
TyphoonCollaboratingAssemblyPropertyEnumerator* enumerator = [[TyphoonCollaboratingAssemblyPropertyEnumerator alloc] initWithAssembly:[CollaboratingMiddleAgesAssembly assembly]];
assertThat([enumerator collaboratingAssemblyProperties], onlyContains(@"quests", nil));
Expand All @@ -38,4 +39,10 @@ - (void)test_assembly_properties_include_superclasses
assertThat([enumerator collaboratingAssemblyProperties], containsInAnyOrder(@"assemblyA", @"assemblyB", nil));
}

- (void)test_excludes_non_assembly_types
{
TyphoonCollaboratingAssemblyPropertyEnumerator* enumerator = [[TyphoonCollaboratingAssemblyPropertyEnumerator alloc] initWithAssembly:[AssemblyWithProperty assembly]];
assertThat([enumerator collaboratingAssemblyProperties], onlyContains(@"assembly", nil));
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Created by Robert Gilliam on 1/9/14.
//

#import <Foundation/Foundation.h>
#import <Typhoon/TyphoonAssembly.h>


@interface AssemblyWithProperty : TyphoonAssembly

@property TyphoonAssembly *assembly;

@property NSString *name;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by Robert Gilliam on 1/9/14.
//

#import "TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.h"


@implementation AssemblyWithProperty
{

}
@end
10 changes: 10 additions & 0 deletions Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
75AB5105A442B91C8829135E /* TyphoonBlockComponentFactory_OverridingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5106A7430A8EF5A6A292 /* TyphoonBlockComponentFactory_OverridingTests.m */; };
75AB5133B0D948DF521F1D61 /* TyphoonAssemblyAdviserTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5DFD1312598D60881E9D /* TyphoonAssemblyAdviserTests.m */; };
75AB5157197D6CD701F214A6 /* TyphoonXMLBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB58BCF87EC527F127BCA0 /* TyphoonXMLBuilder.m */; };
75AB51C098497FB1A9B4C75E /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5057948C5EF8D86D932B /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m */; };
75AB51CC7927741FB7007B1A /* SimpleAssembly.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5D85C9AE1DF0947C025C /* SimpleAssembly.m */; };
75AB51D4CBB1B5127FA29B05 /* SimpleAssembly.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5D85C9AE1DF0947C025C /* SimpleAssembly.m */; };
75AB51DE498D6CE325B20A43 /* TyphoonTestMethodSwizzler.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5AF13A0C6960E99FAAD6 /* TyphoonTestMethodSwizzler.m */; };
Expand All @@ -266,6 +267,7 @@
75AB56689AE148926B861287 /* TyphoonFakeLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5A95775B4E5C8356BB0C /* TyphoonFakeLogger.m */; };
75AB56AA46B0B69D660BC83A /* TyphoonFakeLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5A95775B4E5C8356BB0C /* TyphoonFakeLogger.m */; };
75AB56EB1C4A7565C016E5B4 /* InvalidCollaboratingAssembly.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5663291C06765BB0EFC5 /* InvalidCollaboratingAssembly.m */; };
75AB5702F372BD7FCF58FB19 /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5057948C5EF8D86D932B /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m */; };
75AB572BB151F2951995C3AF /* InvalidCollaboratingAssembly.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5663291C06765BB0EFC5 /* InvalidCollaboratingAssembly.m */; };
75AB5771B87F643C80AAC752 /* TyphoonBlockComponentFactoryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5BF1A64523976ACB3EC9 /* TyphoonBlockComponentFactoryTests.m */; };
75AB589C5495BDA584D85353 /* TyphoonAssemblyAdviserTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5DFD1312598D60881E9D /* TyphoonAssemblyAdviserTests.m */; };
Expand All @@ -286,6 +288,7 @@
75AB5CC31504CE8D3B7092FC /* TyphoonTestMethodSwizzler.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5AF13A0C6960E99FAAD6 /* TyphoonTestMethodSwizzler.m */; };
75AB5D59890D9F4DFF48EE63 /* TyphoonInitializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB57037E106B97B8F22B79 /* TyphoonInitializerTests.m */; };
75AB5E14781FA2CB8F6C29EA /* TyphoonBlockComponentFactory_InvalidCollaboratingAssembliesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5286A84793B6B45E3A08 /* TyphoonBlockComponentFactory_InvalidCollaboratingAssembliesTests.m */; };
75AB5E3BBB07021C773A20C5 /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5057948C5EF8D86D932B /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m */; };
75AB5E630E7452D2AB868AEC /* TyphoonBlockComponentFactoryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5BF1A64523976ACB3EC9 /* TyphoonBlockComponentFactoryTests.m */; };
75AB5EB7083BAC9194CBB82B /* TyphoonBlockComponentFactory_OverridingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB5106A7430A8EF5A6A292 /* TyphoonBlockComponentFactory_OverridingTests.m */; };
75AB5EF221B71E717BB14196 /* TyphoonInitializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 75AB57037E106B97B8F22B79 /* TyphoonInitializerTests.m */; };
Expand Down Expand Up @@ -507,6 +510,7 @@
65FE013B185F787C00C0785C /* PersonFactory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PersonFactory.h; sourceTree = "<group>"; };
65FE013C185F7F3000C0785C /* Person.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Person.h; sourceTree = "<group>"; };
65FE013D185F7F3000C0785C /* Person.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Person.m; sourceTree = "<group>"; };
75AB5057948C5EF8D86D932B /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m; sourceTree = "<group>"; };
75AB5106A7430A8EF5A6A292 /* TyphoonBlockComponentFactory_OverridingTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonBlockComponentFactory_OverridingTests.m; sourceTree = "<group>"; };
75AB51A490B3C70B90B70509 /* SomeResource */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = SomeResource; sourceTree = "<group>"; };
75AB5286A84793B6B45E3A08 /* TyphoonBlockComponentFactory_InvalidCollaboratingAssembliesTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonBlockComponentFactory_InvalidCollaboratingAssembliesTests.m; sourceTree = "<group>"; };
Expand All @@ -519,6 +523,7 @@
75AB56FB630B6976B7B63351 /* TyphoonFakeLogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TyphoonFakeLogger.h; sourceTree = "<group>"; };
75AB57037E106B97B8F22B79 /* TyphoonInitializerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonInitializerTests.m; sourceTree = "<group>"; };
75AB57167D2468CBE686FFB4 /* ClassWithConstructor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ClassWithConstructor.m; sourceTree = "<group>"; };
75AB573FE2A217C3011C1634 /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.h; sourceTree = "<group>"; };
75AB58BCF87EC527F127BCA0 /* TyphoonXMLBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonXMLBuilder.m; sourceTree = "<group>"; };
75AB5A95775B4E5C8356BB0C /* TyphoonFakeLogger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonFakeLogger.m; sourceTree = "<group>"; };
75AB5AABD65CAC43879BC428 /* ErrandQuest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ErrandQuest.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -755,6 +760,8 @@
75AB5C1E5B2E4A7032CDF9EA /* ExtendedSimpleAssembly.h */,
75AB5D85C9AE1DF0947C025C /* SimpleAssembly.m */,
75AB5B71181AC532BC4B7D7F /* SimpleAssembly.h */,
75AB5057948C5EF8D86D932B /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m */,
75AB573FE2A217C3011C1634 /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.h */,
);
path = Block;
sourceTree = "<group>";
Expand Down Expand Up @@ -1455,6 +1462,7 @@
75AB565A5EADD1EDC7525EBB /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests.m in Sources */,
75AB5A73FC1FBAE1BAAA06B4 /* ExtendedSimpleAssembly.m in Sources */,
75AB51CC7927741FB7007B1A /* SimpleAssembly.m in Sources */,
75AB5E3BBB07021C773A20C5 /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1549,6 +1557,7 @@
75AB55507ACC8E5251251992 /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests.m in Sources */,
75AB5525707914FF0D31ECF5 /* ExtendedSimpleAssembly.m in Sources */,
75AB528F93BA1378F46291EC /* SimpleAssembly.m in Sources */,
75AB51C098497FB1A9B4C75E /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1646,6 +1655,7 @@
75AB5F2A9609DF5EBF2CBE87 /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests.m in Sources */,
75AB59BE6D0E4192ADCAAD19 /* ExtendedSimpleAssembly.m in Sources */,
75AB51D4CBB1B5127FA29B05 /* SimpleAssembly.m in Sources */,
75AB5702F372BD7FCF58FB19 /* TyphoonCollaboratingAssemblyPropertyEnumeratorTests_AssemblyWithProperty.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
13 changes: 13 additions & 0 deletions Tests/TypeConversion/TyphoonTypeDescriptorTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ @interface TyphoonTypeDescriptorTests : SenTestCase
@property BOOL aBoolProperty;
@property NSURL* anNSURLProperty;
@property id<Quest> aQuestProperty;
@property NSObject <Quest>* anObjectQuestProperty;

@property(nonatomic, readonly) char charProperty;
@property(nonatomic, readonly) int intProperty;
Expand Down Expand Up @@ -74,6 +75,18 @@ - (void)test_type_description_protocol
assertThat(description, equalTo(@"Type descriptor: id<Quest>"));
}

- (void)test_type_description_class_and_protocol
{
TyphoonTypeDescriptor* descriptor = [self typeForPropertyWithName:@"anObjectQuestProperty"];
assertThatBool([descriptor isPrimitive], equalToBool(NO));
assertThat([descriptor protocol], equalTo(@protocol(Quest)));
assertThat([descriptor typeBeingDescribed], equalTo([NSObject class]));

NSString* description = [descriptor description];
assertThat(description, equalTo(@"Type descriptor: NSObject<Quest>"));
}


- (void)test_typeForPropertyWithName_char
{
TyphoonTypeDescriptor* descriptor = [self typeForPropertyWithName:@"charProperty"];
Expand Down

0 comments on commit 58e5852

Please sign in to comment.