fix!: reject an empty prefix or suffix expectation - #1258
Merged
Merged
Conversation
`StartsWith("")` and `EndsWith("")` throw because an empty expectation is almost always a mistake - an accidentally empty variable - but the same expectation written as `IsEqualTo("").AsPrefix()` or `.AsSuffix()` silently passed. One expectation had two spellings that disagreed, and the silent one is the dangerous one, so the guard now also applies to the match type, wherever it is reached: `IsEqualTo`, `Contains`, `HasItem` and the string collection overloads.
Just like a regex or wildcard pattern, a prefix is only known when the expectation is verified, so the guard sits next to them in `StringEqualityOptions` and fires after the normalization: a prefix that only becomes empty through `IgnoringIndentation()` says just as little as a literal empty one. A `null` prefix or suffix is not rejected, because unlike a null regex it still expresses that the subject is null, an expectation that can fail in either polarity, and `IsEqualTo` deliberately accepts a nullable argument.
FluentAssertions follows the BCL, where every string starts with the empty string, so migrating users can hit the new exception.
vbreuss
enabled auto-merge (squash)
September 19, 2026 17:06
|
Contributor
Test Results 6 files ±0 6 suites ±0 1m 41s ⏱️ -3s Results for commit cac677a. ± Comparison against base commit 08dca50. This pull request removes 126 and adds 126 tests. Note that renamed tests count towards both. |
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.



StartsWith("")andEndsWith("")throw because an empty expectation is almost always a mistake - an accidentally empty variable - but the same expectation written asIsEqualTo("").AsPrefix()or.AsSuffix()silently passed. One expectation had two spellings that disagreed, and the silent one is the dangerous one, so the guard now also applies to the match type, wherever it is reached:IsEqualTo,Contains,HasItemand the string collection overloads.Just like a regex or wildcard pattern, a prefix is only known when the expectation is verified, so the guard sits next to them in
StringEqualityOptionsand fires after the normalization: a prefix that only becomes empty throughIgnoringIndentation()says just as little as a literal empty one. Anullprefix or suffix is not rejected, because unlike a null regex it still expresses that the subject is null, an expectation that can fail in either polarity, andIsEqualTodeliberately accepts a nullable argument.