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

View File

@ -1,3 +1,5 @@
using System.Collections;
namespace Spectre.Console.Testing; namespace Spectre.Console.Testing;
/// <summary> /// <summary>
@ -29,9 +31,32 @@ public sealed class FakeTypeResolver : ITypeResolver
return null; 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)) if (_instances.TryGetValue(type, out var instances))
{ {
return instances.FirstOrDefault(); return instances.LastOrDefault();
} }
if (_registrations.TryGetValue(type, out var registrations)) if (_registrations.TryGetValue(type, out var registrations))
@ -40,7 +65,7 @@ public sealed class FakeTypeResolver : ITypeResolver
// So call CreateInstance on the first registration rather than the type. // So call CreateInstance on the first registration rather than the type.
return registrations.Count == 0 return registrations.Count == 0
? null ? null
: Activator.CreateInstance(registrations[0]); : Activator.CreateInstance(registrations.Last());
} }
return null; return null;