Skip to content

Commit

Permalink
fixed issue ninject#189
Browse files Browse the repository at this point in the history
  • Loading branch information
sglienke committed Sep 16, 2015
1 parent 898be06 commit 0997c87
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/Ninject.Test/Integration/ConditionalBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,39 @@ public void WhenNoAncestorMatchesAppliesToGrandParentAndParent()
barack.Warrior.Weapon.Should().BeOfType<Dagger>();
}

[Fact]
public void WhenMemberHasDoesNotConsiderAttributeOnTarget()
{
kernel.Bind<Knight>().ToSelf();
kernel.Bind<IWeapon>().To<Sword>();
kernel.Bind<IWeapon>().To<ShortSword>().WhenMemberHas<WeakAttribute>();

var knight = kernel.Get<Knight>();
knight.Weapon.Should().BeOfType<Sword>();
}

[Fact]
public void WhenMemberHasDoesConsiderAttributeOnMember()
{
kernel.Bind<Knight>().ToSelf();
kernel.Bind<IWeapon>().To<Sword>().WhenMemberHas<StrongAttribute>();
kernel.Bind<IWeapon>().To<ShortSword>();

var knight = kernel.Get<Knight>();
knight.Weapon.Should().BeOfType<Sword>();
}

[Fact]
public void WhenTargetHasDoesConsiderAttributeOnTarget()
{
kernel.Bind<Knight>().ToSelf();
kernel.Bind<IWeapon>().To<Sword>();
kernel.Bind<IWeapon>().To<ShortSword>().WhenTargetHas<WeakAttribute>();

var knight = kernel.Get<Knight>();
knight.Weapon.Should().BeOfType<ShortSword>();
}

public interface IGenericService<T>
{
}
Expand Down Expand Up @@ -391,5 +424,16 @@ public AnotherGenericService(IWarrior warrior)

public IWarrior Warrior { get; protected set; }
}

public class Knight
{
public IWeapon Weapon { get; private set; }

[Strong]
public Knight([Weak] IWeapon weapon)
{
this.Weapon = weapon;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public IBindingInNamedWithOrOnSyntax<T> WhenMemberHas(Type attributeType)
throw new InvalidOperationException(ExceptionFormatter.InvalidAttributeTypeUsedInBindingCondition(this.serviceNames, "WhenMemberHas", attributeType));
}

this.BindingConfiguration.Condition = r => r.Target != null && r.Target.IsDefined(attributeType, true);
this.BindingConfiguration.Condition = r => r.Target != null && r.Target.Member.IsDefined(attributeType, true);

return this;
}
Expand Down

0 comments on commit 0997c87

Please sign in to comment.