-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ArgumentException (#1730)
- Loading branch information
Showing
4 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
TUnit.Assertions.Tests/Assertions/Delegates/Throws.WithParameterNameTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using TUnit.Assertions.AssertConditions.Throws; | ||
|
||
namespace TUnit.Assertions.Tests.Assertions.Delegates; | ||
|
||
public partial class Throws | ||
{ | ||
public class WithParameterName | ||
{ | ||
[Test] | ||
public async Task Fails_For_Different_Parameter_Name() | ||
{ | ||
var paramName1 = "foo"; | ||
var paramName2 = "bar"; | ||
var expectedMessage = """ | ||
Expected action to throw an ArgumentException which param name equals "bar" | ||
but it differs at index 0: | ||
↓ | ||
"foo" | ||
"bar" | ||
↑ | ||
at Assert.That(action).ThrowsExactly<ArgumentException>().WithParameterName(paramName2) | ||
"""; | ||
ArgumentException exception = new(string.Empty, paramName1); | ||
Action action = () => throw exception; | ||
|
||
var sut = async () | ||
=> await Assert.That(action).ThrowsExactly<ArgumentException>().WithParameterName(paramName2); | ||
|
||
await Assert.That(sut).ThrowsException() | ||
.WithMessage(expectedMessage); | ||
} | ||
|
||
[Test] | ||
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")] | ||
public async Task Returns_Exception_When_Awaited() | ||
{ | ||
var matchingParamName = "foo"; | ||
ArgumentException exception = new(string.Empty, matchingParamName); | ||
Action action = () => throw exception; | ||
|
||
var result = await Assert.That(action).Throws<ArgumentException>().WithParameterName(matchingParamName); | ||
|
||
await Assert.That(result).IsSameReferenceAs(exception); | ||
} | ||
|
||
[Test] | ||
public async Task Succeed_For_Matching_Parameter_Name() | ||
{ | ||
var matchingParamName = "foo"; | ||
ArgumentException exception = new(string.Empty, matchingParamName); | ||
Action action = () => throw exception; | ||
|
||
var sut = async () | ||
=> await Assert.That(action).Throws<ArgumentException>().WithParameterName(matchingParamName); | ||
|
||
await Assert.That(sut).ThrowsNothing(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
TUnit.Assertions/Assertions/Throws/ThrowsWithParamNameAssertCondition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using TUnit.Assertions.Extensions; | ||
using TUnit.Assertions.Helpers; | ||
|
||
namespace TUnit.Assertions.AssertConditions.Throws; | ||
|
||
public class ThrowsWithParamNameAssertCondition<TActual, TException>( | ||
string expectedParamName, | ||
StringComparison stringComparison, | ||
Func<Exception?, ArgumentException?> exceptionSelector) | ||
: DelegateAssertCondition<TActual, ArgumentException>() | ||
where TException : ArgumentException | ||
{ | ||
protected override string GetExpectation() | ||
=> $"to throw {typeof(TException).Name.PrependAOrAn()} which param name equals \"{expectedParamName.TruncateWithEllipsis(100)}\""; | ||
|
||
protected override Task<AssertionResult> GetResult(TActual? actualValue, Exception? exception) | ||
{ | ||
var actualException = exceptionSelector(exception); | ||
|
||
return AssertionResult | ||
.FailIf(actualException is null, | ||
"the exception is null") | ||
.OrFailIf(!string.Equals(actualException!.ParamName, expectedParamName, stringComparison), | ||
$"{new StringDifference(actualException!.ParamName, expectedParamName) | ||
.ToString("it differs at index")}"); | ||
} | ||
} |