Skip to content

Commit

Permalink
Merge branch 'TheOneSock-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-xu committed Oct 5, 2017
2 parents 118635a + 13a5816 commit dd894f8
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/Ninject.Extensions.Factory.Test/FactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,42 @@ public void GenericFactoryMethodsAreSupported()
handlers.Single().Should().BeOfType<IntMessageHandler>();
}

[Fact]
public void DecoratorShouldBeInjectedWithAppropriateBinding()
{
this.kernel.Bind<IWeapon>().To<DecoratedWeapon>();
this.kernel.Bind<IWeapon>().To<Sword>().WhenInjectedExactlyInto<DecoratedWeapon>();
this.kernel.Bind<IWeaponFactory>().ToFactory();

var instance = this.kernel.Get<IWeaponFactory>().CreateWeapon();
var decoratedWeapon = instance as DecoratedWeapon;

instance.Should().BeOfType<DecoratedWeapon>();
decoratedWeapon.WeaponToBeDecorated.Should().BeOfType<Sword>();

}

[Fact]
public void DecoratorShouldBeInjectedWithAppropriateBindingWithArguments()
{
const string Name = "Excalibur";
const int Width = 34;
const int Length = 123;

this.kernel.Bind<ICustomizableWeapon>().To<DecoratedCustomizableWeapon>();
this.kernel.Bind<ICustomizableWeapon>().To<CustomizableSword>().WhenInjectedExactlyInto<DecoratedCustomizableWeapon>();
this.kernel.Bind<IWeaponFactory>().ToFactory();

var weapon = this.kernel.Get<IWeaponFactory>().CreateCustomizableWeapon(Length, Name, Width);
var decoratedWeapon = weapon as DecoratedCustomizableWeapon;

weapon.Should().BeOfType<DecoratedCustomizableWeapon>();
decoratedWeapon.WeaponToBeDecorated.Should().BeOfType<CustomizableSword>();
weapon.Name.Should().Be(Name);
weapon.Length.Should().Be(Length);
weapon.Width.Should().Be(Width);
}

private class CustomInstanceProvider : StandardInstanceProvider
{
protected override string GetName(System.Reflection.MethodInfo methodInfo, object[] arguments)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//-------------------------------------------------------------------------------
// <copyright file="Sword.cs" company="Ninject Project Contributors">
// Copyright (c) 2009-2011 Ninject Project Contributors
// Authors: Remo Gloor ([email protected])
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// you may not use this file except in compliance with one of the Licenses.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// or
// http://www.microsoft.com/opensource/licenses.mspx
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-------------------------------------------------------------------------------

namespace Ninject.Extensions.Factory.Fakes
{
public class DecoratedCustomizableWeapon : ICustomizableWeapon
{
public ICustomizableWeapon WeaponToBeDecorated { get; private set; }

public DecoratedCustomizableWeapon(ICustomizableWeapon weaponToBeDecorated)
{
WeaponToBeDecorated = weaponToBeDecorated;
}

public string Name
{
get { return WeaponToBeDecorated.Name; }
}

public int Width
{
get { return WeaponToBeDecorated.Width; }
}

public int Length
{
get { return WeaponToBeDecorated.Length; }
}
}
}
38 changes: 38 additions & 0 deletions src/Ninject.Extensions.Factory.Test/Fakes/DecoratedWeapon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//-------------------------------------------------------------------------------
// <copyright file="Sword.cs" company="Ninject Project Contributors">
// Copyright (c) 2009-2011 Ninject Project Contributors
// Authors: Remo Gloor ([email protected])
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// you may not use this file except in compliance with one of the Licenses.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// or
// http://www.microsoft.com/opensource/licenses.mspx
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-------------------------------------------------------------------------------

namespace Ninject.Extensions.Factory.Fakes
{
public class DecoratedWeapon : IWeapon
{
public IWeapon WeaponToBeDecorated { get; private set; }

public DecoratedWeapon(IWeapon weaponToBeDecorated)
{
WeaponToBeDecorated = weaponToBeDecorated;
}

public string Name
{
get { return "decorated " + WeaponToBeDecorated.Name; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected virtual IConstructorArgument[] GetConstructorArguments(MethodInfo meth
var constructorArguments = new ConstructorArgument[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
{
constructorArguments[i] = new ConstructorArgument(parameters[i].Name, arguments[i]);
constructorArguments[i] = new ConstructorArgument(parameters[i].Name, arguments[i], true);
}

return constructorArguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.1.1" />
<PackageReference Include="Ninject" Version="3.3.0" />
<PackageReference Include="Ninject" Version="3.3.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit dd894f8

Please sign in to comment.