diff --git a/docs/Nunit3Analyzer.md b/docs/Nunit3Analyzer.md index 3c52de7..b67a0fe 100644 --- a/docs/Nunit3Analyzer.md +++ b/docs/Nunit3Analyzer.md @@ -22,6 +22,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser - [CollectionAssertDoesNotContain_WithCasting](#scenario-collectionassertdoesnotcontain_withcasting) - `collection.Should().NotContain((int)item);` - [CollectionAssertAllItemsAreInstancesOfType](#scenario-collectionassertallitemsareinstancesoftype) - `collection.Should().AllBeOfType();` - [CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument](#scenario-collectionassertallitemsareinstancesoftype_withtypeargument) - `collection.Should().AllBeOfType(type);` +- [CollectionAssertAllItemsAreNotNull](#scenario-collectionassertallitemsarenotnull) - `collection.Should().NotContainNulls();` +- [CollectionAssertAllItemsAreUnique](#scenario-collectionassertallitemsareunique) - `collection.Should().OnlyHaveUniqueItems();` ## Scenarios @@ -598,4 +600,60 @@ CollectionAssert.AllItemsAreInstancesOfType(collection, type); /* fail message: collection.Should().AllBeOfType(type); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */ ``` +### scenario: CollectionAssertAllItemsAreNotNull + +```cs +// arrange +var collection = new object[] { 1, "test", true }; + +// old assertion: +CollectionAssert.AllItemsAreNotNull(collection); + +// new assertion: +collection.Should().NotContainNulls(); +``` + +#### Failure messages + +```cs +var collection = new object[] { 1, null, true }; + +// old assertion: +CollectionAssert.AllItemsAreNotNull(collection); /* fail message: Expected: all items not null + But was: < 1, null, True > + First non-matching item at index [1]: null + */ + +// new assertion: +collection.Should().NotContainNulls(); /* fail message: Expected collection not to contain s, but found one at index 1. */ +``` + +### scenario: CollectionAssertAllItemsAreUnique + +```cs +// arrange +var collection = new[] { 1, 2, 3 }; + +// old assertion: +CollectionAssert.AllItemsAreUnique(collection); + +// new assertion: +collection.Should().OnlyHaveUniqueItems(); +``` + +#### Failure messages + +```cs +var collection = new[] { 1, 2, 1 }; + +// old assertion: +CollectionAssert.AllItemsAreUnique(collection); /* fail message: Expected: all items unique + But was: < 1, 2, 1 > + Not unique items: < 1 > + */ + +// new assertion: +collection.Should().OnlyHaveUniqueItems(); /* fail message: Expected collection to only have unique items, but item 1 is not unique. */ +``` + diff --git a/docs/Nunit4Analyzer.md b/docs/Nunit4Analyzer.md index 9ff75cf..944e0b5 100644 --- a/docs/Nunit4Analyzer.md +++ b/docs/Nunit4Analyzer.md @@ -22,6 +22,8 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser - [CollectionAssertDoesNotContain_WithCasting](#scenario-collectionassertdoesnotcontain_withcasting) - `collection.Should().NotContain((int)item);` - [CollectionAssertAllItemsAreInstancesOfType](#scenario-collectionassertallitemsareinstancesoftype) - `collection.Should().AllBeOfType();` - [CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument](#scenario-collectionassertallitemsareinstancesoftype_withtypeargument) - `collection.Should().AllBeOfType(type);` +- [CollectionAssertAllItemsAreNotNull](#scenario-collectionassertallitemsarenotnull) - `collection.Should().NotContainNulls();` +- [CollectionAssertAllItemsAreUnique](#scenario-collectionassertallitemsareunique) - `collection.Should().OnlyHaveUniqueItems();` ## Scenarios @@ -392,7 +394,7 @@ ClassicAssert.AreNotSame(obj1, obj2); /* fail message: Assert.That(actual, Is. */ // new assertion: -obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to System.Object (HashCode=2766117). */ +obj1.Should().NotBeSameAs(obj2); /* fail message: Did not expect obj1 to refer to System.Object (HashCode=19989589). */ ``` ### scenario: CollectionAssertAreEqual @@ -633,4 +635,62 @@ CollectionAssert.AllItemsAreInstancesOfType(collection, type); /* fail message: collection.Should().AllBeOfType(type); /* fail message: Expected type to be "System.Int32", but found "[System.Int32, System.Int32, System.String]". */ ``` +### scenario: CollectionAssertAllItemsAreNotNull + +```cs +// arrange +var collection = new object[] { 1, "test", true }; + +// old assertion: +CollectionAssert.AllItemsAreNotNull(collection); + +// new assertion: +collection.Should().NotContainNulls(); +``` + +#### Failure messages + +```cs +var collection = new object[] { 1, null, true }; + +// old assertion: +CollectionAssert.AllItemsAreNotNull(collection); /* fail message: Assert.That(collection, Is.All.Not.Null) + Expected: all items not null + But was: < 1, null, True > + First non-matching item at index [1]: null + */ + +// new assertion: +collection.Should().NotContainNulls(); /* fail message: Expected collection not to contain s, but found one at index 1. */ +``` + +### scenario: CollectionAssertAllItemsAreUnique + +```cs +// arrange +var collection = new[] { 1, 2, 3 }; + +// old assertion: +CollectionAssert.AllItemsAreUnique(collection); + +// new assertion: +collection.Should().OnlyHaveUniqueItems(); +``` + +#### Failure messages + +```cs +var collection = new[] { 1, 2, 1 }; + +// old assertion: +CollectionAssert.AllItemsAreUnique(collection); /* fail message: Assert.That(collection, Is.Unique) + Expected: all items unique + But was: < 1, 2, 1 > + Not unique items: < 1 > + */ + +// new assertion: +collection.Should().OnlyHaveUniqueItems(); /* fail message: Expected collection to only have unique items, but item 1 is not unique. */ +``` + diff --git a/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs b/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs index 1950416..2b9e5bb 100644 --- a/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs +++ b/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4/Nunit4AnalyzerTests.cs @@ -808,4 +808,70 @@ public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_ // new assertion: collection.Should().AllBeOfType(type); } + + [Test] + public void CollectionAssertAllItemsAreNotNull() + { + // arrange + var collection = new object[] { 1, "test", true }; + + // old assertion: + CollectionAssert.AllItemsAreNotNull(collection); + + // new assertion: + collection.Should().NotContainNulls(); + } + + [Test, ExpectedAssertionException] + public void CollectionAssertAllItemsAreNotNull_Failure_OldAssertion() + { + // arrange + var collection = new object[] { 1, null, true }; + + // old assertion: + CollectionAssert.AllItemsAreNotNull(collection); + } + + [Test, ExpectedAssertionException] + public void CollectionAssertAllItemsAreNotNull_Failure_NewAssertion() + { + // arrange + var collection = new object[] { 1, null, true }; + + // new assertion: + collection.Should().NotContainNulls(); + } + + [Test] + public void CollectionAssertAllItemsAreUnique() + { + // arrange + var collection = new[] { 1, 2, 3 }; + + // old assertion: + CollectionAssert.AllItemsAreUnique(collection); + + // new assertion: + collection.Should().OnlyHaveUniqueItems(); + } + + [Test, ExpectedAssertionException] + public void CollectionAssertAllItemsAreUnique_Failure_OldAssertion() + { + // arrange + var collection = new[] { 1, 2, 1 }; + + // old assertion: + CollectionAssert.AllItemsAreUnique(collection); + } + + [Test, ExpectedAssertionException] + public void CollectionAssertAllItemsAreUnique_Failure_NewAssertion() + { + // arrange + var collection = new[] { 1, 2, 1 }; + + // new assertion: + collection.Should().OnlyHaveUniqueItems(); + } } \ No newline at end of file diff --git a/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs b/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs index 32c59fc..7b358a6 100644 --- a/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs +++ b/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/Nunit3AnalyzerTests.cs @@ -811,4 +811,70 @@ public void CollectionAssertAllItemsAreInstancesOfType_WithTypeArgument_Failure_ // new assertion: collection.Should().AllBeOfType(type); } + + [TestMethod] + public void CollectionAssertAllItemsAreNotNull() + { + // arrange + var collection = new object[] { 1, "test", true }; + + // old assertion: + CollectionAssert.AllItemsAreNotNull(collection); + + // new assertion: + collection.Should().NotContainNulls(); + } + + [TestMethod, ExpectedTestFrameworkException] + public void CollectionAssertAllItemsAreNotNull_Failure_OldAssertion() + { + // arrange + var collection = new object[] { 1, null, true }; + + // old assertion: + CollectionAssert.AllItemsAreNotNull(collection); + } + + [TestMethod, ExpectedTestFrameworkException] + public void CollectionAssertAllItemsAreNotNull_Failure_NewAssertion() + { + // arrange + var collection = new object[] { 1, null, true }; + + // new assertion: + collection.Should().NotContainNulls(); + } + + [TestMethod] + public void CollectionAssertAllItemsAreUnique() + { + // arrange + var collection = new[] { 1, 2, 3 }; + + // old assertion: + CollectionAssert.AllItemsAreUnique(collection); + + // new assertion: + collection.Should().OnlyHaveUniqueItems(); + } + + [TestMethod, ExpectedTestFrameworkException] + public void CollectionAssertAllItemsAreUnique_Failure_OldAssertion() + { + // arrange + var collection = new[] { 1, 2, 1 }; + + // old assertion: + CollectionAssert.AllItemsAreUnique(collection); + } + + [TestMethod, ExpectedTestFrameworkException] + public void CollectionAssertAllItemsAreUnique_Failure_NewAssertion() + { + // arrange + var collection = new[] { 1, 2, 1 }; + + // new assertion: + collection.Should().OnlyHaveUniqueItems(); + } } diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs index fe8a2ec..6939c41 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/NunitTests.cs @@ -1608,6 +1608,53 @@ public void Nunit4_CollectionAssertDoesNotContain_WithCasting_TestCodeFix(string [Implemented] public void Nunit4_CollectionAssertAllItemsAreInstancesOfType_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("IEnumerable actual, Type type", oldAssertion, newAssertion); + [DataTestMethod] + [AssertionDiagnostic("CollectionAssert.AllItemsAreNotNull(actual{0});")] + [Implemented] + public void Nunit3_CollectionAssertAllItemsAreNotNull_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("IEnumerable actual", assertion); + + [DataTestMethod] + [AssertionDiagnostic("CollectionAssert.AllItemsAreNotNull(actual{0});")] + [Implemented] + public void Nunit4_CollectionAssertAllItemsAreNotNull_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("IEnumerable actual", assertion); + + [DataTestMethod] + [AssertionCodeFix( + oldAssertion: "CollectionAssert.AllItemsAreNotNull(actual{0});", + newAssertion: "actual.Should().NotContainNulls({0});")] + [Implemented] + public void Nunit3_CollectionAssertAllItemsAreNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("IEnumerable actual", oldAssertion, newAssertion); + + [DataTestMethod] + [AssertionCodeFix( + oldAssertion: "CollectionAssert.AllItemsAreNotNull(actual{0});", + newAssertion: "actual.Should().NotContainNulls({0});")] + [Implemented] + public void Nunit4_CollectionAssertAllItemsAreNotNull_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("IEnumerable actual", oldAssertion, newAssertion); + + [DataTestMethod] + [AssertionDiagnostic("CollectionAssert.AllItemsAreUnique(actual{0});")] + [Implemented] + public void Nunit3_CollectionAssertAllItemsAreUnique_TestAnalyzer(string assertion) => Nunit3VerifyDiagnostic("IEnumerable actual", assertion); + + [DataTestMethod] + [AssertionDiagnostic("CollectionAssert.AllItemsAreUnique(actual{0});")] + [Implemented] + public void Nunit4_CollectionAssertAllItemsAreUnique_TestAnalyzer(string assertion) => Nunit4VerifyDiagnostic("IEnumerable actual", assertion); + + [DataTestMethod] + [AssertionCodeFix( + oldAssertion: "CollectionAssert.AllItemsAreUnique(actual{0});", + newAssertion: "actual.Should().OnlyHaveUniqueItems({0});")] + [Implemented] + public void Nunit3_CollectionAssertAllItemsAreUnique_TestCodeFix(string oldAssertion, string newAssertion) => Nunit3VerifyFix("IEnumerable actual", oldAssertion, newAssertion); + + [DataTestMethod] + [AssertionCodeFix( + oldAssertion: "CollectionAssert.AllItemsAreUnique(actual{0});", + newAssertion: "actual.Should().OnlyHaveUniqueItems({0});")] + [Implemented] + public void Nunit4_CollectionAssertAllItemsAreUnique_TestCodeFix(string oldAssertion, string newAssertion) => Nunit4VerifyFix("IEnumerable actual", oldAssertion, newAssertion); #endregion diff --git a/src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs b/src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs index 8c9d56a..a857686 100644 --- a/src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs +++ b/src/FluentAssertions.Analyzers/Tips/NunitCodeFixProvider.cs @@ -209,10 +209,6 @@ private CreateChangedDocument TryComputeFixForNunitClassicAssert(IInvocationOper private CreateChangedDocument TryComputeFixForCollectionAssert(IInvocationOperation invocation, CodeFixContext context, NunitCodeFixContext t) { /** - public static void AllItemsAreNotNull(IEnumerable collection) - public static void AllItemsAreNotNull(IEnumerable collection, string message, params object[] args) - public static void AllItemsAreUnique(IEnumerable collection) - public static void AllItemsAreUnique(IEnumerable collection, string message, params object[] args) public static void AreEqual(IEnumerable expected, IEnumerable actual, IComparer comparer) public static void AreEqual(IEnumerable expected, IEnumerable actual, IComparer comparer, string message, params object[] args) public static void AreEquivalent(IEnumerable expected, IEnumerable actual) @@ -269,6 +265,10 @@ public static void IsOrdered(IEnumerable collection, IComparer comparer) return DocumentEditorUtils.RenameMethodToSubjectShouldGenericAssertion(invocation, ImmutableArray.Create(typeOf.TypeOperand), context, "AllBeOfType", subjectIndex: 0, argumentsToRemove: [1]); } + case "AllItemsAreNotNull": // CollectionAssert.AllItemsAreNotNull(IEnumerable collection) + return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "NotContainNulls", subjectIndex: 0, argumentsToRemove: []); + case "AllItemsAreUnique": // CollectionAssert.AllItemsAreUnique(IEnumerable collection) + return DocumentEditorUtils.RenameMethodToSubjectShouldAssertion(invocation, context, "OnlyHaveUniqueItems", subjectIndex: 0, argumentsToRemove: []); } return null; }