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
9 changes: 3 additions & 6 deletions Source/aweXpect/That/ThatGeneric.IsEquatableTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ private sealed class IsEquatableToConstraint<T, TEquatable>(
string it,
ExpectationGrammars grammars,
T expected)
: ConstraintResult.WithValue<IEquatable<T>>(grammars),
: ConstraintResult.WithValue<TEquatable>(grammars),
IValueConstraint<TEquatable>
where TEquatable : IEquatable<T>
{
public ConstraintResult IsMetBy(TEquatable actual)
{
Actual = actual;
Outcome = actual.Equals(expected) ? Outcome.Success : Outcome.Failure;
Outcome = actual is not null && actual.Equals(expected) ? Outcome.Success : Outcome.Failure;
return this;
}

Expand All @@ -66,9 +66,6 @@ protected override void AppendNegatedExpectation(StringBuilder stringBuilder, st
}

protected override void AppendNegatedResult(StringBuilder stringBuilder, string? indentation = null)
{
stringBuilder.Append(it).Append(" was in ");
Formatter.Format(stringBuilder, Actual);
}
=> AppendNormalResult(stringBuilder, indentation);
}
}
40 changes: 38 additions & 2 deletions Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ but it was ThatGeneric.IsEquatableTo.Wrapper {
}
""");
}

[Fact]
public async Task WhenSubjectIsNull_ShouldFail()
{
NullableWrapper subject = null!;

async Task Act()
=> await That(subject).IsEquatableTo(1L);

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
is equatable to 1,
but it was <null>
""");
}
}

public sealed class NegatedTests
Expand All @@ -83,7 +99,7 @@ await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
is not equatable to 1,
but it was in ThatGeneric.IsEquatableTo.Wrapper {
but it was ThatGeneric.IsEquatableTo.Wrapper {
Value = 1
}
""");
Expand All @@ -104,7 +120,7 @@ Expected that subject
is not equatable to ThatGeneric.IsEquatableTo.Wrapper {
Value = 1
},
but it was in ThatGeneric.IsEquatableTo.Wrapper {
but it was ThatGeneric.IsEquatableTo.Wrapper {
Value = 1
}
""");
Expand Down Expand Up @@ -132,6 +148,26 @@ async Task Act()

await That(Act).DoesNotThrow();
}

[Fact]
public async Task WhenSubjectIsNull_ShouldSucceed()
{
NullableWrapper subject = null!;

async Task Act()
=> await That(subject).DoesNotComplyWith(it => it.IsEquatableTo(1L));

await That(Act).DoesNotThrow();
}
}

private sealed class NullableWrapper(long value) : IEquatable<long>
{
public long Value { get; } = value;

/// <inheritdoc cref="IEquatable{T}.Equals(T)" />
public bool Equals(long other)
=> Value == other;
}

private readonly struct Wrapper(long value)
Expand Down
40 changes: 38 additions & 2 deletions Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
is not equatable to 1,
but it was in ThatGeneric.IsNotEquatableTo.Wrapper {
but it was ThatGeneric.IsNotEquatableTo.Wrapper {
Value = 1
}
""");
Expand All @@ -39,7 +39,7 @@ Expected that subject
is not equatable to ThatGeneric.IsNotEquatableTo.Wrapper {
Value = 1
},
but it was in ThatGeneric.IsNotEquatableTo.Wrapper {
but it was ThatGeneric.IsNotEquatableTo.Wrapper {
Value = 1
}
""");
Expand Down Expand Up @@ -67,6 +67,17 @@ async Task Act()

await That(Act).DoesNotThrow();
}

[Fact]
public async Task WhenSubjectIsNull_ShouldSucceed()
{
NullableWrapper subject = null!;

async Task Act()
=> await That(subject).IsNotEquatableTo(1L);

await That(Act).DoesNotThrow();
}
}

public sealed class NegatedTests
Expand Down Expand Up @@ -132,6 +143,31 @@ but it was ThatGeneric.IsNotEquatableTo.Wrapper {
}
""");
}

[Fact]
public async Task WhenSubjectIsNull_ShouldFail()
{
NullableWrapper subject = null!;

async Task Act()
=> await That(subject).DoesNotComplyWith(it => it.IsNotEquatableTo(1L));

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
is equatable to 1,
but it was <null>
""");
}
}

private sealed class NullableWrapper(long value) : IEquatable<long>
{
public long Value { get; } = value;

/// <inheritdoc cref="IEquatable{T}.Equals(T)" />
public bool Equals(long other)
=> Value == other;
}

private readonly struct Wrapper(long value)
Expand Down
Loading