Permalink
Browse files

Added test for config when dsl

  • Loading branch information...
1 parent c6572c4 commit b6a49d3dc4324af71bd4df47822405cfe3704821 @casualjim casualjim committed Mar 14, 2009
@@ -13,14 +13,14 @@
namespace Ninject.Dynamic
{
- public class RubyKernel : StandardKernel
+ public class DlrKernel : StandardKernel
{
- public RubyKernel(params IModule[] modules) : base(modules)
+ public DlrKernel(params IModule[] modules) : base(modules)
{
}
- public RubyKernel(INinjectSettings settings, params IModule[] modules) : base(settings, modules)
+ public DlrKernel(INinjectSettings settings, params IModule[] modules) : base(settings, modules)
{
}
@@ -77,7 +77,7 @@
<Compile Include="RubyEngine.cs" />
<Compile Include="Modules\RubyModule.cs" />
<Compile Include="Modules\RubyModuleLoaderPlugin.cs" />
- <Compile Include="RubyKernel.cs" />
+ <Compile Include="DlrKernel.cs" />
<Compile Include="Workarounds.cs" />
</ItemGroup>
<ItemGroup>
@@ -339,10 +339,11 @@ def bind(service_type, config={}, &b)
end
def method_missing(message, *args, &b)
- @@posibilities ||= [:meta, :name, :with, :on_activation, :on_deactivation, :activated, :deactivated, :when]
+ @@posibilities ||= [:meta, :name, :with, :on_activation, :on_deactivation, :activated, :deactivated, :when, :condition]
if @@posibilities.include?(message.to_sym)
- @config[message.to_sym] = args.first if args.size > 0
- @config[message.to_sym] = b unless b.nil?
+ key = (message.to_sym == :condition) ? :when : message.to_sym
+ @config[key] = args.first if args.size > 0
+ @config[key] = lambda { |request| b.call(request) } unless b.nil?
else
super #preserve normal behavior
end
@@ -14,15 +14,15 @@
#endregion
-namespace Ninject.Tests.Integration.RubyKernelTests
+namespace Ninject.Tests.Integration.DlrKernelTests
{
- public class RubyKernelContext
+ public class DlrKernelContext
{
- protected readonly RubyKernel kernel;
+ protected readonly DlrKernel kernel;
- public RubyKernelContext()
+ public DlrKernelContext()
{
- kernel = new RubyKernel();
+ kernel = new DlrKernel();
}
protected void SetPath(string path)
@@ -35,7 +35,7 @@ protected void SetPath(string path)
}
}
- public class WhenConfiguredFromCSharp : RubyKernelContext
+ public class WhenConfiguredFromCSharp : DlrKernelContext
{
[Fact]
public void SingleInstanceIsReturnedWhenOneBindingIsRegistered()
@@ -76,7 +76,7 @@ public void DependenciesAreInjectedViaConstructor()
}
- public class WhenConfiguredFromRuby : RubyKernelContext
+ public class WhenConfiguredFromRuby : DlrKernelContext
{
[Fact]
public void SingleInstanceIsReturnedWhenOneBindingIsRegistered()
@@ -117,7 +117,7 @@ public void DependenciesAreInjectedViaConstructor()
}
}
- public class WhenBoundToSelf : RubyKernelContext
+ public class WhenBoundToSelf : DlrKernelContext
{
[Fact]
public void SingleInstanceIsReturnedWhenOneBindingIsRegistered()
@@ -148,7 +148,7 @@ public void DependenciesAreInjectedViaConstructor()
}
- public class WhenBoundToGenericServiceRegisteredViaOpenGenericType : RubyKernelContext
+ public class WhenBoundToGenericServiceRegisteredViaOpenGenericType : DlrKernelContext
{
[Fact]
public void GenericParametersAreInferred()
@@ -165,7 +165,7 @@ public void GenericParametersAreInferred()
}
}
- public class WhenBoundWithConstraints : RubyKernelContext
+ public class WhenBoundWithConstraints : DlrKernelContext
{
[Fact]
public void ReturnsServiceRegisteredViaBindingWithSpecifiedName()
@@ -192,7 +192,7 @@ public void ReturnsServiceRegisteredViaBindingThatMatchesPredicate()
}
}
- public class WhenBoundWithConstructorArguments : RubyKernelContext
+ public class WhenBoundWithConstructorArguments : DlrKernelContext
{
[Fact]
public void ReturnsServiceRegistered()
@@ -221,7 +221,7 @@ public void ReturnsServiceWhenRegisteredAsDSL()
}
}
- public class WhenBoundWithWhenArgument : RubyKernelContext
+ public class WhenBoundWithWhenArgument : DlrKernelContext
{
[Fact]
public void ResolvesTheCorrectTypeAccordingToCondition()
@@ -238,6 +238,22 @@ public void ResolvesTheCorrectTypeAccordingToCondition()
warrior.Weapon.ShouldBeInstanceOf<Shuriken>();
}
+
+ [Fact]
+ public void ResolvesTheCorrectTypeAccordingToConditionWithDsl()
+ {
+ SetPath("config_when_dsl.rb");
+ kernel.AutoLoadModulesRecursively("~");
+
+ var weapon = kernel.Get<IWeapon>();
+ var warrior = kernel.Get<IWarrior>();
+
+ weapon.ShouldNotBeNull();
+ weapon.ShouldBeInstanceOf<Sword>();
+ warrior.ShouldNotBeNull();
+ warrior.Weapon.ShouldBeInstanceOf<Shuriken>();
+
+ }
}
}
@@ -2,7 +2,11 @@
include Ninject::Tests::Fakes
to_configure_ninject do |ninject|
- ninject.bind IWeapon, :to => Knife, :with => { :constructor_argument => { :name => "Blunt knife".to_clr_string } }
- ninject.bind IWeapon, :to => Shuriken
+ ninject.bind IWeapon, :to => Shuriken do
+ condition do |request|
+ request.target.nil? ? false : request.target.member.reflected_type == Samurai.to_clr_type
+ end
+ end
+ ninject.bind IWeapon, :to => Sword
ninject.bind IWarrior, :to => Samurai
end

0 comments on commit b6a49d3

Please sign in to comment.