Add global usings (#668)

* Use global usings

* Fix namespace declarations for test projects
This commit is contained in:
Patrik Svensson
2021-12-23 16:50:31 +01:00
committed by GitHub
parent eb6a9d8d04
commit 52c1d9122b
514 changed files with 10659 additions and 12441 deletions

View File

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

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

View File

@ -1,20 +1,16 @@
using System;
using System.Diagnostics;
namespace Spectre.Console;
namespace Spectre.Console
public static class ShouldlyExtensions
{
public static class ShouldlyExtensions
[DebuggerStepThrough]
public static T And<T>(this T item, Action<T> action)
{
[DebuggerStepThrough]
public static T And<T>(this T item, Action<T> action)
if (action == null)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
action(item);
return item;
throw new ArgumentNullException(nameof(action));
}
action(item);
return item;
}
}
}

View File

@ -1,44 +1,38 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Spectre.Console.Testing;
namespace Spectre.Console.Tests;
namespace Spectre.Console.Tests
public static class TestConsoleExtensions
{
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)
{
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))
{
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()));
throw new InvalidOperationException("Output buffer is not empty.");
}
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}";
});
}
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

@ -1,57 +1,52 @@
using System;
using System.Collections.Generic;
using Spectre.Console.Cli;
namespace Spectre.Console.Testing;
namespace Spectre.Console.Testing
public sealed class FakeTypeRegistrar : ITypeRegistrar
{
public sealed class FakeTypeRegistrar : ITypeRegistrar
public Dictionary<Type, List<Type>> Registrations { 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 Dictionary<Type, List<Type>> Registrations { get; }
public Dictionary<Type, List<object>> Instances { get; }
public Func<Dictionary<Type, List<Type>>, Dictionary<Type, List<object>>, ITypeResolver> TypeResolverFactory { get; set; }
Registrations = new Dictionary<Type, List<Type>>();
Instances = new Dictionary<Type, List<object>>();
}
public FakeTypeRegistrar()
public void Register(Type service, Type implementation)
{
if (!Registrations.ContainsKey(service))
{
Registrations = new Dictionary<Type, List<Type>>();
Instances = new Dictionary<Type, List<object>>();
Registrations.Add(service, new List<Type> { implementation });
}
public void Register(Type service, Type implementation)
else
{
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 TypeResolverFactory?.Invoke(Registrations, Instances);
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 TypeResolverFactory?.Invoke(Registrations, Instances);
}
}

View File

@ -1,41 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Spectre.Console.Cli;
namespace Spectre.Console.Testing;
namespace Spectre.Console.Testing
public sealed class FakeTypeResolver : ITypeResolver
{
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)
{
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;
}
_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;
}
}

View File

@ -1,14 +1,12 @@
using System;
namespace Spectre.Console.Tests;
namespace Spectre.Console.Tests
[AttributeUsage(AttributeTargets.Method)]
public sealed class GitHubIssueAttribute : Attribute
{
public sealed class GitHubIssueAttribute : Attribute
{
public int IssueId { get; }
public int IssueId { get; }
public GitHubIssueAttribute(int issueId)
{
IssueId = issueId;
}
public GitHubIssueAttribute(int issueId)
{
IssueId = issueId;
}
}
}

View File

@ -0,0 +1,8 @@
#if !NET5_0_OR_GREATER
namespace System.Runtime.CompilerServices;
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public sealed class ModuleInitializerAttribute : Attribute
{
}
#endif