-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add AndWhoseResult for delegates DoesNotThrow and DoesNotThrowExactly
#660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vbreuss
merged 2 commits into
main
from
topic/add-andwhoseresult-for-delegate-doesnotthrow
Jun 26, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,84 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Text; | ||
| using aweXpect.Core; | ||
| using aweXpect.Core.Constraints; | ||
| using aweXpect.Core.Sources; | ||
| using aweXpect.Delegates; | ||
|
|
||
| namespace aweXpect.Results; | ||
|
|
||
| /// <summary> | ||
| /// Result for a delegate with a value that does not throw. | ||
| /// </summary> | ||
| public class DelegateWithValueResult<T>(ExpectationBuilder expectationBuilder, ThatDelegate.WithValue<T> returnValue) | ||
| : AndResult<T, ThatDelegate.WithValue<T>>(expectationBuilder, returnValue) | ||
| { | ||
| private readonly ExpectationBuilder _expectationBuilder = expectationBuilder; | ||
|
|
||
| /// <summary> | ||
| /// Returns the result returned from the delegate. | ||
| /// </summary> | ||
| public IThat<T> AndWhoseResult | ||
| { | ||
| get | ||
| { | ||
| _expectationBuilder.And("") | ||
| .AddConstraint((it, grammars) => new DoesNotThrowAnyExceptionConstraint(it, grammars)) | ||
| .ForWhich<DelegateValue<T>, T?>(d => d.Value, " and whose result ", "the result"); | ||
| return new ThatSubject<T?>(_expectationBuilder); | ||
| } | ||
| } | ||
|
|
||
| private sealed class DoesNotThrowAnyExceptionConstraint( | ||
| string it, | ||
| ExpectationGrammars grammars) | ||
| : ConstraintResult(grammars), | ||
| IValueConstraint<DelegateValue<T>> | ||
| { | ||
| private DelegateValue<T>? _actual; | ||
|
|
||
| /// <inheritdoc /> | ||
| public ConstraintResult IsMetBy(DelegateValue<T> value) | ||
| { | ||
| _actual = value; | ||
| if (value.IsNull) | ||
| { | ||
| Outcome = Outcome.Failure; | ||
| return this; | ||
| } | ||
|
|
||
| Outcome = value.Exception is null | ||
| ? Outcome.Success | ||
| : Outcome.Failure; | ||
| return this; | ||
| } | ||
|
|
||
| public override void AppendExpectation(StringBuilder stringBuilder, string? indentation = null) | ||
| { | ||
| // Do not append any expectation | ||
| } | ||
|
|
||
| public override void AppendResult(StringBuilder stringBuilder, string? indentation = null) | ||
| { | ||
| if (_actual?.Exception is not null) | ||
| { | ||
| stringBuilder.Append(it).Append(" did throw "); | ||
| stringBuilder.Append(ThatDelegate.FormatForMessage(_actual.Exception)); | ||
| } | ||
| } | ||
|
|
||
| public override bool TryGetValue<TValue>([NotNullWhen(true)] out TValue? value) where TValue : default | ||
| { | ||
| if (_actual is { Value: TValue typedValue, }) | ||
| { | ||
| value = typedValue; | ||
| return true; | ||
| } | ||
|
|
||
| value = default; | ||
| return typeof(TValue).IsAssignableFrom(typeof(T)); | ||
| } | ||
|
|
||
| public override ConstraintResult Negate() => this; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
180 changes: 180 additions & 0 deletions
180
Tests/aweXpect.Tests/Delegates/ThatDelegate.DoesNotThrow.AndWhoseResult.Tests.cs
This file contains hidden or 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,180 @@ | ||
| namespace aweXpect.Tests; | ||
|
|
||
| public sealed partial class ThatDelegate | ||
| { | ||
| public sealed partial class DoesNotThrow | ||
| { | ||
| public sealed class AndWhoseResult | ||
| { | ||
| public sealed class Tests | ||
| { | ||
| [Fact] | ||
| public async Task WhenDelegateThrows_ShouldFail() | ||
| { | ||
| Func<int> @delegate = () => throw new CustomException(); | ||
|
|
||
| async Task Act() => await That(@delegate).DoesNotThrow().AndWhoseResult.IsGreaterThan(5); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that @delegate | ||
| does not throw any exception and whose result is greater than 5, | ||
| but it did throw a ThatDelegate.CustomException: | ||
| WhenDelegateThrows_ShouldFail | ||
| """); | ||
| } | ||
|
|
||
| [Theory] | ||
| [AutoData] | ||
| public async Task WhenReturnValueDoesNotMatch_ShouldFail(int value) | ||
| { | ||
| Func<int> @delegate = () => value; | ||
|
|
||
| async Task Act() => await That(@delegate).DoesNotThrow().AndWhoseResult.IsLessThan(value); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage($""" | ||
| Expected that @delegate | ||
| does not throw any exception and whose result is less than {value}, | ||
| but the result was {value} | ||
| """); | ||
| } | ||
|
|
||
| [Theory] | ||
| [AutoData] | ||
| public async Task WhenReturnValueMatches_ShouldSucceed(int value) | ||
| { | ||
| Func<int> @delegate = () => value; | ||
|
|
||
| await That(@delegate).DoesNotThrow().AndWhoseResult.IsEqualTo(value); | ||
| } | ||
| } | ||
|
|
||
| public sealed class GenericTests | ||
| { | ||
| [Fact] | ||
| public async Task WhenDelegateThrowsExpectedException_ShouldFail() | ||
| { | ||
| Func<int> @delegate = () => throw new CustomException(); | ||
|
|
||
| async Task Act() | ||
| => await That(@delegate).DoesNotThrow<CustomException>().AndWhoseResult.IsEqualTo(5); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that @delegate | ||
| does not throw a ThatDelegate.CustomException and whose result is equal to 5, | ||
| but it did throw a ThatDelegate.CustomException: | ||
| WhenDelegateThrowsExpectedException_ShouldFail | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenDelegateThrowsOtherException_ShouldFail() | ||
| { | ||
| Func<int> @delegate = () => throw new CustomException(); | ||
|
|
||
| async Task Act() | ||
| => await That(@delegate).DoesNotThrow<OtherException>().AndWhoseResult.IsEqualTo(5); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that @delegate | ||
| does not throw a ThatDelegate.OtherException and whose result is equal to 5, | ||
| but it did throw a ThatDelegate.CustomException: | ||
| WhenDelegateThrowsOtherException_ShouldFail | ||
| """); | ||
| } | ||
|
|
||
| [Theory] | ||
| [AutoData] | ||
| public async Task WhenReturnValueDoesNotMatch_ShouldFail(int value) | ||
| { | ||
| Func<int> @delegate = () => value; | ||
|
|
||
| async Task Act() => await That(@delegate).DoesNotThrow<CustomException>().AndWhoseResult | ||
| .IsEqualTo(value + 1); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage($""" | ||
| Expected that @delegate | ||
| does not throw a ThatDelegate.CustomException and whose result is equal to {value + 1}, | ||
| but the result was {value} which differs by -1 | ||
| """); | ||
| } | ||
|
|
||
| [Theory] | ||
| [AutoData] | ||
| public async Task WhenReturnValueMatches_ShouldSucceed(int value) | ||
| { | ||
| Func<int> @delegate = () => value; | ||
|
|
||
| await That(@delegate).DoesNotThrow<CustomException>().AndWhoseResult.IsEqualTo(value); | ||
| } | ||
| } | ||
|
|
||
| public sealed class TypeTests | ||
| { | ||
| [Fact] | ||
| public async Task WhenDelegateThrowsExpectedException_ShouldFail() | ||
| { | ||
| Func<int> @delegate = () => throw new CustomException(); | ||
|
|
||
| async Task Act() | ||
| => await That(@delegate).DoesNotThrow(typeof(CustomException)).AndWhoseResult.IsEqualTo(5); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that @delegate | ||
| does not throw a ThatDelegate.CustomException and whose result is equal to 5, | ||
| but it did throw a ThatDelegate.CustomException: | ||
| WhenDelegateThrowsExpectedException_ShouldFail | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenDelegateThrowsOtherException_ShouldFail() | ||
| { | ||
| Func<int> @delegate = () => throw new CustomException(); | ||
|
|
||
| async Task Act() | ||
| => await That(@delegate).DoesNotThrow(typeof(OtherException)).AndWhoseResult.IsEqualTo(5); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that @delegate | ||
| does not throw a ThatDelegate.OtherException and whose result is equal to 5, | ||
| but it did throw a ThatDelegate.CustomException: | ||
| WhenDelegateThrowsOtherException_ShouldFail | ||
| """); | ||
| } | ||
|
|
||
| [Theory] | ||
| [AutoData] | ||
| public async Task WhenReturnValueDoesNotMatch_ShouldFail(int value) | ||
| { | ||
| Func<int> @delegate = () => value; | ||
|
|
||
| async Task Act() => await That(@delegate).DoesNotThrow(typeof(CustomException)).AndWhoseResult | ||
| .IsEqualTo(value + 1); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage($""" | ||
| Expected that @delegate | ||
| does not throw a ThatDelegate.CustomException and whose result is equal to {value + 1}, | ||
| but the result was {value} which differs by -1 | ||
| """); | ||
| } | ||
|
|
||
| [Theory] | ||
| [AutoData] | ||
| public async Task WhenReturnValueMatches_ShouldSucceed(int value) | ||
| { | ||
| Func<int> @delegate = () => value; | ||
|
|
||
| await That(@delegate).DoesNotThrow(typeof(CustomException)).AndWhoseResult.IsEqualTo(value); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.