Skip to content

Commit

Permalink
Added a class to generate strings that are null, empty, or whitespace…
Browse files Browse the repository at this point in the history
…. Updated tests calling CanGetInstance to use this sample data.
  • Loading branch information
mesakomarevich committed Oct 23, 2022
1 parent f902319 commit b23c485
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 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" };
}
}

0 comments on commit b23c485

Please sign in to comment.