Skip to content

Commit

Permalink
tidy up and add failing test for TyphoonScopeObjectGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper Blues committed Jan 26, 2014
1 parent 27c16c9 commit a1007d2
Show file tree
Hide file tree
Showing 35 changed files with 3,174 additions and 2,946 deletions.
19 changes: 11 additions & 8 deletions Source/Definition/TyphoonDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
/**
* @ingroup Definition
* Describes the lifecycle of a Typhoon component.
*
* - TyphoonScopeDefault means that a new component is created for each time it is referenced in a collaborator, or retrieved from the
* factory.
*
* - TyphoonScopeObjectGraph (default) means that a new non-retained component is created when resolved from the factory, and any
* dependencies declared during resolution of the object graph will be shared.
* - TyphoonScopePrototype means that a new component is created for each time it is referenced in a collaborator, or retrieved
* from the factory.
* - TyphoonScopeSingleton creates a shared instance.
*
*/
typedef enum
{
TyphoonScopeDefault,
TyphoonScopeSingleton
TyphoonScopeObjectGraph = 1 << 0,
TyphoonScopePrototype = 1 << 1,
TyphoonScopeSingleton = 1 << 2,
} TyphoonScope;


Expand All @@ -47,6 +49,7 @@ typedef void(^TyphoonDefinitionBlock)(TyphoonDefinition* definition);
TyphoonInitializer* _initializer;
NSMutableSet* _injectedProperties;
NSString* _factoryReference;
TyphoonScope _scope;
}

@property(nonatomic, readonly) Class type;
Expand Down Expand Up @@ -77,7 +80,7 @@ typedef void(^TyphoonDefinitionBlock)(TyphoonDefinition* definition);

+ (TyphoonDefinition*)withClass:(Class)clazz properties:(TyphoonDefinitionBlock)properties;

+ (TyphoonDefinition*)withClass:(Class)clazz factory:(TyphoonDefinition *)definition selector:(SEL)selector;
+ (TyphoonDefinition*)withClass:(Class)clazz factory:(TyphoonDefinition*)definition selector:(SEL)selector;

/* ====================================================================================================================================== */
#pragma mark Injection
Expand All @@ -100,7 +103,7 @@ typedef void(^TyphoonDefinitionBlock)(TyphoonDefinition* definition);
/**
* Injects property with result of invocation valueForKeyPath with given keyPath on factoryDefinition.
*/
- (void)injectProperty:(SEL)selector withDefinition:(TyphoonDefinition*)factoryDefinition keyPath:(NSString *)keyPath;
- (void)injectProperty:(SEL)selector withDefinition:(TyphoonDefinition*)factoryDefinition keyPath:(NSString*)keyPath;

/**
* Injects property with the given object instance.
Expand Down
4 changes: 2 additions & 2 deletions Source/Factory/Block/TyphoonBlockComponentFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
*/
@interface TyphoonBlockComponentFactory : TyphoonComponentFactory

+ (instancetype)factoryWithAssembly:(TyphoonAssembly*)assembly;
+ (id)factoryWithAssembly:(TyphoonAssembly*)assembly;

+ (instancetype)factoryWithAssemblies:(NSArray*)assemblies;
+ (id)factoryWithAssemblies:(NSArray*)assemblies;

- (instancetype)initWithAssembly:(TyphoonAssembly*)assembly;

Expand Down
10 changes: 5 additions & 5 deletions Source/Factory/Block/TyphoonBlockComponentFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@
#import "TyphoonDefinition.h"
#import "OCLogTemplate.h"
#import "TyphoonAssembly+TyphoonAssemblyFriend.h"
#import "TyphoonInjectedParameter.h"
#import "TyphoonParameterInjectedByReference.h"



@implementation TyphoonBlockComponentFactory

/* ====================================================================================================================================== */
#pragma mark - Class Methods
+ (instancetype)factoryWithAssembly:(TyphoonAssembly*)assembly

+ (id)factoryWithAssembly:(TyphoonAssembly*)assembly
{
return [[self alloc] initWithAssemblies:@[assembly]];
}

+ (instancetype)factoryWithAssemblies:(NSArray*)assemblies
+ (id)factoryWithAssemblies:(NSArray*)assemblies
{
return [[self alloc] initWithAssemblies:assemblies];
}
Expand Down Expand Up @@ -87,6 +85,8 @@ - (void)registerAllDefinitions:(TyphoonAssembly*)assembly
}
}



/* ====================================================================================================================================== */
#pragma mark - Overridden Methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,14 @@ - (TyphoonGenericStack*)stackForKey:(NSString*)key
return stack;
}


- (NSString*)description
{
NSMutableString* description = [NSMutableString stringWithFormat:@"<%@: ", NSStringFromClass([self class])];
[description appendFormat:@"_registry=%@", _registry];
[description appendString:@">"];
return description;
}


@end
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ - (TyphoonScope)scopeForStringValue:(NSString*)scope

// Here, we don't follow the Spring's implementation :
// the "default" scope is the prototype.
TyphoonScope result = TyphoonScopeDefault;
TyphoonScope result = TyphoonScopeObjectGraph;
if ([scope isEqualToString:@"singleton"])
{
result = TyphoonScopeSingleton;
Expand Down
3 changes: 3 additions & 0 deletions Tests/Definition/TyphoonDefinitionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "AutoWiringSubClassedKnight.h"
#import "TyphoonDefinition+InstanceBuilder.h"
#import "TyphoonPropertyInjectedWithStringRepresentation.h"
#import "TyphoonDefinition.h"


@interface TyphoonDefinitionTests : SenTestCase
Expand Down Expand Up @@ -166,5 +167,7 @@ - (void)test_autoWired_properties
assertThat(autoWired, hasItem(@"foobar"));
}



