Moved analyzer tests to its own project

Also moves tests to `./test` which makes it possible
for all test projects to share the same .editorconfig
files and similar.
This commit is contained in:
Patrik Svensson
2021-06-23 22:47:12 +02:00
parent 721d73e9eb
commit c9b178ac96
307 changed files with 114 additions and 84 deletions

View File

@ -0,0 +1,38 @@
using System;
using System.IO;
using System.Reflection;
namespace Spectre.Console.Tests
{
public static class EmbeddedResourceReader
{
public static Stream LoadResourceStream(string resourceName)
{
if (resourceName is null)
{
throw new ArgumentNullException(nameof(resourceName));
}
var assembly = Assembly.GetCallingAssembly();
resourceName = resourceName.Replace("/", ".");
return assembly.GetManifestResourceStream(resourceName);
}
public static Stream LoadResourceStream(Assembly assembly, string resourceName)
{
if (assembly is null)
{
throw new ArgumentNullException(nameof(assembly));
}
if (resourceName is null)
{
throw new ArgumentNullException(nameof(resourceName));
}
resourceName = resourceName.Replace("/", ".");
return assembly.GetManifestResourceStream(resourceName);
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Linq;
using Shouldly;
namespace Spectre.Console.Cli
{
public static class CommandContextExtensions
{
public static void ShouldHaveRemainingArgument(this CommandContext context, string name, string[] values)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
context.Remaining.Parsed.Contains(name).ShouldBeTrue();
context.Remaining.Parsed[name].Count().ShouldBe(values.Length);
foreach (var value in values)
{
context.Remaining.Parsed[name].ShouldContain(value);
}
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Diagnostics;
namespace Spectre.Console
{
public 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,44 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Spectre.Console.Testing;
namespace Spectre.Console.Tests
{
public static class TestConsoleExtensions
{
private static readonly Regex _lineNumberRegex = new Regex(":\\d+", RegexOptions.Singleline);
private static readonly Regex _filenameRegex = new Regex("\\sin\\s.*cs:nn", RegexOptions.Multiline);
public static string WriteNormalizedException(this TestConsole console, Exception ex, ExceptionFormats formats = ExceptionFormats.Default)
{
if (!string.IsNullOrWhiteSpace(console.Output))
{
throw new InvalidOperationException("Output buffer is not empty.");
}
console.WriteException(ex, formats);
return string.Join("\n", NormalizeStackTrace(console.Output)
.NormalizeLineEndings()
.Split(new char[] { '\n' })
.Select(line => line.TrimEnd()));
}
public static string NormalizeStackTrace(string text)
{
text = _lineNumberRegex.Replace(text, match =>
{
return ":nn";
});
return _filenameRegex.Replace(text, match =>
{
var value = match.Value;
var index = value.LastIndexOfAny(new[] { '\\', '/' });
var filename = value.Substring(index + 1, value.Length - index - 1);
return $" in /xyz/{filename}";
});
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using Spectre.Console.Cli;
namespace Spectre.Console.Testing
{
public sealed class FakeTypeRegistrar : ITypeRegistrar
{
public Dictionary<Type, List<Type>> Registrations { get; }
public Dictionary<Type, List<object>> Instances { get; }
public FakeTypeRegistrar()
{
Registrations = new Dictionary<Type, List<Type>>();
Instances = new Dictionary<Type, List<object>>();
}
public void Register(Type service, Type implementation)
{
if (!Registrations.ContainsKey(service))
{
Registrations.Add(service, new List<Type> { implementation });
}
else
{
Registrations[service].Add(implementation);
}
}
public void RegisterInstance(Type service, object implementation)
{
if (!Instances.ContainsKey(service))
{
Instances.Add(service, new List<object> { implementation });
}
}
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() });
}
}
public ITypeResolver Build()
{
return null;
}
}
}