mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
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:
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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}";
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user