mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 08:52:50 +08:00
47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
namespace Spectre.Console.Testing;
|
|
|
|
/// <summary>
|
|
/// A fake type resolver suitable for testing.
|
|
/// </summary>
|
|
public sealed class FakeTypeResolver : ITypeResolver
|
|
{
|
|
private readonly Dictionary<Type, List<Type>> _registrations;
|
|
private readonly Dictionary<Type, List<object>> _instances;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="FakeTypeResolver"/> class.
|
|
/// </summary>
|
|
/// <param name="registrations">The registrations.</param>
|
|
/// <param name="instances">The singleton registrations.</param>
|
|
public FakeTypeResolver(
|
|
Dictionary<Type, List<Type>> registrations,
|
|
Dictionary<Type, List<object>> instances)
|
|
{
|
|
_registrations = registrations ?? throw new ArgumentNullException(nameof(registrations));
|
|
_instances = instances ?? throw new ArgumentNullException(nameof(instances));
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public object? Resolve(Type? type)
|
|
{
|
|
if (type == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (_instances.TryGetValue(type, out var instances))
|
|
{
|
|
return instances.FirstOrDefault();
|
|
}
|
|
|
|
if (_registrations.TryGetValue(type, out var registrations))
|
|
{
|
|
return registrations.Count == 0
|
|
? null
|
|
: Activator.CreateInstance(type);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|