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
15 changes: 13 additions & 2 deletions Source/aweXpect/That/Delegates/ThatDelegateThrows.WithParamName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@ public static partial class ThatDelegateThrows
/// <summary>
/// Verifies that the actual <see cref="ArgumentException" /> has an <paramref name="expected" /> param name.
/// </summary>
/// <remarks>
/// If <paramref name="expected" /> is <see langword="null" />, does not verify anything.
/// </remarks>
public static AndOrResult<TException, ThatDelegateThrows<TException>> WithParamName<TException>(
this ThatDelegateThrows<TException> source,
string expected)
string? expected)
where TException : ArgumentException?
=> new(source.ExpectationBuilder.AddConstraint((it, grammars)
{
if (expected == null)
{
return new AndOrResult<TException, ThatDelegateThrows<TException>>(source.ExpectationBuilder, source);
}

Comment thread
vbreuss marked this conversation as resolved.
return new AndOrResult<TException, ThatDelegateThrows<TException>>(
source.ExpectationBuilder.AddConstraint((it, grammars)
=> new ThatException.HasParamNameValueConstraint<TException>(
it,
grammars | ExpectationGrammars.Active | ExpectationGrammars.Nested,
expected)),
source);
}
}
13 changes: 8 additions & 5 deletions Source/aweXpect/That/Exceptions/ThatException.HasParamName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,29 @@ public static partial class ThatException
/// <summary>
/// Verifies that the actual <see cref="ArgumentException" /> has an <paramref name="expected" /> param name.
/// </summary>
/// <remarks>
/// If <paramref name="expected" /> is <see langword="null" />, does not verify anything.
/// </remarks>
public static AndOrResult<TException, IThat<TException>> HasParamName<TException>(
this IThat<TException> source,
string expected)
string? expected)
where TException : ArgumentException?
=> new(source.Get().ExpectationBuilder.AddConstraint((it, grammars) =>
new HasParamNameValueConstraint<TException>(it, grammars, expected)),
=> new(source.Get().ExpectationBuilder.AddConstraint((it, grammars)
=> new HasParamNameValueConstraint<TException>(it, grammars, expected)),
source);

internal class HasParamNameValueConstraint<TArgumentException>(
string it,
ExpectationGrammars grammars,
string expected)
string? expected)
: ConstraintResult.WithNotNullValue<Exception?>(it, grammars),
IValueConstraint<Exception?>
where TArgumentException : ArgumentException?
{
public ConstraintResult IsMetBy(Exception? actual)
{
Actual = actual;
Outcome = actual is TArgumentException argumentException && argumentException.ParamName == expected
Outcome = actual is TArgumentException argumentException && (expected is null || argumentException.ParamName == expected)
? Outcome.Success
: Outcome.Failure;
return this;
Expand Down
4 changes: 2 additions & 2 deletions Tests/aweXpect.Api.Tests/Expected/aweXpect_net8.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ namespace aweXpect
where TException : System.Exception? { }
public static aweXpect.Results.StringEqualityTypeResult<TException, aweXpect.Delegates.ThatDelegateThrows<TException>> WithMessage<TException>(this aweXpect.Delegates.ThatDelegateThrows<TException> source, string expected)
where TException : System.Exception? { }
public static aweXpect.Results.AndOrResult<TException, aweXpect.Delegates.ThatDelegateThrows<TException>> WithParamName<TException>(this aweXpect.Delegates.ThatDelegateThrows<TException> source, string expected)
public static aweXpect.Results.AndOrResult<TException, aweXpect.Delegates.ThatDelegateThrows<TException>> WithParamName<TException>(this aweXpect.Delegates.ThatDelegateThrows<TException> source, string? expected)
where TException : System.ArgumentException? { }
public static aweXpect.Results.AndOrResult<TException?, aweXpect.Delegates.ThatDelegateThrows<TException>> WithRecursiveInnerExceptions<TException>(this aweXpect.Delegates.ThatDelegateThrows<TException> source, System.Action<aweXpect.Core.IThat<System.Collections.Generic.IEnumerable<System.Exception>>> expectations)
where TException : System.Exception? { }
Expand Down Expand Up @@ -669,7 +669,7 @@ namespace aweXpect
public static aweXpect.Results.AndOrResult<System.Exception?, aweXpect.Core.IThat<System.Exception?>> HasInnerException(this aweXpect.Core.IThat<System.Exception?> source) { }
public static aweXpect.Results.AndOrResult<System.Exception?, aweXpect.Core.IThat<System.Exception?>> HasInnerException(this aweXpect.Core.IThat<System.Exception?> source, System.Action<aweXpect.Core.IThat<System.Exception?>> expectations) { }
public static aweXpect.Results.StringEqualityTypeResult<System.Exception?, aweXpect.Core.IThat<System.Exception?>> HasMessage(this aweXpect.Core.IThat<System.Exception?> source, string expected) { }
public static aweXpect.Results.AndOrResult<TException, aweXpect.Core.IThat<TException>> HasParamName<TException>(this aweXpect.Core.IThat<TException> source, string expected)
public static aweXpect.Results.AndOrResult<TException, aweXpect.Core.IThat<TException>> HasParamName<TException>(this aweXpect.Core.IThat<TException> source, string? expected)
where TException : System.ArgumentException? { }
public static aweXpect.Results.AndOrResult<System.Exception?, aweXpect.Core.IThat<System.Exception?>> HasRecursiveInnerExceptions(this aweXpect.Core.IThat<System.Exception?> source, System.Action<aweXpect.Core.IThat<System.Collections.Generic.IEnumerable<System.Exception>>> expectations) { }
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/aweXpect.Api.Tests/Expected/aweXpect_netstandard2.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace aweXpect
where TException : System.Exception? { }
public static aweXpect.Results.StringEqualityTypeResult<TException, aweXpect.Delegates.ThatDelegateThrows<TException>> WithMessage<TException>(this aweXpect.Delegates.ThatDelegateThrows<TException> source, string expected)
where TException : System.Exception? { }
public static aweXpect.Results.AndOrResult<TException, aweXpect.Delegates.ThatDelegateThrows<TException>> WithParamName<TException>(this aweXpect.Delegates.ThatDelegateThrows<TException> source, string expected)
public static aweXpect.Results.AndOrResult<TException, aweXpect.Delegates.ThatDelegateThrows<TException>> WithParamName<TException>(this aweXpect.Delegates.ThatDelegateThrows<TException> source, string? expected)
where TException : System.ArgumentException? { }
public static aweXpect.Results.AndOrResult<TException?, aweXpect.Delegates.ThatDelegateThrows<TException>> WithRecursiveInnerExceptions<TException>(this aweXpect.Delegates.ThatDelegateThrows<TException> source, System.Action<aweXpect.Core.IThat<System.Collections.Generic.IEnumerable<System.Exception>>> expectations)
where TException : System.Exception? { }
Expand Down Expand Up @@ -454,7 +454,7 @@ namespace aweXpect
public static aweXpect.Results.AndOrResult<System.Exception?, aweXpect.Core.IThat<System.Exception?>> HasInnerException(this aweXpect.Core.IThat<System.Exception?> source) { }
public static aweXpect.Results.AndOrResult<System.Exception?, aweXpect.Core.IThat<System.Exception?>> HasInnerException(this aweXpect.Core.IThat<System.Exception?> source, System.Action<aweXpect.Core.IThat<System.Exception?>> expectations) { }
public static aweXpect.Results.StringEqualityTypeResult<System.Exception?, aweXpect.Core.IThat<System.Exception?>> HasMessage(this aweXpect.Core.IThat<System.Exception?> source, string expected) { }
public static aweXpect.Results.AndOrResult<TException, aweXpect.Core.IThat<TException>> HasParamName<TException>(this aweXpect.Core.IThat<TException> source, string expected)
public static aweXpect.Results.AndOrResult<TException, aweXpect.Core.IThat<TException>> HasParamName<TException>(this aweXpect.Core.IThat<TException> source, string? expected)
where TException : System.ArgumentException? { }
public static aweXpect.Results.AndOrResult<System.Exception?, aweXpect.Core.IThat<System.Exception?>> HasRecursiveInnerExceptions(this aweXpect.Core.IThat<System.Exception?> source, System.Action<aweXpect.Core.IThat<System.Collections.Generic.IEnumerable<System.Exception>>> expectations) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ public sealed partial class Throws
{
public class WithParamNameTests
{
[Theory]
[AutoData]
public async Task WhenExpectedIsNull_AndParamNameIsEmpty_ShouldSucceed(string message)
{
ArgumentException exception = new(message);
void Delegate() => throw exception;

async Task Act()
=> await That(Delegate).Throws<ArgumentException>()
.WithParamName(null);

await That(Act).DoesNotThrow();
}

[Theory]
[AutoData]
public async Task WhenExpectedIsNull_ShouldSucceed(string message)
{
ArgumentException exception = new(message, nameof(message));
void Delegate() => throw exception;

async Task Act()
=> await That(Delegate).Throws<ArgumentException>()
.WithParamName(null);

await That(Act).DoesNotThrow();
}

[Theory]
[AutoData]
public async Task WhenParamNameIsDifferent_ShouldFail(string message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ public class HasParamName
{
public sealed class Tests
{
[Theory]
[AutoData]
public async Task WhenExpectedIsNull_AndParamNameIsEmpty_ShouldSucceed(string message)
{
ArgumentException subject = new(message);

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

await That(Act).DoesNotThrow();
}

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

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

await That(Act).DoesNotThrow();
}

[Theory]
[AutoData]
public async Task WhenParamNameIsDifferent_ShouldFail(string message)
Expand Down
Loading