Skip to content

Commit

Permalink
TyphoonPatcher supports detaching.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper Blues committed May 4, 2014
1 parent 3a671e6 commit 95e1e36
Show file tree
Hide file tree
Showing 17 changed files with 3,097 additions and 2,963 deletions.
2 changes: 1 addition & 1 deletion .scripts/pod-update-checksum.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6fa80823ca08f6395fe1425c7a51cb2b34f0af68
ca6c5b5f89574fddb52d98775eb6d4ff4498b912
8 changes: 8 additions & 0 deletions A-Typhoon.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
BA798B291EDCA2E25701D626 /* TyphoonDefinition.h in Headers */ = {isa = PBXBuildFile; fileRef = BA798CA3A88193EC0242E6EC /* TyphoonDefinition.h */; };
BA798B302D77DE4B95258820 /* TyphoonDefinition+Infrastructure.h in Headers */ = {isa = PBXBuildFile; fileRef = BA798922DB905F8FE1C0610A /* TyphoonDefinition+Infrastructure.h */; };
BA798B3A9A4135A5FD9832CB /* TyphoonPatchObjectFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = BA7984E24E15162B65A6B4EC /* TyphoonPatchObjectFactory.h */; };
BA798B509453788BAA918C3D /* TyphoonAbstractDetachableComponentFactoryPostProcessor.h in Headers */ = {isa = PBXBuildFile; fileRef = BA798C4ACC77BDD993B0BA03 /* TyphoonAbstractDetachableComponentFactoryPostProcessor.h */; };
BA798B5B102611BACA9E8D37 /* NSValue+TCFUnwrapValues.h in Headers */ = {isa = PBXBuildFile; fileRef = BA79883F3BB7622CC4D6B969 /* NSValue+TCFUnwrapValues.h */; };
BA798B5E6F4F26B9F05F7D9B /* TyphoonAssistedFactoryMethodInitializerClosure.h in Headers */ = {isa = PBXBuildFile; fileRef = BA79896BC01C931A1CDD16AB /* TyphoonAssistedFactoryMethodInitializerClosure.h */; };
BA798B81742C506DFD66F0F1 /* TyphoonAssistedFactoryMethodInitializer.m in Sources */ = {isa = PBXBuildFile; fileRef = BA798D28664C02D91B12FE25 /* TyphoonAssistedFactoryMethodInitializer.m */; };
Expand Down Expand Up @@ -273,6 +274,7 @@
BA798FD2FBC0CA91B17AEEFC /* TyphoonDefinition.m in Sources */ = {isa = PBXBuildFile; fileRef = BA798F905F4B216CEA7A4D8A /* TyphoonDefinition.m */; };
BA798FDF82B13B80AE51CC2F /* TyphoonAssistedFactoryCreatorImplicit.h in Headers */ = {isa = PBXBuildFile; fileRef = BA7980C1984DA759C53E55CC /* TyphoonAssistedFactoryCreatorImplicit.h */; };
BA798FE29893B57DF87E1841 /* NSObject+TyphoonIntrospectionUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = BA798263556BF9A8D3735540 /* NSObject+TyphoonIntrospectionUtils.m */; };
BA798FEFCEC4E67737485A73 /* TyphoonAbstractDetachableComponentFactoryPostProcessor.m in Sources */ = {isa = PBXBuildFile; fileRef = BA7987278D47B1433DBB673F /* TyphoonAbstractDetachableComponentFactoryPostProcessor.m */; };
CAE136B5FDDB099823DEFDF6 /* TyphoonComponentPostProcessor.h in Headers */ = {isa = PBXBuildFile; fileRef = CAEF00E64F8B33A34E7E7E0D /* TyphoonComponentPostProcessor.h */; };
CAE1B659E914D978F5BBD8B3 /* TyphoonOrdered.h in Headers */ = {isa = PBXBuildFile; fileRef = CAE505328C031EAF918E3BC6 /* TyphoonOrdered.h */; };
CAE3A5E5099F311D72D7049F /* TyphoonComponentPostProcessor.h in Headers */ = {isa = PBXBuildFile; fileRef = CAEF00E64F8B33A34E7E7E0D /* TyphoonComponentPostProcessor.h */; };
Expand Down Expand Up @@ -463,6 +465,7 @@
BA79860517B1CB594E12178B /* TyphoonPrimitiveTypeConverter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonPrimitiveTypeConverter.m; sourceTree = "<group>"; };
BA7986DF1FADC2BBC51A7F09 /* TyphoonJRSwizzle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TyphoonJRSwizzle.h; sourceTree = "<group>"; };
BA7986F5550A87B611F2F376 /* TyphoonPropertyPlaceholderConfigurer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonPropertyPlaceholderConfigurer.m; sourceTree = "<group>"; };
BA7987278D47B1433DBB673F /* TyphoonAbstractDetachableComponentFactoryPostProcessor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAbstractDetachableComponentFactoryPostProcessor.m; sourceTree = "<group>"; };
BA79873EF7DD629707AF0CBF /* TyphoonTestUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonTestUtils.m; sourceTree = "<group>"; };
BA798748BF3157857183FB6E /* TyphoonAssistedFactoryParameterInjectedWithArgumentIndex.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryParameterInjectedWithArgumentIndex.m; sourceTree = "<group>"; };
BA7988121D739939C9D5814D /* TyphoonAssistedFactoryBase+TyphoonFactoryMethodClosure.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "TyphoonAssistedFactoryBase+TyphoonFactoryMethodClosure.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -495,6 +498,7 @@
BA798C0D7B619637FD78FB10 /* NSInvocation+TCFInstanceBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSInvocation+TCFInstanceBuilder.m"; sourceTree = "<group>"; };
BA798C288D20A309D557A449 /* TyphoonFactoryProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TyphoonFactoryProvider.h; sourceTree = "<group>"; };
BA798C418D030C1D419B6BFC /* TyphoonAssistedFactoryDefinition.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryDefinition.m; sourceTree = "<group>"; };
BA798C4ACC77BDD993B0BA03 /* TyphoonAbstractDetachableComponentFactoryPostProcessor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TyphoonAbstractDetachableComponentFactoryPostProcessor.h; sourceTree = "<group>"; };
BA798CA3A88193EC0242E6EC /* TyphoonDefinition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = TyphoonDefinition.h; sourceTree = "<group>"; };
BA798CB5CAC2E65EC14F9BB7 /* TyphoonAssistedFactoryBase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TyphoonAssistedFactoryBase.m; sourceTree = "<group>"; };
BA798CB709B2750980CFFC62 /* TyphoonComponentFactory+InstanceBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "TyphoonComponentFactory+InstanceBuilder.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -835,6 +839,8 @@
BA7988BE595AFCF368188255 /* Resource */,
CAEF00E64F8B33A34E7E7E0D /* TyphoonComponentPostProcessor.h */,
CAE505328C031EAF918E3BC6 /* TyphoonOrdered.h */,
BA7987278D47B1433DBB673F /* TyphoonAbstractDetachableComponentFactoryPostProcessor.m */,
BA798C4ACC77BDD993B0BA03 /* TyphoonAbstractDetachableComponentFactoryPostProcessor.h */,
);
path = Configuration;
sourceTree = "<group>";
Expand Down Expand Up @@ -1351,6 +1357,7 @@
BA79878C68DC6BF827B2951B /* NSInvocation+TCFInstanceBuilder.h in Headers */,
BA7987FF4509A08545B354C0 /* NSValue+TCFUnwrapValues.h in Headers */,
FAC0B8FA18CF80E0000F0AD0 /* TyphoonInjectionByObjectInstance.h in Headers */,
BA798B509453788BAA918C3D /* TyphoonAbstractDetachableComponentFactoryPostProcessor.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1595,6 +1602,7 @@
BA7984FC182683B6DA08DB6F /* TyphoonParentReferenceHydratingPostProcessor.m in Sources */,
BA7982950ACC08F1F91FBA05 /* NSInvocation+TCFInstanceBuilder.m in Sources */,
BA79856A1C1DBB588E701FF4 /* NSValue+TCFUnwrapValues.m in Sources */,
BA798FEFCEC4E67737485A73 /* TyphoonAbstractDetachableComponentFactoryPostProcessor.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2014 ibipit
// All Rights Reserved.
//
// NOTICE: This software is the proprietary information of ibipit
// Use is subject to license terms.
//
////////////////////////////////////////////////////////////////////////////////



#import <Foundation/Foundation.h>
#import "TyphoonComponentFactoryPostProcessor.h"


@interface TyphoonAbstractDetachableComponentFactoryPostProcessor : NSObject <TyphoonComponentFactoryPostProcessor>
{
TyphoonComponentFactory* _factory;
NSMutableArray *_rollbackDefinitions;
}

@property(nonatomic, strong, readonly) NSArray *rollbackDefinitions;

/**
Restores a component factory back to its initial state after post processing.
*/
- (void)rollback;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2014 ibipit
// All Rights Reserved.
//
// NOTICE: This software is the proprietary information of ibipit
// Use is subject to license terms.
//
////////////////////////////////////////////////////////////////////////////////



