Move Spectre.Console.Cli to it's own package

This commit is contained in:
Patrik Svensson
2022-05-14 22:56:36 +02:00
committed by Patrik Svensson
parent b600832e00
commit 36ca22ffac
262 changed files with 736 additions and 48 deletions

View 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;
}
}

View 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);
}
}

View 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;
}
}

View File

@ -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;

View File

@ -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>