mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 17:02:51 +08:00
Added a TypeRegistrar to CommandAppTester
exposed as a public property. For convenience, and to keep the similarity to the real CommandApp it is also available in the ctor of CommandAppTester.
This commit is contained in:
parent
b92827ce3d
commit
6b5b31957c
@ -11,6 +11,20 @@ namespace Spectre.Console.Testing
|
|||||||
private Action<CommandApp>? _appConfiguration;
|
private Action<CommandApp>? _appConfiguration;
|
||||||
private Action<IConfigurator>? _configuration;
|
private Action<IConfigurator>? _configuration;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="CommandAppTester"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="registrar">The registrar.</param>
|
||||||
|
public CommandAppTester(ITypeRegistrar? registrar = null)
|
||||||
|
{
|
||||||
|
Registrar = registrar;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the Registrar to use in the CommandApp.
|
||||||
|
/// </summary>
|
||||||
|
public ITypeRegistrar? Registrar { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the default command.
|
/// Sets the default command.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -88,7 +102,7 @@ namespace Spectre.Console.Testing
|
|||||||
CommandContext? context = null;
|
CommandContext? context = null;
|
||||||
CommandSettings? settings = null;
|
CommandSettings? settings = null;
|
||||||
|
|
||||||
var app = new CommandApp();
|
var app = new CommandApp(Registrar);
|
||||||
_appConfiguration?.Invoke(app);
|
_appConfiguration?.Invoke(app);
|
||||||
|
|
||||||
if (_configuration != null)
|
if (_configuration != null)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using Spectre.Console.Testing;
|
using Spectre.Console.Testing;
|
||||||
using Spectre.Console.Tests.Data;
|
using Spectre.Console.Tests.Data;
|
||||||
@ -62,6 +64,36 @@ namespace Spectre.Console.Tests.Unit.Cli
|
|||||||
injected.Age.ShouldBe(35);
|
injected.Age.ShouldBe(35);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_Inject_Dependency_Using_A_Given_Registrar()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var dependency = new FakeDependency();
|
||||||
|
var registrar = new FakeTypeRegistrar { TypeResolverFactory = FakeTypeResolver.Factory };
|
||||||
|
registrar.RegisterInstance(typeof(FakeDependency), dependency);
|
||||||
|
var app = new CommandAppTester(registrar);
|
||||||
|
|
||||||
|
app.SetDefaultCommand<GenericCommand<InjectSettings>>();
|
||||||
|
app.Configure(config => config.PropagateExceptions());
|
||||||
|
|
||||||
|
// When
|
||||||
|
var result = app.Run(new[]
|
||||||
|
{
|
||||||
|
"--name", "foo",
|
||||||
|
"--age", "35",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.ExitCode.ShouldBe(0);
|
||||||
|
result.Settings.ShouldBeOfType<InjectSettings>().And(injected =>
|
||||||
|
{
|
||||||
|
injected.ShouldNotBeNull();
|
||||||
|
injected.Fake.ShouldBeSameAs(dependency);
|
||||||
|
injected.Name.ShouldBe("Hello foo");
|
||||||
|
injected.Age.ShouldBe(35);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ namespace Spectre.Console.Testing
|
|||||||
{
|
{
|
||||||
public Dictionary<Type, List<Type>> Registrations { get; }
|
public Dictionary<Type, List<Type>> Registrations { get; }
|
||||||
public Dictionary<Type, List<object>> Instances { get; }
|
public Dictionary<Type, List<object>> Instances { get; }
|
||||||
|
public Func<Dictionary<Type, List<Type>>, Dictionary<Type, List<object>>, ITypeResolver> TypeResolverFactory { get; set; }
|
||||||
|
|
||||||
public FakeTypeRegistrar()
|
public FakeTypeRegistrar()
|
||||||
{
|
{
|
||||||
@ -50,7 +51,7 @@ namespace Spectre.Console.Testing
|
|||||||
|
|
||||||
public ITypeResolver Build()
|
public ITypeResolver Build()
|
||||||
{
|
{
|
||||||
return null;
|
return TypeResolverFactory?.Invoke(Registrations, Instances);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
41
test/Spectre.Console.Tests/Utilities/FakeTypeResolver.cs
Normal file
41
test/Spectre.Console.Tests/Utilities/FakeTypeResolver.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Spectre.Console.Cli;
|
||||||
|
|
||||||
|
namespace Spectre.Console.Testing
|
||||||
|
{
|
||||||
|
public sealed class FakeTypeResolver : ITypeResolver
|
||||||
|
{
|
||||||
|
private readonly Dictionary<Type, List<Type>> _registrations;
|
||||||
|
private readonly Dictionary<Type, List<object>> _instances;
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Func<Dictionary<Type, List<Type>>, Dictionary<Type, List<object>>, ITypeResolver> Factory =>
|
||||||
|
(r, i) => new FakeTypeResolver(r, i);
|
||||||
|
|
||||||
|
public object Resolve(Type type)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user