Skip to content

Commit

Permalink
Update verifier for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Jan 31, 2024
1 parent 2a50dc0 commit 7939f88
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 55 deletions.
5 changes: 4 additions & 1 deletion src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
101 changes: 47 additions & 54 deletions src/xunit.analyzers.tests/Utility/XunitVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ public XunitVerifier() :
this(ImmutableStack<string>.Empty)
{ }

protected XunitVerifier(ImmutableStack<string> context)
{
protected XunitVerifier(ImmutableStack<string> context) =>
Context = context ?? throw new ArgumentNullException(nameof(context));
}

protected ImmutableStack<string> Context { get; }

public virtual void Empty<T>(
protected virtual string CreateMessage(string? message)
{
foreach (var frame in Context)
message = "Context: " + frame + Environment.NewLine + message;

return message ?? string.Empty;
}

void IVerifier.Empty<T>(
string collectionName,
IEnumerable<T> collection)
{
Expand All @@ -36,54 +42,38 @@ public virtual void Empty<T>(
throw EmptyException.ForNamedNonEmptyCollection(tracker.FormatStart(), collectionName);
}

public virtual void Equal<T>(
void IVerifier.Equal<T>(
T expected,
T actual,
string? message = null)
string? message)
{
if (message is null && Context.IsEmpty)
Assert.Equal(expected, actual);
else if (!EqualityComparer<T>.Default.Equals(expected, actual))
throw EqualException.ForMismatchedValuesWithMessage(expected, actual, CreateMessage(message));
}
if (EqualityComparer<T>.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 ? "<no message provided>" : CreateMessage(message));

void IVerifier.False(
[DoesNotReturnIf(true)] bool assert,
string? message = null)
string? message)
{
if (message is null && Context.IsEmpty)
Assert.False(assert);
else
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<T>(
void IVerifier.NotEmpty<T>(
string collectionName,
IEnumerable<T> collection)
{
Expand All @@ -93,11 +83,18 @@ public virtual void NotEmpty<T>(
throw NotEmptyException.ForNamedNonEmptyCollection(collectionName);
}

public virtual void SequenceEqual<T>(
IVerifier IVerifier.PushContext(string context)
{
Assert.IsAssignableFrom<XunitVerifier>(this);

return new XunitVerifier(Context.Push(context));
}

void IVerifier.SequenceEqual<T>(
IEnumerable<T> expected,
IEnumerable<T> actual,
IEqualityComparer<T>? equalityComparer = null,
string? message = null)
IEqualityComparer<T>? equalityComparer,
string? message)
{
var comparer = new SequenceEqualEnumerableEqualityComparer<T>(equalityComparer);
var areEqual = comparer.Equals(expected, actual);
Expand All @@ -106,34 +103,30 @@ public virtual void SequenceEqual<T>(
throw EqualException.ForMismatchedValuesWithMessage(expected, actual, CreateMessage(message));
}

public virtual IVerifier PushContext(string context)
{
Assert.IsAssignableFrom<XunitVerifier>(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<T> : IEqualityComparer<IEnumerable<T>?>
{
readonly IEqualityComparer<T> itemEqualityComparer;

public SequenceEqualEnumerableEqualityComparer(IEqualityComparer<T>? itemEqualityComparer)
{
public SequenceEqualEnumerableEqualityComparer(IEqualityComparer<T>? itemEqualityComparer) =>
this.itemEqualityComparer = itemEqualityComparer ?? EqualityComparer<T>.Default;
}

public bool Equals(IEnumerable<T>? x, IEnumerable<T>? 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);
}
Expand Down

0 comments on commit 7939f88

Please sign in to comment.