Repository navigation
feat: generate expectations from a predicate with [GenerateExpectation] - #1830
Conversation
…on]`
A custom expectation needs an extension method, a constraint class and four texts, even when it only checks a condition. A source generator in `aweXpect.Core` writes them from a marked method, so extension packages can use it as well as test projects.
- `[GenerateExpectation]` on a static method that returns `bool`, `Task<bool>` or `ValueTask<bool>` generates the expectation and, with `{Not}` in its name, the negated one
- the subject parameter decides how `null` is handled: value types also get an overload on their nullable type, and `[NotNullWhen]` and `[DisallowNull]` refine nullable subjects
- an optional `Result` method describes the subject after "but"
- unsupported declarations are reported as errors `aweXpect4001` to `aweXpect4003`
…tion]`
Inserting "not" doesn't read well for every expectation, e.g. `HasTitle` whose negation is `DoesNotHaveTitle` with "does not have title …".
- `NegatedName` names the negated expectation instead of `{Not}` in the name
- `NegatedExpectationText` replaces the negated text, so `{not}` is no longer required when it is given
a03313d to
e5f3b8b
Compare
Test Results 91 files ± 0 91 suites ±0 2h 8m 48s ⏱️ - 11m 21s Results for commit 4eda2e2. ± Comparison against base commit 8dd735e. This pull request removes 4956 and adds 4966 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
🚀 Benchmark ResultsDetails
|
Split `GetExpectation` and `AppendExpectation` into smaller methods to reduce their cognitive complexity, remove the unused `model` parameter of `AppendTextMethod` and select the parameter names in the null-check loop.
Move the checks of the expectation texts into `GetTextProblem`, which reports the problem like `GetNameProblem`.
…Expectation]` A result method that ends with a `string` and an `ExpectationGrammars` parameter receives how the subject is referred to and the grammars, and describes the whole subject, e.g. with the verb agreeing with "they". Both are matched by their types and only together, so they cannot be confused with the other parameters of the expectation.
…ocal classes and pass the token to sync predicates - The generated extension methods are `internal` when the class, the subject or a parameter type (including type arguments, array elements and containing types) is only internal, so a public class no longer exposes an internal type; a type that is not even internal reports `aweXpect4002`. - A `file`-local class reports `aweXpect4001`, because the generated part would be a different type. - A synchronous predicate may now end with a `CancellationToken` as well, which receives the token of the evaluation instead of becoming a parameter of the expectation.
- Move the page to the first entry of the section (`01-generated-expectations.md`). - Open "Your first expectation" with the generated `IsRadioFriendly`, before the hand-written walk-through, and list the page first in the overview table.
|
…icate with `[GenerateExpectation]` (#1830) by Valentin Breuß
…icate with `[GenerateExpectation]` (#1830) by Valentin Breuß
|
This is addressed in release v3.2.0. |



A custom expectation needs an extension method, a constraint class and four texts, even when it only checks a condition. A source generator in
aweXpect.Corewrites them from a marked method, so extension packages can use it as well as test projects.[GenerateExpectation]on a static method that returnsbool,Task<bool>orValueTask<bool>generates the expectation and, with{Not}in its name, the negated onenullis handled: value types also get an overload on their nullable type, and[NotNullWhen]and[DisallowNull]refine nullable subjectsResultmethod describes the subject after "but"aweXpect4001toaweXpect4003