@end

File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ - (id)knight
{
[definition injectProperty:@selector(quest) withDefinition:[self defaultQuest]];
[definition injectProperty:@selector(damselsRescued) withValueAsText:@"12"];
[definition setScope:TyphoonScopeDefault];
[definition setScope:TyphoonScopeObjectGraph];
}];
}

Expand Down
25 changes: 25 additions & 0 deletions Tests/Factory/Block/TestAssemblies/ObjectGraphAssembly.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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 <Foundation/Foundation.h>
#import "TyphoonAssembly.h"


@interface ObjectGraphAssembly : TyphoonAssembly

- (id)objectGraphKnight;

- (id)objectGraphQuest;

- (id)prototypeKnight;

@end
62 changes: 62 additions & 0 deletions Tests/Factory/Block/TestAssemblies/ObjectGraphAssembly.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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 <Typhoon/TyphoonDefinition.h>
#import "ObjectGraphAssembly.h"
#import "Knight.h"
#import "Fort.h"
#import "CampaignQuest.h"


@implementation ObjectGraphAssembly

- (id)objectGraphKnight
{
return [TyphoonDefinition withClass:[Knight class] properties:^(TyphoonDefinition* definition)
{
[definition injectProperty:@selector(homeFort) withDefinition:[self objectGraphFort]];
[definition injectProperty:@selector(quest) withDefinition:[self objectGraphQuest]];
}];
}

- (id)objectGraphFort
{
return [TyphoonDefinition withClass:[Fort class]];
}

- (id)objectGraphQuest
{
return [TyphoonDefinition withClass:[CampaignQuest class] properties:^(TyphoonDefinition* definition)
{
[definition injectProperty:@selector(fort) withDefinition:[self objectGraphFort]];
}];
}

/* ====================================================================================================================================== */

- (id)prototypeKnight
{
return nil;
}

- (id)prototypeCastle
{
return nil;
}

- (id)prototypeQuest
{
return nil;
}

@end
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions Tests/Factory/TyphoonComponentFactoryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ - (void)test_componentForKey_returns_with_property_dependencies_resolved_by_type
[_componentFactory register:[TyphoonDefinition withClass:[Knight class] properties:^(TyphoonDefinition* definition)
{
[definition injectProperty:@selector(quest)];
[definition setScope:TyphoonScopeDefault];
[definition setScope:TyphoonScopeObjectGraph];
[definition setKey:@"knight"];
}]];

Expand Down Expand Up @@ -350,7 +350,7 @@ - (void)test_load_singleton
}]];
[_componentFactory register:[TyphoonDefinition withClass:[CavalryMan class] properties:^(TyphoonDefinition* definition)
{
[definition setScope:TyphoonScopeDefault];
[definition setScope:TyphoonScopeObjectGraph];
[definition setLazy:YES];
}]];
[_componentFactory load];
Expand Down
48 changes: 48 additions & 0 deletions Tests/Factory/TyphoonScopeTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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 <SenTestingKit/SenTestingKit.h>
#import "ObjectGraphAssembly.h"
#import "TyphoonBlockComponentFactory.h"
#import "Knight.h"
#import "CampaignQuest.h"
#import "Fort.h"
#import "OCLogTemplate.h"

@interface TyphoonScopeTests : SenTestCase
{
ObjectGraphAssembly* _assembly;
}

@end

@implementation TyphoonScopeTests

- (void)setUp
{
TyphoonBlockComponentFactory* factory = [TyphoonBlockComponentFactory factoryWithAssembly:[ObjectGraphAssembly assembly]];
_assembly = (ObjectGraphAssembly*)factory;
}


//- (void)test_object_graph_assembly
//{
// Knight* knight = [_assembly objectGraphKnight];
// CampaignQuest* quest = [_assembly objectGraphQuest];
//
// LogDebug(@"Knight's fort: %@", knight.homeFort);
// LogDebug(@"Quest's fort: %@", quest.fort);
//
// assertThatBool(knight.homeFort == quest.fort, equalToBool(YES));
//}

@end
2 changes: 2 additions & 0 deletions Tests/Model/CampaignQuest.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
#import <Foundation/Foundation.h>
#import "Quest.h"

@class Fort;

@interface CampaignQuest : NSObject <Quest>

@property(nonatomic, strong) NSURL* imageUrl;
@property(nonatomic, strong) Fort* fort;

- (id)initWithImageUrl:(NSURL*)imageUrl;

Expand Down
17 changes: 17 additions & 0 deletions Tests/Model/Fort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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 <Foundation/Foundation.h>


@interface Fort : NSObject
@end
20 changes: 20 additions & 0 deletions Tests/Model/Fort.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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 "Fort.h"


@implementation Fort
{

}
@end
2 changes: 2 additions & 0 deletions Tests/Model/Knight.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import <Foundation/Foundation.h>

@class CampaignQuest;
@class Fort;

#import "Quest.h"

Expand All @@ -27,6 +28,7 @@
@property(nonatomic, strong, readonly) id <Quest> readOnlyQuest;
@property(nonatomic, strong) NSArray* favoriteDamsels;
@property(nonatomic, strong) NSSet* friends;
@property(nonatomic, strong) Fort* homeFort;


- (id)initWithQuest:(id <Quest>)quest;
Expand Down
Loading

0 comments on commit a1007d2

Please sign in to comment.