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
28 changes: 28 additions & 0 deletions TUnit.Assertions.Tests/ThrowInDelegateValueAssertionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using TUnit.Assertions.AssertConditions.Throws;

namespace TUnit.Assertions.Tests;

public class ThrowInDelegateValueAssertionTests
{
[Test]
public async Task ThrowInDelegateValueAssertion_ReturnsExpectedErrorMessage()
{
var assertion = async () => await Assert.That(() =>
{
throw new Exception("No");
return true;

Check warning on line 13 in TUnit.Assertions.Tests/ThrowInDelegateValueAssertionTests.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (windows-latest)

Unreachable code detected

Check warning on line 13 in TUnit.Assertions.Tests/ThrowInDelegateValueAssertionTests.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (macos-latest)

Unreachable code detected

Check warning on line 13 in TUnit.Assertions.Tests/ThrowInDelegateValueAssertionTests.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (ubuntu-latest)

Unreachable code detected
}).IsEqualTo(true);

await Assert.That(assertion)
.Throws<AssertionException>()
.WithMessageContaining("""
Expected () =>
{
throw new Exception("No");
return true;
} to be equal to True

but An exception was thrown during the assertion: System.Exception: No
""");
}
}
15 changes: 10 additions & 5 deletions TUnit.Assertions/AssertConditions/ExpectedValueAssertCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void WithTransform(Func<TActual?, TActual?> actualTransformation,
{
_transformations.Add((actualTransformation, expectedTransformation));
}

public void WithComparer(Func<TActual?, TExpected?, AssertionDecision> comparer)
{
_customComparers.Add(comparer);
Expand All @@ -26,13 +26,13 @@ AssertionMetadata assertionMetadata
)
{
var expected = ExpectedValue;

foreach (var (actualTransformation, expectedTransformation) in _transformations)
{
actualValue = actualTransformation(actualValue);
expected = expectedTransformation(expected);
}

foreach (var result in _customComparers.Select(customComparer => customComparer(actualValue, expected)))
{
switch (result)
Expand All @@ -44,8 +44,13 @@ AssertionMetadata assertionMetadata
}
}

if (exception is not null)
{
return FailWithMessage($"An exception was thrown during the assertion: {exception}");
}

return GetResult(actualValue, expected);
}

protected abstract ValueTask<AssertionResult> GetResult(TActual? actualValue, TExpected? expectedValue);
}
}
Loading