Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #578 by setting serviceName to string.Empty if no registrations for the specified serviceType are found. #579

Merged
merged 2 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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