mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-07-06 04:28:15 +08:00
Move Spectre.Console.Cli to it's own package
This commit is contained in:

committed by
Patrik Svensson

parent
b600832e00
commit
36ca22ffac
16
src/Spectre.Console.Testing/Extensions/ShouldlyExtensions.cs
Normal file
16
src/Spectre.Console.Testing/Extensions/ShouldlyExtensions.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace Spectre.Console;
|
||||
|
||||
internal static class ShouldlyExtensions
|
||||
{
|
||||
[DebuggerStepThrough]
|
||||
public static T And<T>(this T item, Action<T> action)
|
||||
{
|
||||
if (action == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(action));
|
||||
}
|
||||
|
||||
action(item);
|
||||
return item;
|
||||
}
|
||||
}
|
68
src/Spectre.Console.Testing/FakeTypeRegistrar.cs
Normal file
68
src/Spectre.Console.Testing/FakeTypeRegistrar.cs
Normal file
@ -0,0 +1,68 @@
|
||||
namespace Spectre.Console.Testing;
|
||||
|
||||
/// <summary>
|
||||
/// A fake type registrar suitable for testing.
|
||||
/// </summary>
|
||||
public sealed class FakeTypeRegistrar : ITypeRegistrar
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets all registrations.
|
||||
/// </summary>
|
||||
public Dictionary<Type, List<Type>> Registrations { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets all singleton registrations.
|
||||
/// </summary>
|
||||
public Dictionary<Type, List<object>> Instances { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FakeTypeRegistrar"/> class.
|
||||
/// </summary>
|
||||
public FakeTypeRegistrar()
|
||||
{
|
||||
Registrations = new Dictionary<Type, List<Type>>();
|
||||
Instances = new Dictionary<Type, List<object>>();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Register(Type service, Type implementation)
|
||||
{
|
||||
if (!Registrations.ContainsKey(service))
|
||||
{
|
||||
Registrations.Add(service, new List<Type> { implementation });
|
||||
}
|
||||
else
|
||||
{
|
||||
Registrations[service].Add(implementation);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void RegisterInstance(Type service, object implementation)
|
||||
{
|
||||
if (!Instances.ContainsKey(service))
|
||||
{
|
||||
Instances.Add(service, new List<object> { implementation });
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void RegisterLazy(Type service, Func<object> factory)
|
||||
{
|
||||
if (factory is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(factory));
|
||||
}
|
||||
|
||||
if (!Instances.ContainsKey(service))
|
||||
{
|
||||
Instances.Add(service, new List<object> { factory() });
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ITypeResolver Build()
|
||||
{
|
||||
return new FakeTypeResolver(Registrations, Instances);
|
||||
}
|
||||
}
|
46
src/Spectre.Console.Testing/FakeTypeResolver.cs
Normal file
46
src/Spectre.Console.Testing/FakeTypeResolver.cs
Normal file
@ -0,0 +1,46 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Diagnostics;
|
||||
global using System.IO;
|
||||
global using System.Linq;
|
||||
global using System.Threading;
|
||||
global using System.Threading.Tasks;
|
||||
global using Spectre.Console.Cli;
|
||||
|
@ -8,12 +8,18 @@
|
||||
<Description>Contains testing utilities for Spectre.Console.</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="Spectre.Console.Tests" />
|
||||
<InternalsVisibleTo Include="Spectre.Console.Cli.Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AdditionalFiles Include="..\stylecop.json" Link="Properties/stylecop.json" />
|
||||
<None Include="../../resources/gfx/small-logo.png" Pack="true" PackagePath="\" Link="Properties/small-logo.png" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Label="Project References">
|
||||
<ProjectReference Include="..\Spectre.Console.Cli\Spectre.Console.Cli.csproj" />
|
||||
<ProjectReference Include="..\Spectre.Console\Spectre.Console.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
Reference in New Issue
Block a user