-
Notifications
You must be signed in to change notification settings - Fork 732
Throw when asserting on AndConstraint #1649
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
Throw when asserting on AndConstraint #1649
Conversation
|
The release notes are missing, but now I'm in doubt whether we should retain the |
|
If you write "Unreleased" instead of "6.1.0" the Changelog is correct. |
Oh, I like that. |
|
I've thought of an alternative approach where we instead add honeypot overloads of Pros:
Cons:
I could find 12 base assertions, that should cover all cases:
/// <summary>
/// You are asserting the 'AndConstaint' itself. You probably want to remove the 'Should()' method directly following 'And'.
/// </summary>
[Obsolete("You are asserting the 'AndConstaint' itself. You probably want to remove the 'Should()' method directly following 'And'", error: true)]
public void Should(ReferenceTypeAssertions<TSubject, TAssertions>)
where TAssertions : ReferenceTypeAssertions<TSubject, TAssertions>
{
InvalidShouldCall();
}
/// <summary>
/// You are asserting the 'AndConstaint' itself. You probably want to remove the 'Should()' method directly following 'And'.
/// </summary>
[Obsolete("You are asserting the 'AndConstaint' itself. You probably want to remove the 'Should()' method directly following 'And'", error: true)]
public void Should(BooleanAssertions<TAssertions>)
where TAssertions : BooleanAssertions<TAssertions>
{
InvalidShouldCall();
}
... 10 more ...
private static void InvalidShouldCall() =>
throw new InvalidOperationException("You are asserting the 'AndConstaint' itself. You probably want to remove the 'Should()' method directly following 'And'."); |
I like both approaches equally. The advantage of this "honeypot" |
|
Another benefit! |
Fair enough. @eNeRGy164 what do you think? |
|
May be another approach: Instead of pervert the call of |
I didn't go on this route as it changed the API (as mentioned by @jnyrup) and will require attention for authors in the future if they add new assertion classes. So, I will modify the code to this. I think the remark about the return value by @lg2de is also interesting. It could also be an Analyzer for that matter. You could argue that making the method |
|
Technical there are breaking changes. But there are no functional breaking changes, except there are users explicitly asserting on Adding |
Because of the |
|
We could debate is that we're just fixing a false positive for them. |
|
@dennisdoomen How about support chaining instead of preventing? |
Not sure what that would look like. Extension methods on all concrete versions of |
public static TAssertions Should<TAssertions>(this BooleanAssertions<TAssertions> @this)
where TAssertions : BooleanAssertions<TAssertions>
{
return (TAssertions)@this;
}Would allow one to write true.Should().BeTrue().And.Should().BeFalse();As the second true.Should().BeTrue().And.Should().Should().Should().BeFalse(); |
|
Nice. Just tried it. |
|
So, which direction do you want to go? The prevention or the NOOP chaining? |
|
I'm fine with the direction we took. @jnyrup needs to approve the PR. |
This is an attempt to help people when they write
.Should()directly after.Andby throwing an exception.As classes with assertions are not decorated with something to recognize (e.g. a shared base class or interface) I use reflection to get all the return types for
Shouldmethods on theAssertionExtensionsclass.Had to rewrite unit tests that were using
ObjectAssertionsto test cross-assembly type comparison as they would also fail with the new exception.Should fix #1617
IMPORTANT