From 4d9ef730ad368814c7950cf5defcc3284db7b9ed Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Mon, 13 May 2024 12:25:21 +0300 Subject: [PATCH] feat: add nunit null asserts migration docs --- docs/NunitAnalyzer.md | 64 ++++++++++++++ .../NunitAnalyzerTests.cs | 88 +++++++++++++++++++ 2 files changed, 152 insertions(+) diff --git a/docs/NunitAnalyzer.md b/docs/NunitAnalyzer.md index d5d4cf3b..d73c0dd4 100644 --- a/docs/NunitAnalyzer.md +++ b/docs/NunitAnalyzer.md @@ -6,6 +6,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser - [BooleanAssertIsTrue](#scenario-booleanassertistrue) - `flag.Should().BeTrue();` - [BooleanAssertIsFalse](#scenario-booleanassertisfalse) - `flag.Should().BeFalse();` +- [AssertNull](#scenario-assertnull) - `obj.Should().BeNull();` +- [AssertNotNull](#scenario-assertnotnull) - `obj.Should().NotBeNull();` ## Scenarios @@ -72,4 +74,66 @@ Assert.IsFalse(flag); /* fail message: Expected: False flag.Should().BeFalse(); /* fail message: Expected flag to be false, but found True. */ ``` +### scenario: AssertNull + +```cs +// arrange +object obj = null; + +// old assertion: +Assert.IsNull(obj); +Assert.Null(obj); + +// new assertion: +obj.Should().BeNull(); +``` + +#### Failure messages + +```cs +object obj = "foo"; + +// old assertion: +Assert.Null(obj); /* fail message: Expected: null + But was: "foo" + */ +Assert.IsNull(obj); /* fail message: Expected: null + But was: "foo" + */ + +// new assertion: +obj.Should().BeNull(); /* fail message: Expected obj to be , but found "foo". */ +``` + +### scenario: AssertNotNull + +```cs +// arrange +object obj = "foo"; + +// old assertion: +Assert.IsNotNull(obj); +Assert.NotNull(obj); + +// new assertion: +obj.Should().NotBeNull(); +``` + +#### Failure messages + +```cs +object obj = null; + +// old assertion: +Assert.NotNull(obj); /* fail message: Expected: not null + But was: null + */ +Assert.IsNotNull(obj); /* fail message: Expected: not null + But was: null + */ + +// new assertion: +obj.Should().NotBeNull(); /* fail message: Expected obj not to be . */ +``` + diff --git a/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/NunitAnalyzerTests.cs b/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/NunitAnalyzerTests.cs index 24f3f855..7d22059a 100644 --- a/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/NunitAnalyzerTests.cs +++ b/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/NunitAnalyzerTests.cs @@ -94,4 +94,92 @@ public void BooleanAssertIsFalse_Failure_NewAssertion() // new assertion: flag.Should().BeFalse(); } + + [TestMethod] + public void AssertNull() + { + // arrange + object obj = null; + + // old assertion: + Assert.IsNull(obj); + Assert.Null(obj); + + // new assertion: + obj.Should().BeNull(); + } + + [TestMethod, ExpectedTestFrameworkException] + public void AssertNull_Failure_OldAssertion_0() + { + // arrange + object obj = "foo"; + + // old assertion: + Assert.Null(obj); + } + + [TestMethod, ExpectedTestFrameworkException] + public void AssertNull_Failure_OldAssertion_1() + { + // arrange + object obj = "foo"; + + // old assertion: + Assert.IsNull(obj); + } + + [TestMethod, ExpectedTestFrameworkException] + public void AssertNull_Failure_NewAssertion() + { + // arrange + object obj = "foo"; + + // new assertion: + obj.Should().BeNull(); + } + + [TestMethod] + public void AssertNotNull() + { + // arrange + object obj = "foo"; + + // old assertion: + Assert.IsNotNull(obj); + Assert.NotNull(obj); + + // new assertion: + obj.Should().NotBeNull(); + } + + [TestMethod, ExpectedTestFrameworkException] + public void AssertNotNull_Failure_OldAssertion_0() + { + // arrange + object obj = null; + + // old assertion: + Assert.NotNull(obj); + } + + [TestMethod, ExpectedTestFrameworkException] + public void AssertNotNull_Failure_OldAssertion_1() + { + // arrange + object obj = null; + + // old assertion: + Assert.IsNotNull(obj); + } + + [TestMethod, ExpectedTestFrameworkException] + public void AssertNotNull_Failure_NewAssertion() + { + // arrange + object obj = null; + + // new assertion: + obj.Should().NotBeNull(); + } }