-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: support direct check for boolean is true
#797
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
Merged
Changes from all commits
Commits
Show all changes
5 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using aweXpect.Core.Constraints; | ||
| using aweXpect.Core.Nodes; | ||
| using aweXpect.Core.TimeSystem; | ||
| using aweXpect.Results; | ||
|
|
||
| namespace aweXpect.Core; | ||
|
|
||
| /// <summary> | ||
| /// Wraps the <see cref="ExpectationBuilder" /> for a bool. | ||
| /// </summary> | ||
| [DebuggerDisplay("ThatBool: {ExpectationBuilder}")] | ||
| public class ThatBool : ExpectationResult<bool>, IExpectThat<bool> | ||
| { | ||
| /// <inheritdoc cref="ThatBool" /> | ||
| public ThatBool(ExpectationBuilder expectationBuilder) : this( | ||
| new WithDefaultExpectationBuilderProxy(expectationBuilder)) | ||
| { | ||
| } | ||
|
|
||
| private ThatBool(WithDefaultExpectationBuilderProxy expectationBuilder) : base(expectationBuilder) | ||
| { | ||
| ExpectationBuilder = expectationBuilder; | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IExpectThat{T}.ExpectationBuilder" /> | ||
| public ExpectationBuilder ExpectationBuilder { get; } | ||
|
|
||
| private sealed class WithDefaultExpectationBuilderProxy(ExpectationBuilder inner) | ||
| : ExpectationBuilder(inner.Subject, inner.ExpectationGrammars) | ||
| { | ||
| public override ExpectationBuilder UpdateContexts(Action<ResultContexts> callback) | ||
| => inner.UpdateContexts(callback); | ||
|
|
||
| internal override Task<ConstraintResult> IsMet(Node rootNode, EvaluationContext.EvaluationContext context, | ||
| ITimeSystem timeSystem, TimeSpan? timeout, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| if (rootNode is ExpectationNode expectationNode && expectationNode.IsEmpty()) | ||
| { | ||
| rootNode.AddConstraint(new IsTrueConstraint(inner.ExpectationGrammars)); | ||
| } | ||
|
|
||
| return inner.IsMet(rootNode, context, timeSystem, timeout, cancellationToken); | ||
| } | ||
|
|
||
| private sealed class IsTrueConstraint(ExpectationGrammars grammars) | ||
| : ConstraintResult.WithEqualToValue<bool>("it", grammars, false), | ||
| IValueConstraint<bool> | ||
| { | ||
| public ConstraintResult IsMetBy(bool actual) | ||
| { | ||
| Actual = actual; | ||
| Outcome = true.Equals(actual) ? Outcome.Success : Outcome.Failure; | ||
| return this; | ||
| } | ||
|
|
||
| protected override void AppendNormalExpectation(StringBuilder stringBuilder, string? indentation = null) | ||
| { | ||
| stringBuilder.Append("is "); | ||
| Formatter.Format(stringBuilder, true, FormattingOptions.Indented(indentation)); | ||
| } | ||
|
|
||
| protected override void AppendNormalResult(StringBuilder stringBuilder, string? indentation = null) | ||
| { | ||
| stringBuilder.Append(It); | ||
| stringBuilder.Append(" was "); | ||
| Formatter.Format(stringBuilder, Actual, FormattingOptions.Indented(indentation)); | ||
| } | ||
|
|
||
| protected override void AppendNegatedExpectation(StringBuilder stringBuilder, string? indentation = null) | ||
| { | ||
| stringBuilder.Append("is not "); | ||
| Formatter.Format(stringBuilder, true, FormattingOptions.Indented(indentation)); | ||
| } | ||
|
|
||
| protected override void AppendNegatedResult(StringBuilder stringBuilder, string? indentation = null) | ||
| { | ||
| stringBuilder.Append(It); | ||
| stringBuilder.Append(" was"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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 |
|---|---|---|
| @@ -1,4 +1,53 @@ | ||
| namespace aweXpect.Tests; | ||
|
|
||
| // ReSharper disable once ClassNeverInstantiated.Global | ||
| public sealed partial class ThatBool; | ||
| public sealed partial class ThatBool | ||
| { | ||
| /* TODO: Enable after next Core update | ||
| public sealed class Tests | ||
| { | ||
| [Fact] | ||
| public async Task WhenFalse_ShouldFail() | ||
| { | ||
| bool subject = false; | ||
|
|
||
| async Task Act() | ||
| => await That(subject); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that subject | ||
| is True, | ||
| but it was False | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenFalse_ShouldFailWithDescriptiveMessage() | ||
| { | ||
| bool subject = false; | ||
|
|
||
| async Task Act() | ||
| => await That(subject).Because("we want to test the failure"); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that subject | ||
| is True, because we want to test the failure, | ||
| but it was False | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenTrue_ShouldSucceed() | ||
| { | ||
| bool subject = true; | ||
|
|
||
| async Task Act() | ||
| => await That(subject); | ||
|
|
||
| await That(Act).DoesNotThrow(); | ||
| } | ||
| } | ||
| */ | ||
| } |
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.