Skip to content

Commit

Permalink
Merge pull request #579 from mesakomarevich/fixing_CanGetInstance
Browse files Browse the repository at this point in the history
Fixed issue #578 by setting serviceName to string.Empty if no registrations for the specified serviceType are found.
  • Loading branch information
seesharper authored Oct 23, 2022
2 parents f4a15b2 + b23c485 commit 272c770
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
14 changes: 8 additions & 6 deletions src/LightInject.Tests/LazyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ public void GetInstance_LazyService_CreatesTargetWhenValuePropertyIsAccessed()
Assert.IsAssignableFrom<Foo>(instance.Value);
}

[Fact]
public void CanGetInstance_LazyForKnownService_ReturnsTrue()
[Theory]
[MemberData(nameof(StringDataGenerator.NullOrWhiteSpaceData), MemberType = typeof(StringDataGenerator))]
public void CanGetInstance_LazyForKnownService_ReturnsTrue(string serviceName)
{
var container = CreateContainer();
container.Register<IFoo, Foo>();
Assert.True(container.CanGetInstance(typeof(Lazy<IFoo>), string.Empty));
Assert.True(container.CanGetInstance(typeof(Lazy<IFoo>), serviceName));
}

[Fact]
public void CanGetInstance_LazyForUnknownService_ReturnsFalse()
[Theory]
[MemberData(nameof(StringDataGenerator.NullOrWhiteSpaceData), MemberType = typeof(StringDataGenerator))]
public void CanGetInstance_LazyForUnknownService_ReturnsFalse(string serviceName)
{
var container = CreateContainer();
Assert.False(container.CanGetInstance(typeof(Lazy<IFoo>), string.Empty));
Assert.False(container.CanGetInstance(typeof(Lazy<IFoo>), serviceName));
}

[Fact]
Expand Down
50 changes: 29 additions & 21 deletions src/LightInject.Tests/ServiceContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,59 +1127,67 @@ public void GetInstance_UsingFallbackForDependency_ReturnsSingleInstance()

}

[Fact]
public void CanGetInstance_KnownService_ReturnsTrue()
[Theory]
[MemberData(nameof(StringDataGenerator.NullOrWhiteSpaceData), MemberType = typeof(StringDataGenerator))]
public void CanGetInstance_KnownService_ReturnsTrue(string serviceName)
{
var container = CreateContainer();
container.Register<IFoo, Foo>();
var canCreateInstance = container.CanGetInstance(typeof(IFoo), string.Empty);
var canCreateInstance = container.CanGetInstance(typeof(IFoo), serviceName);
Assert.True(canCreateInstance);
}
[Fact]
public void CanGetInstance_UnknownService_ReturnFalse()

[Theory]
[MemberData(nameof(StringDataGenerator.NullOrWhiteSpaceData), MemberType = typeof(StringDataGenerator))]
public void CanGetInstance_UnknownService_ReturnFalse(string serviceName)
{
var container = CreateContainer();
container.Register<IFoo, Foo>();
var canCreateInstance = container.CanGetInstance(typeof(IBar), string.Empty);
var canCreateInstance = container.CanGetInstance(typeof(IBar), serviceName);
Assert.False(canCreateInstance);
}

[Fact]
public void CanGetInstance_FuncForKnownService_ReturnsTrue()
[Theory]
[MemberData(nameof(StringDataGenerator.NullOrWhiteSpaceData), MemberType = typeof(StringDataGenerator))]
public void CanGetInstance_FuncForKnownService_ReturnsTrue(string serviceName)
{
var container = CreateContainer();
container.Register<IFoo, Foo>();
Assert.True(container.CanGetInstance(typeof(Func<IFoo>), string.Empty));
Assert.True(container.CanGetInstance(typeof(Func<IFoo>), serviceName));
}

[Fact]
public void CanGetInstance_FuncForUnknownService_ReturnsFalse()
[Theory]
[MemberData(nameof(StringDataGenerator.NullOrWhiteSpaceData), MemberType = typeof(StringDataGenerator))]
public void CanGetInstance_FuncForUnknownService_ReturnsFalse(string serviceName)
{
var container = CreateContainer();
Assert.False(container.CanGetInstance(typeof(Func<IFoo>), string.Empty));
Assert.False(container.CanGetInstance(typeof(Func<IFoo>), serviceName));
}

[Fact]
public void CanGetInstance_ExplicitlyRegisteredFunc_ReturnsTrue()
[Theory]
[MemberData(nameof(StringDataGenerator.NullOrWhiteSpaceData), MemberType = typeof(StringDataGenerator))]
public void CanGetInstance_ExplicitlyRegisteredFunc_ReturnsTrue(string serviceName)
{
var container = CreateContainer();
container.Register<Func<IFoo>>(f => (() => new Foo()));
Assert.True(container.CanGetInstance(typeof(Func<IFoo>), string.Empty));
Assert.True(container.CanGetInstance(typeof(Func<IFoo>), serviceName));
}

[Fact]
public void CanGetInstance_ParameterizedFuncForKnownService_ReturnsTrue()
[Theory]
[MemberData(nameof(StringDataGenerator.NullOrWhiteSpaceData), MemberType = typeof(StringDataGenerator))]
public void CanGetInstance_ParameterizedFuncForKnownService_ReturnsTrue(string serviceName)
{
var container = CreateContainer();
container.Register<int, IFoo>((factory, i) => new FooWithOneParameter(i));
Assert.True(container.CanGetInstance(typeof(Func<IFoo>), string.Empty));
Assert.True(container.CanGetInstance(typeof(Func<IFoo>), serviceName));
}

[Fact]
public void CanGetInstance_ParameterizedFuncForUnknownService_ReturnsFalse()
[Theory]
[MemberData(nameof(StringDataGenerator.NullOrWhiteSpaceData), MemberType = typeof(StringDataGenerator))]
public void CanGetInstance_ParameterizedFuncForUnknownService_ReturnsFalse(string serviceName)
{
var container = CreateContainer();
Assert.False(container.CanGetInstance(typeof(Func<IFoo>), string.Empty));
Assert.False(container.CanGetInstance(typeof(Func<IFoo>), serviceName));
}

[Fact]
Expand Down
16 changes: 16 additions & 0 deletions src/LightInject.Tests/StringDataGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace LightInject.Tests;

public static class StringDataGenerator
{
public static IEnumerable<object[]> NullOrWhiteSpaceData()
{
yield return new object[] { null };
yield return new object[] { string.Empty };
yield return new object[] { " " };
yield return new object[] { "\t" };
yield return new object[] { "\n" };
yield return new object[] { "\r" };
}
}
5 changes: 4 additions & 1 deletion src/LightInject/LightInject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3747,9 +3747,12 @@ private Action<IEmitter> GetRegisteredEmitMethod(Type serviceType, string servic
if (registrations.Count > 1)
{
var serviceNames = registrations.Keys.OrderBy(k => k).ToArray();
var defaultServiceName = string.Empty;
serviceName = options.DefaultServiceSelector(serviceNames);
}
else
{
serviceName = string.Empty;
}
}

registrations.TryGetValue(serviceName, out Action<IEmitter> emitMethod);
Expand Down

0 comments on commit 272c770

Please sign in to comment.