(#1313) fixed errors in FakeTypeRegistrar and FakeTypeResolver

to make the unit tests pass.
This commit is contained in:
Nils Andresen 2023-11-10 23:05:55 +01:00 committed by Patrik Svensson
parent c448d0d5f6
commit dc402220f2
2 changed files with 39 additions and 6 deletions

View File

@ -44,6 +44,10 @@ public sealed class FakeTypeRegistrar : ITypeRegistrar
{
Instances.Add(service, new List<object> { implementation });
}
else
{
Instances[service].Add(implementation);
}
}
/// <inheritdoc/>
@ -58,6 +62,10 @@ public sealed class FakeTypeRegistrar : ITypeRegistrar
{
Instances.Add(service, new List<object> { factory() });
}
else
{
Instances[service].Add(factory());
}
}
/// <inheritdoc/>

View File

@ -1,3 +1,5 @@
using System.Collections;
namespace Spectre.Console.Testing;
/// <summary>
@ -29,18 +31,41 @@ public sealed class FakeTypeResolver : ITypeResolver
return null;
}
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
// return all registrations
type = type.GenericTypeArguments[0];
var allRegistrations = Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
var castList = allRegistrations as IList;
if (_instances.TryGetValue(type, out var listInstances))
{
listInstances.ForEach(i => castList.Add(i));
}
if (_registrations.TryGetValue(type, out var listRegistrations))
{
listRegistrations
.Select<Type, object>(x => Activator.CreateInstance(x)!)
.ToList()
.ForEach(i => castList.Add(i));
}
return allRegistrations;
}
if (_instances.TryGetValue(type, out var instances))
{
return instances.FirstOrDefault();
return instances.LastOrDefault();
}
if (_registrations.TryGetValue(type, out var registrations))
{
// The type might be an interface, but the registration should be a class.
// So call CreateInstance on the first registration rather than the type.
{
// The type might be an interface, but the registration should be a class.
// So call CreateInstance on the first registration rather than the type.
return registrations.Count == 0
? null
: Activator.CreateInstance(registrations[0]);
? null
: Activator.CreateInstance(registrations.Last());
}
return null;