mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-14 16:02:50 +08:00
(#1313) fixed errors in FakeTypeRegistrar and FakeTypeResolver
to make the unit tests pass.
This commit is contained in:
parent
c448d0d5f6
commit
dc402220f2
@ -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/>
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user