#import "TyphoonAbstractDetachableComponentFactoryPostProcessor.h"
#import "TyphoonComponentFactory.h"
#import "TyphoonDefinition.h"

@implementation TyphoonComponentFactory (DetachableComponentFactoryPostProcessor)

- (void)setRegistry:(NSMutableArray *)registry
{
_registry = registry;
}

@end


@implementation TyphoonAbstractDetachableComponentFactoryPostProcessor

- (void)postProcessComponentFactory:(TyphoonComponentFactory *)factory
{
_factory = factory;
[self cacheDefinitionsIn:_factory];
}

- (void)rollback
{
NSMutableArray *postProcessors = (NSMutableArray *) _factory.factoryPostProcessors;
if (!postProcessors.lastObject == self) {
[NSException raise:@"Only the last TyphoonAbstractDetachableComponentFactoryPostProcessor can be rolled-back"
format:NSInternalInconsistencyException];
}
[postProcessors removeLastObject];
_factory.registry = _rollbackDefinitions;
[_factory unload];
}

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

- (void)cacheDefinitionsIn:(TyphoonComponentFactory *)factory
{
NSMutableArray *definitions = [[NSMutableArray alloc] init];
for (TyphoonDefinition *definition in factory.registry) {
[definitions addObject:[definition copy]];
}
_rollbackDefinitions = [definitions mutableCopy];
}


