Repository navigation
feat: recognise expectations that guarantee a not-null subject by attribute - #1050
Merged
Merged
Conversation
…ribute
Mark the 646 expectations in `aweXpect` that a `null` subject cannot satisfy
with `[GuaranteesNotNull]` and let `IsNotNullSuppressor` read the attribute
instead of a hardcoded list of three method names.
Matching by name could not distinguish overloads. The attribute can, which is
what the three partial cases need: `IsNotEmpty`, `DoesNotStartWith` and
`DoesNotEndWith` are marked on their collection overloads but not on their
`string?` ones, where a `null` subject fulfils them.
The analyzer no longer requires the receiver to be `IThat<TSubject>`, because
`aweXpect.Core` declares expectations such as `Throws` or `IsExactly` as
instance members of other types. Both the attribute and the marked method must
still come from a referenced aweXpect assembly, so a look-alike attribute in
user code cannot trigger a suppression.
The source generator emits the attribute for the nullable expectations it
derives from `ConstraintResult.WithNotNullValue`. `Is{Not}NullOrEmpty` on a
`Guid?` builds on `WithValue` instead, where only the negated expectation fails
for `null`; `NegatedFailsOnNull` expresses that.
Not marked, because a `null` subject satisfies them for at least one accepted
argument:
- `ThatString.StartsWith` / `EndsWith`, which are fulfilled by `null!`.
- The `DoesNotContain` overloads taking a sequence, which are fulfilled by an
empty one.
`GuaranteesNotNullTests` discovers every marked expectation in both assemblies
by reflection and invokes it against a `null` subject. The expectations whose
call shape it cannot construct faithfully are listed in `ExcludedFromInvocation`
and checked to have a hand-written null-subject test instead, so neither list
can grow silently.
vbreuss
force-pushed
the
feat/guarantees-not-null-expectations
branch
from
September 12, 2026 17:36
d18c724 to
4bb9ec2
Compare
Contributor
Test Results 23 files - 27 23 suites - 27 9m 20s ⏱️ - 1m 39s Results for commit 7b72158. ± Comparison against base commit ae28ac8. This pull request removes 3233 and adds 3498 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
A review of the previous commit found that both self-policing claims of `GuaranteesNotNullTests` were weaker than advertised. `GetCoveringTests` stripped the declaring type before matching, so any nested test class with a matching expectation name counted as coverage — `ThatString`'s `Contains` tests "covered" `ThatEnumerable.Contains`, and 22 unrelated classes including the dictionary ones "covered" `ThatEnumerable.AreAllUnique`. It now matches on the declaring type as well. The check remains per expectation name rather than per overload, which is the most a mechanical check can assert, and the comment no longer claims otherwise. An expectation whose subject cannot be null was dropped before the exclusion check, so it was neither invoked, excluded nor reported. That is correct for a genuinely non-nullable value type, but a generic subject that merely resolved to one of this test's own helper structs would have vanished just as quietly. `SkippedExpectations_ShouldHaveASubjectThatCannotBeNull` now rejects exactly that. Verified by making an unconstrained subject resolve to `EnumerableStruct`. `ExcludedExpectations_ShouldBeCovered` moves behind `NET8_0_OR_GREATER` as a whole: both of its assertions describe this repository rather than runtime behaviour, and the `IAsyncEnumerable` expectations and their tests are absent from the older target frameworks. Also from the review: the analyzer checks the assembly before decoding attributes, since the receiver restriction that used to narrow the candidates is gone; the four new null-subject tests assert their message like their neighbours, so they cannot pass for the wrong reason; and a continuation argument no longer defaults to a minute.
vbreuss
enabled auto-merge (squash)
September 12, 2026 18:22
|
github-actions Bot
added a commit
that referenced
this pull request
Sep 12, 2026
…ntee a not-null subject by attribute (#1050) by Valentin Breuß
github-actions Bot
added a commit
that referenced
this pull request
Sep 12, 2026
…ntee a not-null subject by attribute (#1050) by Valentin Breuß
Contributor
🚀 Benchmark ResultsDetails
|
Contributor
👽 Mutation ResultsaweXpectDetails
The final mutation score is 90.72%Coverage Thresholds: high:80 low:60 break:0aweXpect.CoreDetails
The final mutation score is NaN%Coverage Thresholds: high:80 low:60 break:0 |
This was referenced Oct 12, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Mark the expectations in
aweXpectthat anullsubject cannot satisfy with[GuaranteesNotNull]and letIsNotNullSuppressorread the attribute instead of a hardcoded list of three method names.Matching by name could not distinguish overloads. The attribute can, which is what the three partial cases need:
IsNotEmpty,DoesNotStartWithandDoesNotEndWithare marked on their collection overloads but not on theirstring?ones, where anullsubject fulfils them.The analyzer no longer requires the receiver to be
IThat<TSubject>, becauseaweXpect.Coredeclares expectations such asThrowsorIsExactlyas instance members of other types. Both the attribute and the marked method must still come from a referenced aweXpect assembly, so a look-alike attribute in user code cannot trigger a suppression.The source generator emits the attribute for the nullable expectations it derives from
ConstraintResult.WithNotNullValue.Is{Not}NullOrEmptyon aGuid?builds onWithValueinstead, where only the negated expectation fails fornull;NegatedFailsOnNullexpresses that.Not marked, because a
nullsubject satisfies them for at least one accepted argument:ThatString.StartsWith/EndsWith, which are fulfilled bynull!.DoesNotContainoverloads taking a sequence, which are fulfilled by an empty one.GuaranteesNotNullTestsdiscovers every marked expectation in both assemblies by reflection and invokes it against anullsubject. The expectations whose call shape it cannot construct faithfully are listed inExcludedFromInvocationand checked to have a hand-written null-subject test instead, so neither list can grow silently.