Assert.IsTrue and Assert.IsFalse are often misused. Alternative assertions offer better information when the test fails. For example, with classic model assertions:
- Assert.IsTrue( x < y );
+ Assert.Less( x, y );
- Assert.IsFalse( z == null );
+ Assert.NotNull( z );
- Assert.IsTrue( x < y );
+ Assert.That( x, Is.LessThan( y ) );
- Assert.IsFalse( x == null );
+ Assert.That( z, Is.Not.Null );
we can detect most bad IsTrue/IsFalse assertions by checking if the first argument is a BinaryExpressionSyntax. syntax.OperatorToken could make to the correct assertion/constraint to use and syntax.Left and syntax.Right can be moved around to arguments of the new assert (and constraint if we went that way.)
Open questions
Classic model assertions or fluent assertions?
We mostly still use classic-style I believe but maybe we should make the fixit suggest the new stuff. Do we want to adopt the new kind or not?
When the argument isn't a BinaryExpressionSyntax
This is a slightly harder case:
var result = x < y;
Assert.IsTrue( result );
is it worth catching this case? Definitely better to ignore it on the first-pass. We could do a survey to see how frequently this syntax pattern happens.
Assert.IsTrue
andAssert.IsFalse
are often misused. Alternative assertions offer better information when the test fails. For example, with classic model assertions:or with the fluent APIs (reference):
we can detect most bad
IsTrue
/IsFalse
assertions by checking if the first argument is aBinaryExpressionSyntax
.syntax.OperatorToken
could make to the correct assertion/constraint to use andsyntax.Left
andsyntax.Right
can be moved around to arguments of the new assert (and constraint if we went that way.)Open questions
Classic model assertions or fluent assertions?
We mostly still use classic-style I believe but maybe we should make the fixit suggest the new stuff. Do we want to adopt the new kind or not?
When the argument isn't a
BinaryExpressionSyntax
This is a slightly harder case:
is it worth catching this case? Definitely better to ignore it on the first-pass. We could do a survey to see how frequently this syntax pattern happens.