mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 08:52:50 +08:00
(#1313) Add new test to TypeRegistrarBaseTests
to assert the assumptions we're making in the code.
This commit is contained in:
parent
1fd028942f
commit
ead7115cbe
@ -23,13 +23,16 @@ public sealed class TypeRegistrarBaseTests
|
|||||||
/// <exception cref="TestFailedException">This exception is raised, if a test fails.</exception>
|
/// <exception cref="TestFailedException">This exception is raised, if a test fails.</exception>
|
||||||
public void RunAllTests()
|
public void RunAllTests()
|
||||||
{
|
{
|
||||||
var testCases = new Action<ITypeRegistrar>[]
|
var testCases = new[]
|
||||||
{
|
{
|
||||||
RegistrationsCanBeResolved,
|
RegistrationsCanBeResolved,
|
||||||
InstanceRegistrationsCanBeResolved,
|
InstanceRegistrationsCanBeResolved,
|
||||||
LazyRegistrationsCanBeResolved,
|
LazyRegistrationsCanBeResolved,
|
||||||
ResolvingNotRegisteredServiceReturnsNull,
|
ResolvingNotRegisteredServiceReturnsNull,
|
||||||
ResolvingNullTypeReturnsNull,
|
ResolvingNullTypeReturnsNull,
|
||||||
|
ResolvingSingleInstanceOfMultipleRegistrationsResolvesTheFirstOne,
|
||||||
|
ResolvingAnEnumerableOfInstancesDoesNotReturnNull,
|
||||||
|
ResolvingAnEnumerableOfInstancesOfMultipleRegistrationsResolvesAllRegistrations,
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach (var test in testCases)
|
foreach (var test in testCases)
|
||||||
@ -38,6 +41,80 @@ public sealed class TypeRegistrarBaseTests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ResolvingAnEnumerableOfInstancesDoesNotReturnNull(ITypeRegistrar registrar)
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var resolver = registrar.Build();
|
||||||
|
|
||||||
|
// When
|
||||||
|
var actual = resolver.Resolve(typeof(IEnumerable<IMockService>)) as IEnumerable<IMockService>;
|
||||||
|
|
||||||
|
// Then
|
||||||
|
if (actual == null)
|
||||||
|
{
|
||||||
|
throw new TestFailedException(
|
||||||
|
"Expected an IEnumerable never to resolve to null.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ResolvingAnEnumerableOfInstancesOfMultipleRegistrationsResolvesAllRegistrations(ITypeRegistrar registrar)
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var theLastRegistration = new AnotherMockService("last");
|
||||||
|
registrar.RegisterLazy(typeof(IMockService), () => new AnotherMockService("first"));
|
||||||
|
registrar.Register(typeof(IMockService), typeof(MockService));
|
||||||
|
registrar.RegisterInstance(typeof(IMockService), theLastRegistration);
|
||||||
|
var resolver = registrar.Build();
|
||||||
|
|
||||||
|
// When
|
||||||
|
var actual = (resolver.Resolve(typeof(IEnumerable<IMockService>)) as IEnumerable<IMockService>)!.ToList();
|
||||||
|
|
||||||
|
// Then
|
||||||
|
if (actual.Count != 3)
|
||||||
|
{
|
||||||
|
throw new TestFailedException(
|
||||||
|
"Expected the resolver to resolve a list with exactly 3 elements.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actual.Count(x => x.GetType() == typeof(AnotherMockService)) != 2)
|
||||||
|
{
|
||||||
|
throw new TestFailedException(
|
||||||
|
$"Expected the resolver to resolve a list with exactly 2 elements of type {nameof(AnotherMockService)}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actual.Count(x => x.GetType() == typeof(MockService)) != 1)
|
||||||
|
{
|
||||||
|
throw new TestFailedException(
|
||||||
|
$"Expected the resolver to resolve a list with exactly one element of type {nameof(MockService)}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!actual.Contains(theLastRegistration))
|
||||||
|
{
|
||||||
|
throw new TestFailedException(
|
||||||
|
"Expected the resolver to resolve the known instance that was registered.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ResolvingSingleInstanceOfMultipleRegistrationsResolvesTheFirstOne(ITypeRegistrar registrar)
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var theLastRegistration = new AnotherMockService("last");
|
||||||
|
registrar.RegisterLazy(typeof(IMockService), () => new AnotherMockService("first"));
|
||||||
|
registrar.Register(typeof(IMockService), typeof(MockService));
|
||||||
|
registrar.RegisterInstance(typeof(IMockService), theLastRegistration);
|
||||||
|
var resolver = registrar.Build();
|
||||||
|
|
||||||
|
// When
|
||||||
|
var actual = resolver.Resolve(typeof(IMockService));
|
||||||
|
|
||||||
|
// Then
|
||||||
|
if (!ReferenceEquals(actual, theLastRegistration))
|
||||||
|
{
|
||||||
|
throw new TestFailedException(
|
||||||
|
"Expected the resolver to resolve the first registered instance of multiple registrations.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void ResolvingNullTypeReturnsNull(ITypeRegistrar registrar)
|
private static void ResolvingNullTypeReturnsNull(ITypeRegistrar registrar)
|
||||||
{
|
{
|
||||||
// Given no registration
|
// Given no registration
|
||||||
@ -167,6 +244,11 @@ public sealed class TypeRegistrarBaseTests
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class AnotherMockService : IMockService
|
||||||
|
{
|
||||||
|
public AnotherMockService(string _){}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Exception, to be raised when a test fails.
|
/// Exception, to be raised when a test fails.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user