Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

// ReSharper disable once CheckNamespace
Expand All @@ -17,6 +18,7 @@ namespace System.Collections.Generic
/// property
/// to access the singleton instance of this type.
/// </remarks>
[ExcludeFromCodeCoverage]
internal sealed class ReferenceEqualityComparer : IEqualityComparer<object?>, IEqualityComparer
{
private ReferenceEqualityComparer() { }
Expand Down
27 changes: 18 additions & 9 deletions Source/aweXpect/Equivalency/EquivalencyComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public string GetExtendedFailure(string it, ExpectationGrammars grammars, object
{
if (grammars.HasFlag(ExpectationGrammars.Negated))
{
return $"{it} was considered equivalent to {Formatter.Format(actual, FormattingOptions.Indented())}";
return $"{it} was considered equivalent for {Formatter.Format(actual, FormattingOptions.Indented())}";
}

if (actual is null != expected is null)
Expand All @@ -50,14 +50,9 @@ public string GetExtendedFailure(string it, ExpectationGrammars grammars, object
return _failureBuilder.ToString();
}

if (_failureBuilder.Length > 0)
{
_failureBuilder.Insert(0, " was not:");
_failureBuilder.Insert(0, it);
return _failureBuilder.ToString();
}

return $"{it} was considered equivalent";
_failureBuilder.Insert(0, " was not:");
_failureBuilder.Insert(0, it);
return _failureBuilder.ToString();
}

private static bool HandleSpecialCases<TActual, TExpected>(TActual actual, TExpected expected,
Expand All @@ -69,6 +64,13 @@ private static bool HandleSpecialCases<TActual, TExpected>(TActual actual, TExpe
isConsideredEqual = actualEqualityComparer.Equals(actual, expected);
if (isConsideredEqual == false)
{
failureBuilder.AppendLine();
if (failureBuilder.Length > 2)
Comment thread
vbreuss marked this conversation as resolved.
{
failureBuilder.AppendLine("and");
}

failureBuilder.Append(" ");
Formatter.Format(failureBuilder, actual, FormattingOptions.SingleLine);
failureBuilder.Append(" did not equal ");
Formatter.Format(failureBuilder, expected, FormattingOptions.SingleLine);
Expand All @@ -82,6 +84,13 @@ private static bool HandleSpecialCases<TActual, TExpected>(TActual actual, TExpe
isConsideredEqual = expectedEqualityComparer.Equals(actual, expected);
if (isConsideredEqual == false)
{
failureBuilder.AppendLine();
if (failureBuilder.Length > 2)
Comment thread
vbreuss marked this conversation as resolved.
{
failureBuilder.AppendLine("and");
}

failureBuilder.Append(" ");
Formatter.Format(failureBuilder, actual, FormattingOptions.SingleLine);
failureBuilder.Append(" did not equal ");
Formatter.Format(failureBuilder, expected, FormattingOptions.SingleLine);
Expand Down
77 changes: 54 additions & 23 deletions Source/aweXpect/That/Exceptions/ThatException.HasParamName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,38 @@ public ConstraintResult IsMetBy(Exception? actual)

protected override void AppendNormalExpectation(StringBuilder stringBuilder, string? indentation = null)
{
if (Grammars.HasFlag(ExpectationGrammars.Active))
if (expected is null)
{
stringBuilder.Append("with ParamName ");
}
else if (Grammars.HasFlag(ExpectationGrammars.Nested))
{
stringBuilder.Append("ParamName is ");
if (Grammars.HasFlag(ExpectationGrammars.Active))
{
stringBuilder.Append("with a ParamName");
}
else if (Grammars.HasFlag(ExpectationGrammars.Nested))
{
stringBuilder.Append("ParamName exists");
}
else
{
stringBuilder.Append("has a ParamName");
}
}
else
{
stringBuilder.Append("has ParamName ");
}
if (Grammars.HasFlag(ExpectationGrammars.Active))
{
stringBuilder.Append("with ParamName ");
}
else if (Grammars.HasFlag(ExpectationGrammars.Nested))
{
stringBuilder.Append("ParamName is ");
}
else
{
stringBuilder.Append("has ParamName ");
}

Formatter.Format(stringBuilder, expected);
Formatter.Format(stringBuilder, expected);
}
}

protected override void AppendNormalResult(StringBuilder stringBuilder, string? indentation = null)
Expand All @@ -65,29 +83,42 @@ protected override void AppendNormalResult(StringBuilder stringBuilder, string?
stringBuilder.Append(It).Append(" had ParamName ");
Formatter.Format(stringBuilder, argumentException.ParamName);
}
else
{
stringBuilder.Append(It).Append(" was ");
Formatter.Format(stringBuilder, Actual);
}
}

protected override void AppendNegatedExpectation(StringBuilder stringBuilder, string? indentation = null)
{
if (Grammars.HasFlag(ExpectationGrammars.Active))
{
stringBuilder.Append("without ParamName ");
}
else if (Grammars.HasFlag(ExpectationGrammars.Nested))
if (expected is null)
{
stringBuilder.Append("ParamName is not ");
if (Grammars.HasFlag(ExpectationGrammars.Active))
{
stringBuilder.Append("without a ParamName");
}
else if (Grammars.HasFlag(ExpectationGrammars.Nested))
{
stringBuilder.Append("ParamName does not exist");
}
else
{
stringBuilder.Append("does not have a ParamName");
}
}
else
{
stringBuilder.Append("does not have ParamName ");
}
if (Grammars.HasFlag(ExpectationGrammars.Active))
{
stringBuilder.Append("without ParamName ");
}
else if (Grammars.HasFlag(ExpectationGrammars.Nested))
{
stringBuilder.Append("ParamName is not ");
}
else
{
stringBuilder.Append("does not have ParamName ");
}

Formatter.Format(stringBuilder, expected);
Formatter.Format(stringBuilder, expected);
}
}

protected override void AppendNegatedResult(StringBuilder stringBuilder, string? indentation = null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Text;
using aweXpect.Core;

namespace aweXpect.Internal.Tests.ThatTests.Exceptions;

public class HasParamNameValueConstraintTests
{
[Theory]
[InlineData(ExpectationGrammars.None, "has ParamName \"foo\"")]
[InlineData(ExpectationGrammars.Negated, "does not have ParamName \"foo\"")]
[InlineData(ExpectationGrammars.Nested, "ParamName is \"foo\"")]
[InlineData(ExpectationGrammars.Nested | ExpectationGrammars.Negated, "ParamName is not \"foo\"")]
[InlineData(ExpectationGrammars.Active, "with ParamName \"foo\"")]
[InlineData(ExpectationGrammars.Active | ExpectationGrammars.Negated, "without ParamName \"foo\"")]
public async Task AppendExpectation_WithExpected_ShouldAppendExpectedText(ExpectationGrammars grammar,
string expected)
{
ThatException.HasParamNameValueConstraint<ArgumentException> sut = new("it", grammar, "foo");
StringBuilder sb = new();

sut.AppendExpectation(sb, "");

await That(sb.ToString()).IsEqualTo(expected);
}

[Theory]
[InlineData(ExpectationGrammars.None, "has a ParamName")]
[InlineData(ExpectationGrammars.Negated, "does not have a ParamName")]
[InlineData(ExpectationGrammars.Nested, "ParamName exists")]
[InlineData(ExpectationGrammars.Nested | ExpectationGrammars.Negated, "ParamName does not exist")]
[InlineData(ExpectationGrammars.Active, "with a ParamName")]
[InlineData(ExpectationGrammars.Active | ExpectationGrammars.Negated, "without a ParamName")]
public async Task AppendExpectation_WithNullAsExpected_ShouldAppendExpectedText(ExpectationGrammars grammar,
string expected)
{
ThatException.HasParamNameValueConstraint<ArgumentException> sut = new("it", grammar, null);
StringBuilder sb = new();

sut.AppendExpectation(sb, "");

await That(sb.ToString()).IsEqualTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,91 @@ but it was <null>
""");
}
}
public sealed class NegatedTests
{
[Theory]
[AutoData]
public async Task WhenExpectedIsNull_AndParamNameIsEmpty_ShouldFail(string message)
{
ArgumentException subject = new(message);

async Task Act()
=> await That(subject).DoesNotComplyWith(it =>
it.HasParamName(null));

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
does not have a ParamName,
but it had
""");
}

[Theory]
[AutoData]
public async Task WhenExpectedIsNull_ShouldFail(string message)
{
ArgumentException subject = new(message, nameof(message));

async Task Act()
=> await That(subject).DoesNotComplyWith(it =>
it.HasParamName(null));

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
does not have a ParamName,
but it had
""");
}

[Theory]
[AutoData]
public async Task WhenParamNameIsDifferent_ShouldSucceed(string message)
{
ArgumentException subject = new(message, nameof(message));

async Task Act()
=> await That(subject).DoesNotComplyWith(it =>
it.HasParamName("somethingElse"));

await That(Act).DoesNotThrow();
}

[Theory]
[AutoData]
public async Task WhenParamNameMatchesExpected_ShouldFail(string message)
{
ArgumentException subject = new(message, nameof(message));

async Task Act()
=> await That(subject).DoesNotComplyWith(it =>
it.HasParamName("message"));

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
does not have ParamName "message",
but it had
""");
}

[Fact]
public async Task WhenSubjectIsNull_ShouldFail()
{
ArgumentException? subject = null;

async Task Act()
=> await That(subject).DoesNotComplyWith(it =>
it.HasParamName("message"));

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
does not have ParamName "message",
but it was <null>
""");
}
}
}
}
Loading
Loading