Skip to content

feat: generate expectations from a predicate with [GenerateExpectation] - #1830

Merged
vbreuss merged 9 commits into
mainfrom
feat/generate-expectation
Oct 15, 2026
Merged

vbreuss merged 9 commits into
mainfrom
feat/generate-expectation

Conversation

@vbreuss

@vbreuss vbreuss commented Oct 15, 2026

Copy link
Copy Markdown
Member

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

…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`
@vbreuss vbreuss self-assigned this Oct 15, 2026
…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
@vbreuss
vbreuss force-pushed the feat/generate-expectation branch from a03313d to e5f3b8b Compare October 15, 2026 03:51
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

     91 files  ±  0       91 suites  ±0   2h 8m 48s ⏱️ - 11m 21s
 37 646 tests + 83   37 646 ✅ + 83  0 💤 ±0  0 ❌ ±0 
218 546 runs  +350  218 546 ✅ +350  0 💤 ±0  0 ❌ ±0 

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.

	
	    get;
	"a": 1
	await
	{
	}, 1)


, 2)

b, 2, False)
…
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(077ae421-7e32-420d-a68c-9dd33e77a385)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(13fa5854-39f4-49e0-aace-a1ddfe753e41)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(56f967f9-d416-4dbb-ac71-753de47adb60)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(6666d6e6-3d19-4554-8343-6a6945c2ec29)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(b7fc3c16-8b55-4d93-968a-78b197ab9c46)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(cbad08d7-f474-45f4-8f1d-a3fc09bbe3ca)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(fca853ea-b0fb-4668-ab75-2d44548602d8)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(1ade6a1f-ef0a-4702-a8ea-307706ef6802)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(34997ce0-6cf4-465d-a00e-9d107f77eb3c)
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(3671223c-b774-4886-9b4b-fe78759cf6b3)
…

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.5 LTS (Noble Numbat)
Intel Xeon 6973P-C 4.10GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.303
[Host] : .NET 8.0.32 (8.0.32, 8.0.3226.50405), X64 RyuJIT x86-64-v4

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Method Mean vs aweXpect Error StdDev Gen0 Gen1 Allocated vs aweXpect
Contains_aweXpect 268.7 ns 5.24 ns 4.37 ns 0.0086 - 720 B
Contains_FluentAssertions 336.2 ns +25% 17.59 ns 16.45 ns 0.0191 - 1608 B +123%
Contains_TUnit 302.4 ns +13% 12.14 ns 11.35 ns 0.0100 - 848 B +18%
Equivalency_aweXpect 6,713.9 ns 234.70 ns 219.54 ns 0.0305 - 3040 B
Equivalency_FluentAssertions 228,613.3 ns +3305% 16,062.88 ns 14,239.32 ns 6.8359 - 590226 B +19315%
Equivalency_TUnit 13,160.1 ns +96% 152.93 ns 119.40 ns 0.1831 - 15680 B +416%
EventTriggered_aweXpect 560.7 ns 6.83 ns 6.06 ns 0.0219 - 1848 B
EventTriggered_FluentAssertions 33,395.3 ns +5856% 834.46 ns 696.82 ns 0.0610 0.0305 5253 B +184%
Int_GreaterThan_aweXpect 127.8 ns 2.83 ns 2.51 ns 0.0060 - 512 B
Int_GreaterThan_FluentAssertions 244.9 ns +92% 10.94 ns 10.23 ns 0.0143 - 1224 B +139%
Int_GreaterThan_TUnit 188.6 ns +48% 5.81 ns 5.15 ns 0.0091 - 776 B +52%
ItemsCount_AtLeast_aweXpect 237.3 ns 9.06 ns 8.47 ns 0.0072 - 600 B
ItemsCount_AtLeast_FluentAssertions 506.5 ns +113% 29.37 ns 27.47 ns 0.0238 - 2008 B +235%
ItemsCount_AtLeast_TUnit 317.9 ns +34% 4.13 ns 3.66 ns 0.0124 - 1064 B +77%
Multiple_aweXpect 596.1 ns 26.41 ns 23.41 ns 0.0200 - 1688 B
Multiple_FluentAssertions 1,742.2 ns +192% 36.45 ns 30.44 ns 0.0801 - 6744 B +300%
Multiple_TUnit 702.9 ns +18% 16.61 ns 14.73 ns 0.0267 - 2240 B +33%
Satisfies_aweXpect 154.9 ns 3.05 ns 2.54 ns 0.0064 - 552 B
Satisfies_FluentAssertions 14,562.7 ns +9299% 331.73 ns 294.07 ns 0.0610 0.0458 5614 B +917%
Satisfies_TUnit 189.8 ns +23% 5.44 ns 5.09 ns 0.0083 - 712 B +29%
String_aweXpect 204.4 ns 4.68 ns 4.38 ns 0.0067 - 576 B
String_FluentAssertions 530.3 ns +159% 20.51 ns 18.18 ns 0.0267 - 2304 B +300%
String_TUnit 283.8 ns +39% 5.51 ns 4.89 ns 0.0105 - 912 B +58%
StringArray_aweXpect 460.9 ns 15.43 ns 14.43 ns 0.0100 - 864 B
StringArray_FluentAssertions 1,224.6 ns +166% 9.57 ns 7.99 ns 0.0496 - 4152 B +381%
StringArray_TUnit 552.7 ns +20% 7.34 ns 6.13 ns 0.0143 - 1264 B +46%
StringArrayInAnyOrder_aweXpect 460.5 ns 8.62 ns 6.73 ns 0.0105 - 912 B
StringArrayInAnyOrder_FluentAssertions 14,009.4 ns +2942% 214.93 ns 179.47 ns 0.3967 - 33465 B +3569%
StringArrayInAnyOrder_TUnit 647.6 ns +41% 9.99 ns 9.35 ns 0.0162 - 1408 B +54%
Throws_aweXpect 7,517.0 ns 279.32 ns 247.61 ns 0.0153 - 1720 B
Throws_FluentAssertions 24,120.2 ns +221% 614.65 ns 544.87 ns 0.2136 - 18608 B +982%
Throws_TUnit 7,340.2 ns -2% 83.31 ns 65.04 ns 0.0153 - 1824 B +6%
ThrowsAsync_aweXpect 7,169.2 ns 49.42 ns 41.27 ns 0.0229 - 2208 B
ThrowsAsync_FluentAssertions 34,127.6 ns +376% 204.41 ns 181.20 ns 0.2441 - 23553 B +967%
ThrowsAsync_TUnit 14,209.7 ns +98% 320.77 ns 267.86 ns 0.0305 - 2920 B +32%

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.
@sonarqubecloud

Copy link
Copy Markdown

@vbreuss
vbreuss merged commit 126a8c8 into main Oct 15, 2026
16 checks passed
@vbreuss
vbreuss deleted the feat/generate-expectation branch October 15, 2026 13:05
github-actions Bot added a commit that referenced this pull request Oct 15, 2026
…icate with `[GenerateExpectation]` (#1830) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Oct 15, 2026
…icate with `[GenerateExpectation]` (#1830) by Valentin Breuß
@github-actions

Copy link
Copy Markdown
Contributor

This is addressed in release v3.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state: released The issue is released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant