From 1e6ed5006374df6ad82b727d37ebf969b135c679 Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Mon, 20 May 2024 23:20:55 +0300 Subject: [PATCH] feat: add nunit assert AreNotSame (#350) --- docs/Nunit3Analyzer.md | 60 +++++++++++++++ docs/Nunit4Analyzer.md | 64 +++++++++++++++- .../Nunit4AnalyzerTests.cs | 76 ++++++++++++++++++- .../Nunit3AnalyzerTests.cs | 74 +++++++++++++++++- .../Tips/NunitTests.cs | 36 ++++++++- 5 files changed, 302 insertions(+), 8 deletions(-) diff --git a/docs/Nunit3Analyzer.md b/docs/Nunit3Analyzer.md index 6ffdfd1..3c52de7 100644 --- a/docs/Nunit3Analyzer.md +++ b/docs/Nunit3Analyzer.md @@ -12,6 +12,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser - [AssertIsNotEmpty](#scenario-assertisnotempty) - `collection.Should().NotBeEmpty();` - [AssertZero](#scenario-assertzero) - `number.Should().Be(0);` - [AssertNotZero](#scenario-assertnotzero) - `number.Should().NotBe(0);` +- [AssertAreSame](#scenario-assertaresame) - `obj1.Should().BeSameAs(obj2);` +- [AssertAreNotSame](#scenario-assertarenotsame) - `obj1.Should().NotBeSameAs(obj2);` - [CollectionAssertAreEqual](#scenario-collectionassertareequal) - `collection.Should().Equal(expected);` - [CollectionAssertAreNotEqual](#scenario-collectionassertarenotequal) - `collection.Should().NotEqual(expected);` - [CollectionAssertContains](#scenario-collectionassertcontains) - `collection.Should().Contain(2);` @@ -308,6 +310,64 @@ Assert.That(number, Is.Not.Zero); /* fail message: Expected: not equal to 0 number.Should().NotBe(0); /* fail message: Did not expect number to be 0. */ ``` +### scenario: AssertAreSame + +```cs +// arrange +var obj1 = new object(); +var obj2 = obj1; + +// old assertion: +Assert.AreSame(obj1, obj2); + +// new assertion: +obj1.Should().BeSameAs(obj2); +``` + +#### Failure messages + +```cs +object obj1 = 6; +object obj2 = "foo"; + +// old assertion: +Assert.AreSame(obj1, obj2); /* fail message: Expected: same as 6 + But was: "foo" + */ + +// new assertion: +obj1.Should().BeSameAs(obj2); /* fail message: Expected obj1 to refer to "foo", but found 6. */ +``` + +### scenario: AssertAreNotSame + +```cs +// arrange +object obj1 = 6; +object obj2 = "foo"; + +// old assertion: +Assert.AreNotSame(obj1, obj2); + +// new assertion: +obj1.Should().NotBeSameAs(obj2); +``` + +#### Failure messages + +```cs +var obj1 = "foo"; +var obj2 = "foo"; + +// old assertion: +Assert.AreNotSame(obj1, obj2); /* fail message: Expected: not same as "foo" + But was: "foo" + */ + +// new assertion: +obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to "foo". */ +``` + ### scenario: CollectionAssertAreEqual ```cs diff --git a/docs/Nunit4Analyzer.md b/docs/Nunit4Analyzer.md index 1742854..9ff75cf 100644 --- a/docs/Nunit4Analyzer.md +++ b/docs/Nunit4Analyzer.md @@ -12,6 +12,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser - [AssertIsNotEmpty](#scenario-assertisnotempty) - `collection.Should().NotBeEmpty();` - [AssertZero](#scenario-assertzero) - `number.Should().Be(0);` - [AssertNotZero](#scenario-assertnotzero) - `number.Should().NotBe(0);` +- [AssertAreSame](#scenario-assertaresame) - `obj1.Should().BeSameAs(obj2);` +- [AssertAreNotSame](#scenario-assertarenotsame) - `obj1.Should().NotBeSameAs(obj2);` - [CollectionAssertAreEqual](#scenario-collectionassertareequal) - `collection.Should().Equal(expected);` - [CollectionAssertAreNotEqual](#scenario-collectionassertarenotequal) - `collection.Should().NotEqual(expected);` - [CollectionAssertContains](#scenario-collectionassertcontains) - `collection.Should().Contain(2);` @@ -333,12 +335,72 @@ Assert.That(number, Is.Not.Zero); /* fail message: Assert.That(number, Is.Not. number.Should().NotBe(0); /* fail message: Did not expect number to be 0. */ ``` +### scenario: AssertAreSame + +```cs +// arrange +var obj1 = new object(); +var obj2 = obj1; + +// old assertion: +ClassicAssert.AreSame(obj1, obj2); + +// new assertion: +obj1.Should().BeSameAs(obj2); +``` + +#### Failure messages + +```cs +object obj1 = 6; +object obj2 = "foo"; + +// old assertion: +ClassicAssert.AreSame(obj1, obj2); /* fail message: Assert.That(actual, Is.SameAs(expected)) + Expected: same as 6 + But was: "foo" + */ + +// new assertion: +obj1.Should().BeSameAs(obj2); /* fail message: Expected obj1 to refer to "foo", but found 6. */ +``` + +### scenario: AssertAreNotSame + +```cs +// arrange +object obj1 = 6; +object obj2 = "foo"; + +// old assertion: +ClassicAssert.AreNotSame(obj1, obj2); + +// new assertion: +obj1.Should().NotBeSameAs(obj2); +``` + +#### Failure messages + +```cs +var obj1 = new object(); +var obj2 = obj1; + +// old assertion: +ClassicAssert.AreNotSame(obj1, obj2); /* fail message: Assert.That(actual, Is.Not.SameAs(expected)) + Expected: not same as + But was: + */ + +// new assertion: +obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to System.Object (HashCode=2766117). */ +``` + ### scenario: CollectionAssertAreEqual ```cs // arrange var collection = new[] { 1, 2, 3 }; -var expected = new [] { 1, 2, 3 }; +var expected = new[] { 1, 2, 3 }; // old assertion: CollectionAssert.AreEqual(expected, collection); diff --git a/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs b/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs index dbb87c7..1950416 100644 --- a/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs +++ b/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs @@ -358,7 +358,7 @@ public void AssertIsNotEmpty_Failure_OldAssertion_2() // old assertion: CollectionAssert.IsNotEmpty(collection); -} + } [Test, ExpectedAssertionException] public void AssertIsNotEmpty_Failure_NewAssertion() @@ -458,12 +458,84 @@ public void AssertNotZero_Failure_NewAssertion() number.Should().NotBe(0); } + [Test] + public void AssertAreSame() + { + // arrange + var obj1 = new object(); + var obj2 = obj1; + + // old assertion: + ClassicAssert.AreSame(obj1, obj2); + + // new assertion: + obj1.Should().BeSameAs(obj2); + } + + [Test, ExpectedAssertionException] + public void AssertAreSame_Failure_OldAssertion() + { + // arrange + object obj1 = 6; + object obj2 = "foo"; + + // old assertion: + ClassicAssert.AreSame(obj1, obj2); + } + + [Test, ExpectedAssertionException] + public void AssertAreSame_Failure_NewAssertion() + { + // arrange + object obj1 = 6; + object obj2 = "foo"; + + // new assertion: + obj1.Should().BeSameAs(obj2); + } + + [Test] + public void AssertAreNotSame() + { + // arrange + object obj1 = 6; + object obj2 = "foo"; + + // old assertion: + ClassicAssert.AreNotSame(obj1, obj2); + + // new assertion: + obj1.Should().NotBeSameAs(obj2); + } + + [Test, ExpectedAssertionException] + public void AssertAreNotSame_Failure_OldAssertion() + { + // arrange + var obj1 = new object(); + var obj2 = obj1; + + // old assertion: + ClassicAssert.AreNotSame(obj1, obj2); + } + + [Test, ExpectedAssertionException] + public void AssertAreNotSame_Failure_NewAssertion() + { + // arrange + var obj1 = new object(); + var obj2 = obj1; + + // new assertion: + obj1.Should().NotBeSameAs(obj2); + } + [Test] public void CollectionAssertAreEqual() { // arrange var collection = new[] { 1, 2, 3 }; - var expected = new [] { 1, 2, 3 }; + var expected = new[] { 1, 2, 3 }; // old assertion: CollectionAssert.AreEqual(expected, collection); diff --git a/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs b/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs index 52161c2..32c59fc 100644 --- a/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs +++ b/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs @@ -361,7 +361,7 @@ public void AssertIsNotEmpty_Failure_OldAssertion_2() // old assertion: CollectionAssert.IsNotEmpty(collection); -} + } [TestMethod, ExpectedTestFrameworkException] public void AssertIsNotEmpty_Failure_NewAssertion() @@ -461,6 +461,78 @@ public void AssertNotZero_Failure_NewAssertion() number.Should().NotBe(0); } + [TestMethod] + public void AssertAreSame() + { + // arrange + var obj1 = new object(); + var obj2 = obj1; + + // old assertion: + Assert.AreSame(obj1, obj2); + + // new assertion: + obj1.Should().BeSameAs(obj2); + } + + [TestMethod, ExpectedTestFrameworkException] + public void AssertAreSame_Failure_OldAssertion() + { + // arrange + object obj1 = 6; + object obj2 = "foo"; + + // old assertion: + Assert.AreSame(obj1, obj2); + } + + [TestMethod, ExpectedTestFrameworkException] + public void AssertAreSame_Failure_NewAssertion() + { + // arrange + object obj1 = 6; + object obj2 = "foo"; + + // new assertion: + obj1.Should().BeSameAs(obj2); + } + + [TestMethod] + public void AssertAreNotSame() + { + // arrange + object obj1 = 6; + object obj2 = "foo"; + + // old assertion: + Assert.AreNotSame(obj1, obj2); + + // new assertion: + obj1.Should().NotBeSameAs(obj2); + } + + [TestMethod, ExpectedTestFrameworkException] + public void AssertAreNotSame_Failure_OldAssertion() + { + // arrange + var obj1 = "foo"; + var obj2 = "foo"; + + // old assertion: + Assert.AreNotSame(obj1, obj2); + } + + [TestMethod, ExpectedTestFrameworkException] + public void AssertAreNotSame_Failure_NewAssertion() + { + // arrange + var obj1 = "foo"; + var obj2 = "foo"; + + // new assertion: + obj1.Should().NotBeSameAs(obj2); + } + [TestMethod] public void CollectionAssertAreEqual() { diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs index 28b9307..fe8a2ec 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs @@ -915,13 +915,13 @@ public void Nunit4_AssertNotEqualObject_TestCodeFix(string oldAssertion, string [DataTestMethod] [AssertionDiagnostic("Assert.AreSame(expected, actual{0});")] [Implemented] - public void Nunit3_AssertSame_TestAnalyzer(string assertion) + public void Nunit3_AssertAreSame_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("object expected, object actual", assertion); [DataTestMethod] [AssertionDiagnostic("ClassicAssert.AreSame(expected, actual{0});")] [Implemented] - public void Nunit4_AssertSame_TestAnalyzer(string assertion) + public void Nunit4_AssertAreSame_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("object expected, object actual", assertion); [DataTestMethod] @@ -929,7 +929,7 @@ public void Nunit4_AssertSame_TestAnalyzer(string assertion) oldAssertion: "Assert.AreSame(expected, actual{0});", newAssertion: "actual.Should().BeSameAs(expected{0});")] [Implemented] - public void Nunit3_AssertSame_TestCodeFix(string oldAssertion, string newAssertion) + public void Nunit3_AssertAreSame_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("object expected, object actual", oldAssertion, newAssertion); [DataTestMethod] @@ -937,7 +937,35 @@ public void Nunit3_AssertSame_TestCodeFix(string oldAssertion, string newAsserti oldAssertion: "ClassicAssert.AreSame(expected, actual{0});", newAssertion: "actual.Should().BeSameAs(expected{0});")] [Implemented] - public void Nunit4_AssertSame_TestCodeFix(string oldAssertion, string newAssertion) + public void Nunit4_AssertAreSame_TestCodeFix(string oldAssertion, string newAssertion) + => Nunit4VerifyFix("object expected, object actual", oldAssertion, newAssertion); + + [DataTestMethod] + [AssertionDiagnostic("Assert.AreNotSame(expected, actual{0});")] + [Implemented] + public void Nunit3_AssertAreNotSame_TestAnalyzer(string assertion) + => Nunit3VerifyDiagnostic("object expected, object actual", assertion); + + [DataTestMethod] + [AssertionDiagnostic("ClassicAssert.AreNotSame(expected, actual{0});")] + [Implemented] + public void Nunit4_AssertAreNotSame_TestAnalyzer(string assertion) + => Nunit4VerifyDiagnostic("object expected, object actual", assertion); + + [DataTestMethod] + [AssertionCodeFix( + oldAssertion: "Assert.AreNotSame(expected, actual{0});", + newAssertion: "actual.Should().NotBeSameAs(expected{0});")] + [Implemented] + public void Nunit3_AssertAreNotSame_TestCodeFix(string oldAssertion, string newAssertion) + => Nunit3VerifyFix("object expected, object actual", oldAssertion, newAssertion); + + [DataTestMethod] + [AssertionCodeFix( + oldAssertion: "ClassicAssert.AreNotSame(expected, actual{0});", + newAssertion: "actual.Should().NotBeSameAs(expected{0});")] + [Implemented] + public void Nunit4_AssertAreNotSame_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("object expected, object actual", oldAssertion, newAssertion); #endregion