diff --git a/documentation/NUnit4002.md b/documentation/NUnit4002.md index c42a23d1..b4e64dfd 100644 --- a/documentation/NUnit4002.md +++ b/documentation/NUnit4002.md @@ -16,11 +16,41 @@ Replace 'EqualTo' with a keyword in the corresponding specific constraint. ## Motivation -ADD MOTIVATION HERE +Sometimes constraints can be written more concisely using the inbuilt constraints provided by NUnit - +e.g. `Is.True` instead of `Is.EqualTo(true)`. + +Also, from NUnit version 4.3.0 where new overloads of `Is.EqualTo` were introduced, it is sometimes +not possible to uniquely determine `default` when provided as the expected value - e.g. in +`Is.EqualTo(default)`. Again, in such cases, it is better to use the inbuilt constraint provided by NUnit. + +Some examples of constraints that can be be simplified by using a more specific constraint can be seen below. + +```csharp +[Test] +public void Test() +{ + Assert.That(actualFalse, Is.EqualTo(false)); + Assert.That(actualTrue, Is.EqualTo(true)); + Assert.That(actualObject, Is.EqualTo(null)); + Assert.That(actualObject, Is.EqualTo(default)); + Assert.That(actualInt, Is.EqualTo(default)); +} ## How to fix violations -ADD HOW TO FIX VIOLATIONS HERE +The analyzer comes with a code fix that will replace the constraint `Is.EqualTo(x)` with +the matching `Is.X` constraint (for some `x`). So the code block above will be changed into + +```csharp +[Test] +public void Test() +{ + Assert.That(actualFalse, Is.False); + Assert.That(actualTrue, Is.True); + Assert.That(actualObject, Is.Null); + Assert.That(actualObject, Is.Null); + Assert.That(actualInt, Is.Default); +} ## Configure severity