-
Notifications
You must be signed in to change notification settings - Fork 285
Add CallerArgumentExpression overloads to Assert.IsTrue/IsFalse/IsNull/IsNotNull #5851
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
Conversation
@@ -163,7 +145,7 @@ public static void IsNull(object? value, [InterpolatedStringHandlerArgument(name | |||
/// <exception cref="AssertFailedException"> | |||
/// Thrown if <paramref name="value"/> is not null. | |||
/// </exception> | |||
public static void IsNull(object? value, string? message) | |||
public static void IsNull(object? value, [CallerArgumentExpression(nameof(value))] string message = "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can discuss about the design, what I had in mind was to still allow the user to set a custom message but for us to use the capture mechanism to enrich the default message (always displayed - or with some verbosity setting in case).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if we should have an analyzer to ensure people don't set this value or if we should use the trick from @sharwell (bait and switch).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could be missing something, but I don't see how that trick can prevent users from passing the value. If the overload is available in the assembly passed to Csc
, then it's available.
The trick is useful if we want to preserve binary compatibility and keep Assert.IsNull(object)
but let the compiler prefer Assert.IsNull(object, /*caller arg expression*/ string = "")
, by passing an assembly that only has the latter to Csc
, but at run time, we keep the assembly that has both overloads.
91ab65c
to
0e864ba
Compare
0e864ba
to
3782fab
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev/v4 #5851 +/- ##
==========================================
+ Coverage 62.99% 63.05% +0.05%
==========================================
Files 574 574
Lines 31434 31417 -17
==========================================
+ Hits 19803 19811 +8
+ Misses 11631 11606 -25
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big fan of the current message but let's merge as-is and I'll suggest a bigger assertion message rename.
Part of #1154
Related to #1285