diff --git a/src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs b/src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs index 01a922f0..5e09e08c 100644 --- a/src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs +++ b/src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs @@ -1,7 +1,10 @@ using System.Collections.Immutable; -using System.IO; using Microsoft.CodeAnalysis.Testing; +#if !NET472 +using System.IO; +#endif + static class CodeAnalyzerHelper { public static readonly ReferenceAssemblies CurrentXunitV2; diff --git a/src/xunit.analyzers.tests/Utility/XunitVerifier.cs b/src/xunit.analyzers.tests/Utility/XunitVerifier.cs index c20c22ed..9e096122 100644 --- a/src/xunit.analyzers.tests/Utility/XunitVerifier.cs +++ b/src/xunit.analyzers.tests/Utility/XunitVerifier.cs @@ -18,14 +18,20 @@ public XunitVerifier() : this(ImmutableStack.Empty) { } - protected XunitVerifier(ImmutableStack context) - { + protected XunitVerifier(ImmutableStack context) => Context = context ?? throw new ArgumentNullException(nameof(context)); - } protected ImmutableStack Context { get; } - public virtual void Empty( + protected virtual string CreateMessage(string? message) + { + foreach (var frame in Context) + message = "Context: " + frame + Environment.NewLine + message; + + return message ?? string.Empty; + } + + void IVerifier.Empty( string collectionName, IEnumerable collection) { @@ -36,30 +42,27 @@ public virtual void Empty( throw EmptyException.ForNamedNonEmptyCollection(tracker.FormatStart(), collectionName); } - public virtual void Equal( + void IVerifier.Equal( T expected, T actual, - string? message = null) + string? message) { - if (message is null && Context.IsEmpty) - Assert.Equal(expected, actual); - else if (!EqualityComparer.Default.Equals(expected, actual)) - throw EqualException.ForMismatchedValuesWithMessage(expected, actual, CreateMessage(message)); - } + if (EqualityComparer.Default.Equals(expected, actual)) + return; - public virtual void True( - [DoesNotReturnIf(false)] bool assert, - string? message = null) - { if (message is null && Context.IsEmpty) - Assert.True(assert); + throw EqualException.ForMismatchedValues(expected, actual); else - Assert.True(assert, CreateMessage(message)); + throw EqualException.ForMismatchedValuesWithMessage(expected, actual, CreateMessage(message)); } - public virtual void False( + [DoesNotReturn] + void IVerifier.Fail(string? message) => + Assert.Fail(message is null ? "" : CreateMessage(message)); + + void IVerifier.False( [DoesNotReturnIf(true)] bool assert, - string? message = null) + string? message) { if (message is null && Context.IsEmpty) Assert.False(assert); @@ -67,23 +70,10 @@ public virtual void False( Assert.False(assert, CreateMessage(message)); } - [DoesNotReturn] - public virtual void Fail(string? message = null) - { - if (message is null && Context.IsEmpty) - Assert.True(false); - else - Assert.True(false, CreateMessage(message)); - - throw new InvalidOperationException("This code is unreachable"); - } - - public virtual void LanguageIsSupported(string language) - { + void IVerifier.LanguageIsSupported(string language) => Assert.False(language != LanguageNames.CSharp && language != LanguageNames.VisualBasic, CreateMessage($"Unsupported Language: '{language}'")); - } - public virtual void NotEmpty( + void IVerifier.NotEmpty( string collectionName, IEnumerable collection) { @@ -93,11 +83,18 @@ public virtual void NotEmpty( throw NotEmptyException.ForNamedNonEmptyCollection(collectionName); } - public virtual void SequenceEqual( + IVerifier IVerifier.PushContext(string context) + { + Assert.IsAssignableFrom(this); + + return new XunitVerifier(Context.Push(context)); + } + + void IVerifier.SequenceEqual( IEnumerable expected, IEnumerable actual, - IEqualityComparer? equalityComparer = null, - string? message = null) + IEqualityComparer? equalityComparer, + string? message) { var comparer = new SequenceEqualEnumerableEqualityComparer(equalityComparer); var areEqual = comparer.Equals(expected, actual); @@ -106,34 +103,30 @@ public virtual void SequenceEqual( throw EqualException.ForMismatchedValuesWithMessage(expected, actual, CreateMessage(message)); } - public virtual IVerifier PushContext(string context) - { - Assert.IsAssignableFrom(this); - - return new XunitVerifier(Context.Push(context)); - } - - protected virtual string CreateMessage(string? message) + void IVerifier.True( + [DoesNotReturnIf(false)] bool assert, + string? message) { - foreach (var frame in Context) - message = "Context: " + frame + Environment.NewLine + message; - - return message ?? string.Empty; + if (message is null && Context.IsEmpty) + Assert.True(assert); + else + Assert.True(assert, CreateMessage(message)); } sealed class SequenceEqualEnumerableEqualityComparer : IEqualityComparer?> { readonly IEqualityComparer itemEqualityComparer; - public SequenceEqualEnumerableEqualityComparer(IEqualityComparer? itemEqualityComparer) - { + public SequenceEqualEnumerableEqualityComparer(IEqualityComparer? itemEqualityComparer) => this.itemEqualityComparer = itemEqualityComparer ?? EqualityComparer.Default; - } public bool Equals(IEnumerable? x, IEnumerable? y) { - if (ReferenceEquals(x, y)) { return true; } - if (x is null || y is null) { return false; } + if (ReferenceEquals(x, y)) + return true; + + if (x is null || y is null) + return false; return x.SequenceEqual(y, itemEqualityComparer); }