@end
3 changes: 2 additions & 1 deletion Source/Configuration/TyphoonComponentFactoryPostProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*
Allows for custom modification of a component factory's definitions.
Component factories can auto-detect TyphoonComponentFactoryPostProcessor components in their definitions and apply them before any other components get created.
Component factories can auto-detect TyphoonComponentFactoryPostProcessor components in their definitions and apply them before any other
components get created.
@see TyphoonPropertyPlaceholderConfigurer for an example implementation.
@see TyphoonComponentPostProcessor which modifies instances after they've been built, rather than the definitions
Expand Down
5 changes: 4 additions & 1 deletion Source/Test/Patcher/TyphoonPatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#import <Foundation/Foundation.h>
#import "TyphoonComponentFactoryPostProcessor.h"
#import "TyphoonAbstractDetachableComponentFactoryPostProcessor.h"

@class TyphoonDefinition;

Expand All @@ -25,7 +26,7 @@ typedef id (^TyphoonPatchObjectCreationBlock)();
* difficult put the system in the required state. Patcher allows taking a fully assembled system, changing just the part required for the
* given test scenario.
*/
@interface TyphoonPatcher : NSObject <TyphoonComponentFactoryPostProcessor>
@interface TyphoonPatcher : TyphoonAbstractDetachableComponentFactoryPostProcessor
{
NSMutableDictionary *_patches;
}
Expand All @@ -34,4 +35,6 @@ typedef id (^TyphoonPatchObjectCreationBlock)();

- (void)patchDefinition:(TyphoonDefinition *)definition withObject:(TyphoonPatchObjectCreationBlock)objectCreationBlock;

- (void)detach;

@end
7 changes: 6 additions & 1 deletion Source/Test/Patcher/TyphoonPatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#import "TyphoonDefinition.h"
#import "TyphoonPatcher.h"
#import "TyphoonPatchObjectFactory.h"
#import "TyphoonMethod.h"
#import "TyphoonComponentFactory.h"
#import "TyphoonDefinition+Infrastructure.h"

Expand Down Expand Up @@ -43,11 +42,17 @@ - (void)patchDefinition:(TyphoonDefinition *)definition withObject:(TyphoonPatch
[self patchDefinitionWithKey:definition.key withObject:objectCreationBlock];
}

- (void)detach
{
[self rollback];
}

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

- (void)postProcessComponentFactory:(TyphoonComponentFactory *)factory
{
[super postProcessComponentFactory:factory];
for (TyphoonDefinition *definition in [factory registry]) {
id patchObject = [_patches objectForKey:definition.key];
if (patchObject) {
Expand Down
1 change: 1 addition & 0 deletions Tests/Factory/Block/TestAssemblies/MiddleAgesAssembly.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ - (id)knight
{
return [TyphoonDefinition withClass:[Knight class] configuration:^(TyphoonDefinition *definition) {
[definition injectProperty:@selector(quest) with:[self defaultQuest]];
[definition injectProperty:@selector(favoriteDamsels) with:@[@"foo", @"bar"]];
[definition injectProperty:@selector(damselsRescued) with:[[self cavalryMan] property:@selector(damselsRescued)]];
[definition setScope:TyphoonScopeObjectGraph];
}];
Expand Down
6 changes: 3 additions & 3 deletions Tests/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ PODS:
- OCHamcrest (1.9)
- OCMockito (0.23):
- OCHamcrest
- Typhoon (2.0.1):
- Typhoon (2.0.2):
- Typhoon/no-arc
- Typhoon/no-arc (2.0.1)
- Typhoon/no-arc (2.0.2)

DEPENDENCIES:
- OCHamcrest (~> 1.9)
Expand All @@ -18,6 +18,6 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
OCHamcrest: 4696b90a600307734cc554eb043cc98663035d04
OCMockito: 85f910881a3731e17e633fffc6883e8532acf8eb
Typhoon: 366c0daa37c4ece711679a8930000a0957644f82
Typhoon: 78c8e4fe24c4a5457b148d1204340b294a28eb4e

COCOAPODS: 0.32.1
2 changes: 1 addition & 1 deletion Tests/Pods/Local Podspecs/Typhoon.podspec

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Tests/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Tests/Pods/Pods-OS X Tests (Cocoapods)-environment.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Tests/Pods/Pods-iOS Tests (Cocoapods)-environment.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 95e1e36

Please sign in to comment.