Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -47,7 +47,7 @@ public string GetExtendedFailure(string it, string? actual, string? expected,
IEqualityComparer<string> comparer,
StringDifferenceSettings? settings)
{
if (actual == null || expected == null)
if (string.IsNullOrEmpty(actual) || expected == null)
{
return $"{it} was {Formatter.Format(actual)}";
}
Expand Down Expand Up @@ -89,8 +89,8 @@ public ValueTask<bool>
#else
public Task<bool>
#endif
AreConsideredEqual(string? actual, string? expected, bool ignoreCase,
IEqualityComparer<string> comparer)
AreConsideredEqual(string? actual, string? expected, bool ignoreCase,
IEqualityComparer<string> comparer)
{
if (actual is null && expected is null)
{
Expand All @@ -113,7 +113,8 @@ public Task<bool>
#if NET8_0_OR_GREATER
return ValueTask.FromResult(actual.Length >= expected.Length && comparer.Equals(actual[..expected.Length], expected));
#else
return Task.FromResult(actual.Length >= expected.Length && comparer.Equals(actual[..expected.Length], expected));
return Task.FromResult(actual.Length >= expected.Length &&
comparer.Equals(actual[..expected.Length], expected));
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public string GetExtendedFailure(string it, string? actual, string? expected,
IEqualityComparer<string> comparer,
StringDifferenceSettings? settings)
{
if (actual == null || expected == null)
if (string.IsNullOrEmpty(actual) || expected == null)
{
return $"{it} was {Formatter.Format(actual)}";
}
Expand Down Expand Up @@ -96,8 +96,8 @@ public ValueTask<bool>
#else
public Task<bool>
#endif
AreConsideredEqual(string? actual, string? expected, bool ignoreCase,
IEqualityComparer<string> comparer)
AreConsideredEqual(string? actual, string? expected, bool ignoreCase,
IEqualityComparer<string> comparer)
{
if (actual is null && expected is null)
{
Expand All @@ -120,7 +120,8 @@ public Task<bool>
#if NET8_0_OR_GREATER
return ValueTask.FromResult(actual.Length >= expected.Length && comparer.Equals(actual[^expected.Length..], expected));
#else
return Task.FromResult(actual.Length >= expected.Length && comparer.Equals(actual[^expected.Length..], expected));
return Task.FromResult(actual.Length >= expected.Length &&
comparer.Equals(actual[^expected.Length..], expected));
#endif
}

Expand Down
7 changes: 5 additions & 2 deletions Source/aweXpect/That/Strings/ThatString.IsEqualTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ public async Task<ConstraintResult> IsMetBy(string? actual, CancellationToken ca
{
Actual = actual;
Outcome = await options.AreConsideredEqual(actual, expected) ? Outcome.Success : Outcome.Failure;
expectationBuilder.UpdateContexts(contexts => contexts
.Add(new ResultContext("Actual", actual)));
if (!string.IsNullOrWhiteSpace(actual))
{
expectationBuilder.UpdateContexts(contexts => contexts
.Add(new ResultContext("Actual", actual)));
}
Comment thread
vbreuss marked this conversation as resolved.
Outdated
return this;
}

Expand Down
25 changes: 21 additions & 4 deletions Tests/aweXpect.Tests/Strings/ThatString.EndsWith.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Expected that subject
"some arbitrary text"
"Text"
↑ (expected suffix)

Actual:
some arbitrary text
""");
Expand All @@ -54,7 +54,7 @@ Expected that subject
"some arbitrary text"
"SOME"
↑ (expected suffix)

Actual:
some arbitrary text
""");
Expand Down Expand Up @@ -100,6 +100,23 @@ async Task Act()
await That(Act).DoesNotThrow();
}

[Fact]
public async Task WhenActualIsEmpty_ShouldFail()
{
string subject = "";
string expected = "SOME";

async Task Act()
=> await That(subject).EndsWith(expected);

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
ends with "SOME",
but it was ""
""");
}

[Fact]
public async Task WhenExpectedIsNull_ShouldFail()
{
Expand Down Expand Up @@ -138,7 +155,7 @@ Expected that subject
"some arbitrary text"
"some"
↑ (expected suffix)

Actual:
some arbitrary text
""");
Expand Down Expand Up @@ -200,7 +217,7 @@ Expected that subject
ends with "more than text",
but it was "text" which is shorter than the expected length of 14 and misses the prefix:
"more than "

Actual:
text
""");
Expand Down
17 changes: 17 additions & 0 deletions Tests/aweXpect.Tests/Strings/ThatString.StartsWith.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ async Task Act()
await That(Act).DoesNotThrow();
}

[Fact]
public async Task WhenActualIsEmpty_ShouldFail()
{
string subject = "";
string expected = "SOME";

async Task Act()
=> await That(subject).StartsWith(expected);

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
starts with "SOME",
but it was ""
""");
}

[Fact]
public async Task WhenExpectedIsNull_ShouldFail()
{
Expand Down
Loading