Skip to content

Commit

Permalink
Merge pull request #53 from DoclerLabs/develop
Browse files Browse the repository at this point in the history
prepare 0.20.0
  • Loading branch information
aliokan authored May 3, 2017
2 parents ef84ae0 + ef7f566 commit b665a42
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/hex/di/Injector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ class Injector
this.map( clazz, name ).toSingleton( type );
}

public function getInstance<T>( type : Class<T>, name : String = '' ) : T
public function getInstance<T>( type : Class<T>, name : String = '', targetType : Class<Dynamic> = null ) : T
{
var mappingID = this._getMappingID( type, name );
var mapping = this._mapping[ mappingID ];

if ( mapping != null )
{
return mapping.getResult( null );
return mapping.getResult( targetType );
}
else if ( this._parentInjector != null )
{
return this._parentInjector.getInstance( type, name );
return this._parentInjector.getInstance( type, name, targetType );
}
else
{
Expand All @@ -104,18 +104,18 @@ class Injector
}
}

public function getInstanceWithClassName<T>( className : String, name : String = '' ) : T
public function getInstanceWithClassName<T>( className : String, name : String = '', targetType : Class<Dynamic> = null ) : T
{
var mappingID = className + '|' + name;
var mapping = cast this._mapping[ mappingID ];

if ( mapping != null )
{
return mapping.getResult( Type.getClass( className.split( '<' )[ 0 ] ) );
return mapping.getResult( targetType );
}
else if ( this._parentInjector != null )
{
return this._parentInjector.getInstanceWithClassName( className, name );
return this._parentInjector.getInstanceWithClassName( className, name, targetType );
}
else
{
Expand Down
44 changes: 44 additions & 0 deletions test/hex/di/InjectorTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -888,4 +888,48 @@ class InjectorTest implements IInjectorListener
Assert.equals(NamedClassInjecteeWithClassName, provider.target, "Target should be proper class");
Assert.equals("hex.di.mock.injectees.NamedClassInjecteeWithClassName", provider.className, "Target should be proper class");
}

@Test( "Test dependency provider with class name" )
public function testDependencyProviderWithClassName() : Void
{
var instance = new Clazz();
var provider = new MockDependencyProvider<Clazz>(instance);
injector.map( Clazz ).toProvider(provider);

var returnVal = injector.getInstanceWithClassName("hex.di.mock.types.Clazz");

Assert.equals(instance, returnVal, "Returned value must come from dependency provider");
Assert.equals(injector, provider.injector, "Injector provided to the dependency provider must be the correct one");
Assert.isNull(provider.target, "Target is unknown so it's supposed to be null");
}

@Test( "Test getInstance target type is passed to dependency provider" )
public function testGetInstanceTargetTypeIsPassedToDependencyProvider() : Void
{
var instance = new Clazz();
var provider = new MockDependencyProvider<Clazz>(instance);
injector.map( Clazz ).toProvider(provider);

var returnVal = injector.getInstance(Clazz, null, NamedClassInjecteeWithClassName);

Assert.equals(instance, returnVal, "Returned value must come from dependency provider");
Assert.equals(injector, provider.injector, "Injector provided to the dependency provider must be the correct one");
Assert.equals("hex.di.mock.injectees.NamedClassInjecteeWithClassName", provider.className, "Target should be proper class");
}

@Test( "Test getInstanceWithClassName target type is passed to dependency provider" )
public function testGetInstanceWithClassNameTargetTypeIsPassedToDependencyProvider() : Void
{
var instance = new Clazz();
var provider = new MockDependencyProvider<Clazz>(instance);
injector.map( Clazz ).toProvider(provider);

var returnVal = injector.getInstanceWithClassName("hex.di.mock.types.Clazz", null, NamedClassInjecteeWithClassName);

Assert.equals(instance, returnVal, "Returned value must come from dependency provider");
Assert.equals(injector, provider.injector, "Injector provided to the dependency provider must be the correct one");
Assert.equals("hex.di.mock.injectees.NamedClassInjecteeWithClassName", provider.className, "Target should be proper class");
}


}
3 changes: 0 additions & 3 deletions test/hex/di/mock/provider/MockDependencyProvider.hx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ class MockDependencyProvider<T> implements IDependencyProvider<T>

}


/* INTERFACE hex.di.provider.IDependencyProvider.IDependencyProvider<T> */

public function getResult(injector:IDependencyInjector, target:Class<Dynamic>):T
{
this.injector = injector;
Expand Down
4 changes: 2 additions & 2 deletions test/hex/di/util/InjectionUtilTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ private class MockDependencyInjector implements IDependencyInjector

}

public function getInstance<T>( type : Class<T>, name : String = '' ) : T
public function getInstance<T>( type : Class<T>, name : String = '', targetType : Class<Dynamic> = null ) : T
{
return null;
}

public function getInstanceWithClassName<T>( className : String, name : String = '' ) : T
public function getInstanceWithClassName<T>( className : String, name : String = '', targetType : Class<Dynamic> = null ) : T
{
this.instanceWithClassName = { className: className, name: name };
return null;
Expand Down

0 comments on commit b665a42

Please sign in to comment.