From b9a2416d10716da0fb43beb8466bc734fc3e3a74 Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Sat, 1 May 2021 12:48:29 +0200 Subject: [PATCH] Make [Not]HaveSameCount generic only --- .../GenericCollectionAssertions.cs | 9 ++++---- .../FluentAssertions/net47.verified.txt | 4 ++-- .../netcoreapp2.1.verified.txt | 4 ++-- .../netcoreapp3.0.verified.txt | 4 ++-- .../netstandard2.0.verified.txt | 4 ++-- .../netstandard2.1.verified.txt | 4 ++-- .../GenericDictionaryAssertionSpecs.cs | 22 +++++++++---------- 7 files changed, 25 insertions(+), 26 deletions(-) diff --git a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs index d2b30a2468..ed7df0c263 100644 --- a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs +++ b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -1452,7 +1451,7 @@ public AndConstraint HaveElementSucceeding(T predecessor, T expecta /// /// Zero or more objects to format using the placeholders in . /// - public AndConstraint HaveSameCount(IEnumerable otherCollection, string because = "", + public AndConstraint HaveSameCount(IEnumerable otherCollection, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(otherCollection, nameof(otherCollection), "Cannot verify count against a collection."); @@ -1467,7 +1466,7 @@ public AndConstraint HaveElementSucceeding(T predecessor, T expecta } int actualCount = Subject.Count(); - int expectedCount = (otherCollection as ICollection)?.Count ?? otherCollection.Cast().Count(); + int expectedCount = otherCollection.Count(); Execute.Assertion .ForCondition(actualCount == expectedCount) @@ -2390,7 +2389,7 @@ public AndConstraint NotHaveCount(int unexpected, string because = /// /// Zero or more objects to format using the placeholders in . /// - public AndConstraint NotHaveSameCount(IEnumerable otherCollection, string because = "", + public AndConstraint NotHaveSameCount(IEnumerable otherCollection, string because = "", params object[] becauseArgs) { Guard.ThrowIfArgumentIsNull(otherCollection, nameof(otherCollection), "Cannot verify count against a collection."); @@ -2414,7 +2413,7 @@ public AndConstraint NotHaveCount(int unexpected, string because = } int actualCount = Subject.Count(); - int expectedCount = otherCollection.Cast().Count(); + int expectedCount = otherCollection.Count(); Execute.Assertion .ForCondition(actualCount != expectedCount) diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt index 490e3f14f0..7a2ffa4ca4 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/net47.verified.txt @@ -377,7 +377,7 @@ namespace FluentAssertions.Collections public FluentAssertions.AndWhichConstraint HaveElementAt(int index, T element, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint HaveElementPreceding(T successor, T expectation, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint HaveElementSucceeding(T predecessor, T expectation, string because = "", params object[] becauseArgs) { } - public FluentAssertions.AndConstraint HaveSameCount(System.Collections.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint HaveSameCount(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint IntersectWith(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotBeEmpty(string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotBeEquivalentTo(System.Collections.Generic.IEnumerable unexpected, string because = "", params object[] becauseArgs) { } @@ -406,7 +406,7 @@ namespace FluentAssertions.Collections where TKey : class { } public FluentAssertions.AndConstraint NotEqual(System.Collections.Generic.IEnumerable unexpected, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotHaveCount(int unexpected, string because = "", params object[] becauseArgs) { } - public FluentAssertions.AndConstraint NotHaveSameCount(System.Collections.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint NotHaveSameCount(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotIntersectWith(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint OnlyContain(System.Linq.Expressions.Expression> predicate, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint OnlyHaveUniqueItems(string because = "", params object[] becauseArgs) { } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt index 1338d77954..64b707dd4b 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp2.1.verified.txt @@ -377,7 +377,7 @@ namespace FluentAssertions.Collections public FluentAssertions.AndWhichConstraint HaveElementAt(int index, T element, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint HaveElementPreceding(T successor, T expectation, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint HaveElementSucceeding(T predecessor, T expectation, string because = "", params object[] becauseArgs) { } - public FluentAssertions.AndConstraint HaveSameCount(System.Collections.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint HaveSameCount(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint IntersectWith(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotBeEmpty(string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotBeEquivalentTo(System.Collections.Generic.IEnumerable unexpected, string because = "", params object[] becauseArgs) { } @@ -406,7 +406,7 @@ namespace FluentAssertions.Collections where TKey : class { } public FluentAssertions.AndConstraint NotEqual(System.Collections.Generic.IEnumerable unexpected, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotHaveCount(int unexpected, string because = "", params object[] becauseArgs) { } - public FluentAssertions.AndConstraint NotHaveSameCount(System.Collections.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint NotHaveSameCount(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotIntersectWith(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint OnlyContain(System.Linq.Expressions.Expression> predicate, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint OnlyHaveUniqueItems(string because = "", params object[] becauseArgs) { } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt index 22d5c4c319..8d98a3fa59 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netcoreapp3.0.verified.txt @@ -377,7 +377,7 @@ namespace FluentAssertions.Collections public FluentAssertions.AndWhichConstraint HaveElementAt(int index, T element, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint HaveElementPreceding(T successor, T expectation, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint HaveElementSucceeding(T predecessor, T expectation, string because = "", params object[] becauseArgs) { } - public FluentAssertions.AndConstraint HaveSameCount(System.Collections.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint HaveSameCount(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint IntersectWith(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotBeEmpty(string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotBeEquivalentTo(System.Collections.Generic.IEnumerable unexpected, string because = "", params object[] becauseArgs) { } @@ -406,7 +406,7 @@ namespace FluentAssertions.Collections where TKey : class { } public FluentAssertions.AndConstraint NotEqual(System.Collections.Generic.IEnumerable unexpected, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotHaveCount(int unexpected, string because = "", params object[] becauseArgs) { } - public FluentAssertions.AndConstraint NotHaveSameCount(System.Collections.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint NotHaveSameCount(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotIntersectWith(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint OnlyContain(System.Linq.Expressions.Expression> predicate, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint OnlyHaveUniqueItems(string because = "", params object[] becauseArgs) { } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt index 7096f68bdf..f261a7f15f 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.0.verified.txt @@ -370,7 +370,7 @@ namespace FluentAssertions.Collections public FluentAssertions.AndWhichConstraint HaveElementAt(int index, T element, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint HaveElementPreceding(T successor, T expectation, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint HaveElementSucceeding(T predecessor, T expectation, string because = "", params object[] becauseArgs) { } - public FluentAssertions.AndConstraint HaveSameCount(System.Collections.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint HaveSameCount(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint IntersectWith(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotBeEmpty(string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotBeEquivalentTo(System.Collections.Generic.IEnumerable unexpected, string because = "", params object[] becauseArgs) { } @@ -399,7 +399,7 @@ namespace FluentAssertions.Collections where TKey : class { } public FluentAssertions.AndConstraint NotEqual(System.Collections.Generic.IEnumerable unexpected, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotHaveCount(int unexpected, string because = "", params object[] becauseArgs) { } - public FluentAssertions.AndConstraint NotHaveSameCount(System.Collections.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint NotHaveSameCount(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotIntersectWith(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint OnlyContain(System.Linq.Expressions.Expression> predicate, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint OnlyHaveUniqueItems(string because = "", params object[] becauseArgs) { } diff --git a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt index 62f4d90511..7dd6bb163e 100644 --- a/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt +++ b/Tests/Approval.Tests/ApprovedApi/FluentAssertions/netstandard2.1.verified.txt @@ -377,7 +377,7 @@ namespace FluentAssertions.Collections public FluentAssertions.AndWhichConstraint HaveElementAt(int index, T element, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint HaveElementPreceding(T successor, T expectation, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint HaveElementSucceeding(T predecessor, T expectation, string because = "", params object[] becauseArgs) { } - public FluentAssertions.AndConstraint HaveSameCount(System.Collections.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint HaveSameCount(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint IntersectWith(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotBeEmpty(string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotBeEquivalentTo(System.Collections.Generic.IEnumerable unexpected, string because = "", params object[] becauseArgs) { } @@ -406,7 +406,7 @@ namespace FluentAssertions.Collections where TKey : class { } public FluentAssertions.AndConstraint NotEqual(System.Collections.Generic.IEnumerable unexpected, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotHaveCount(int unexpected, string because = "", params object[] becauseArgs) { } - public FluentAssertions.AndConstraint NotHaveSameCount(System.Collections.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } + public FluentAssertions.AndConstraint NotHaveSameCount(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint NotIntersectWith(System.Collections.Generic.IEnumerable otherCollection, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint OnlyContain(System.Linq.Expressions.Expression> predicate, string because = "", params object[] becauseArgs) { } public FluentAssertions.AndConstraint OnlyHaveUniqueItems(string because = "", params object[] becauseArgs) { } diff --git a/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs index fc05481ad2..08a3a4ee71 100644 --- a/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs @@ -615,7 +615,7 @@ public void When_dictionary_and_collection_have_the_same_number_elements_it_shou [2] = "Two", [3] = "Three" }; - IEnumerable collection = new[] { 4, 5, 6 }; + var collection = new[] { 4, 5, 6 }; // Act / Assert dictionary.Should().HaveSameCount(collection); @@ -631,7 +631,7 @@ public void When_dictionary_and_collection_do_not_have_the_same_number_of_elemen [2] = "Two", [3] = "Three" }; - IEnumerable collection = new[] { 4, 6 }; + var collection = new[] { 4, 6 }; // Act Action act = () => dictionary.Should().HaveSameCount(collection); @@ -651,7 +651,7 @@ public void When_comparing_item_counts_and_a_reason_is_specified_it_should_it_in [2] = "Two", [3] = "Three" }; - IEnumerable collection = new[] { 4, 6 }; + var collection = new[] { 4, 6 }; // Act Action act = () => dictionary.Should().HaveSameCount(collection, "we want to test the {0}", "reason"); @@ -666,7 +666,7 @@ public void When_asserting_dictionary_and_collection_have_same_count_against_nul { // Arrange Dictionary dictionary = null; - IEnumerable collection = new[] { 1, 2, 3 }; + var collection = new[] { 1, 2, 3 }; // Act Action act = () => dictionary.Should().HaveSameCount(collection, @@ -687,7 +687,7 @@ public void When_asserting_dictionary_and_collection_have_same_count_against_a_n [2] = "Two", [3] = "Three" }; - IEnumerable collection = null; + int[] collection = null; // Act Action act = () => dictionary.Should().HaveSameCount(collection); @@ -711,7 +711,7 @@ public void When_asserting_not_same_count_and_collections_have_different_number_ [2] = "Two", [3] = "Three" }; - IEnumerable collection = new[] { 4, 6 }; + var collection = new[] { 4, 6 }; // Act / Assert dictionary.Should().NotHaveSameCount(collection); @@ -727,7 +727,7 @@ public void When_asserting_not_same_count_and_both_collections_have_the_same_num [2] = "Two", [3] = "Three" }; - IEnumerable collection = new[] { 4, 5, 6 }; + var collection = new[] { 4, 5, 6 }; // Act Action act = () => dictionary.Should().NotHaveSameCount(collection); @@ -747,7 +747,7 @@ public void When_comparing_not_same_item_counts_and_a_reason_is_specified_it_sho [2] = "Two", [3] = "Three" }; - IEnumerable collection = new[] { 4, 5, 6 }; + var collection = new[] { 4, 5, 6 }; // Act Action act = () => dictionary.Should().NotHaveSameCount(collection, "we want to test the {0}", "reason"); @@ -762,7 +762,7 @@ public void When_asserting_dictionary_and_collection_to_not_have_same_count_agai { // Arrange Dictionary dictionary = null; - IEnumerable collection = new[] { 1, 2, 3 }; + var collection = new[] { 1, 2, 3 }; // Act Action act = () => dictionary.Should().NotHaveSameCount(collection, @@ -783,7 +783,7 @@ public void When_asserting_dictionary_and_collection_to_not_have_same_count_agai [2] = "Two", [3] = "Three" }; - IEnumerable collection = null; + int[] collection = null; // Act Action act = () => dictionary.Should().NotHaveSameCount(collection); @@ -803,7 +803,7 @@ public void When_asserting_dictionary_and_collection_to_not_have_same_count_but_ [2] = "Two", [3] = "Three" }; - IEnumerable collection = dictionary; + var collection = dictionary; // Act Action act = () => dictionary.Should().NotHaveSameCount(collection,