From f6c65c164ab0771b35c115b821bf3daac95bbea8 Mon Sep 17 00:00:00 2001 From: iliashkolyar Date: Tue, 28 Dec 2021 17:02:20 +0200 Subject: [PATCH] =?UTF-8?q?Change=20HaveCount=20assertion=20message=20orde?= =?UTF-8?q?r=20to=20state=20number=20before=20dumpi=E2=80=A6=20(#1760)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change HaveCount assertion message order to state number before dumping object contents --- .../GenericCollectionAssertions.cs | 24 +++++++++---------- .../CollectionAssertionSpecs.HaveCount.cs | 4 ++-- ...tionAssertionSpecs.HaveCountGreaterThan.cs | 2 +- ...tionSpecs.HaveCountGreaterThanOrEqualTo.cs | 2 +- ...lectionAssertionSpecs.HaveCountLessThan.cs | 2 +- ...sertionSpecs.HaveCountLessThanOrEqualTo.cs | 2 +- ...GenericCollectionAssertionOfStringSpecs.cs | 4 ++-- .../GenericDictionaryAssertionSpecs.cs | 12 +++++----- docs/_pages/releases.md | 2 ++ 9 files changed, 28 insertions(+), 26 deletions(-) diff --git a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs index ac952b7d12..ff75c7090d 100644 --- a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs +++ b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs @@ -1230,8 +1230,8 @@ public AndConstraint HaveCount(int expected, string because = "", p .ForCondition(actualCount == expected) .BecauseOf(because, becauseArgs) .FailWith( - "Expected {context:collection} {0} to contain {1} item(s){reason}, but found {2}.", - Subject, expected, actualCount); + "Expected {context:collection} to contain {0} item(s){reason}, but found {1}: {2}.", + expected, actualCount, Subject); } return new AndConstraint((TAssertions)this); @@ -1269,8 +1269,8 @@ public AndConstraint HaveCount(int expected, string because = "", p { Execute.Assertion .BecauseOf(because, becauseArgs) - .FailWith("Expected {context:collection} {0} to have a count {1}{reason}, but count is {2}.", - Subject, countPredicate.Body, actualCount); + .FailWith("Expected {context:collection} to have a count {0}{reason}, but count is {1}: {2}.", + countPredicate.Body, actualCount, Subject); } } @@ -1299,7 +1299,7 @@ public AndConstraint HaveCountGreaterThanOrEqualTo(int expected, st .Then .Given(subject => subject.Count()) .ForCondition(actualCount => actualCount >= expected) - .FailWith("but found {0}.", actualCount => actualCount) + .FailWith("but found {0}: {1}.", actualCount => actualCount, _ => Subject) .Then .ClearExpectation(); @@ -1331,7 +1331,7 @@ public AndConstraint HaveCountGreaterThan(int expected, string beca .Then .Given(subject => subject.Count()) .ForCondition(actualCount => actualCount > expected) - .FailWith("but found {0}.", actualCount => actualCount) + .FailWith("but found {0}: {1}.", actualCount => actualCount, _ => Subject) .Then .ClearExpectation(); @@ -1360,7 +1360,7 @@ public AndConstraint HaveCountLessThanOrEqualTo(int expected, strin .Then .Given(subject => subject.Count()) .ForCondition(actualCount => actualCount <= expected) - .FailWith("but found {0}.", actualCount => actualCount) + .FailWith("but found {0}: {1}.", actualCount => actualCount, _ => Subject) .Then .ClearExpectation(); @@ -1392,7 +1392,7 @@ public AndConstraint HaveCountLessThan(int expected, string because .Then .Given(subject => subject.Count()) .ForCondition(actualCount => actualCount < expected) - .FailWith("but found {0}.", actualCount => actualCount) + .FailWith("but found {0}: {1}.", actualCount => actualCount, _ => Subject) .Then .ClearExpectation(); @@ -2431,7 +2431,7 @@ public AndConstraint NotEqual(IEnumerable unexpected, string bec .Then .Given(subject => subject.ConvertOrCastToCollection()) .ForCondition(actualItems => !actualItems.SequenceEqual(unexpected)) - .FailWith("Did not expect collections {0} and {1} to be equal{reason}.", actualItems => unexpected, actualItems => actualItems); + .FailWith("Did not expect collections {0} and {1} to be equal{reason}.", _ => unexpected, actualItems => actualItems); return new AndConstraint((TAssertions)this); } @@ -2493,7 +2493,7 @@ public AndConstraint NotHaveCount(int unexpected, string because = .ForCondition(subject => !ReferenceEquals(subject, otherCollection)) .FailWith( "Expected {context:collection} {0} to not have the same count as {1}{reason}, but they both reference the same object.", - subject => subject, subject => otherCollection) + subject => subject, _ => otherCollection) .Then .Given(subject => (actual: subject.Count(), expected: otherCollection.Count())) .ForCondition(count => count.actual != count.expected) @@ -2532,13 +2532,13 @@ public AndConstraint NotHaveCount(int unexpected, string because = .ForCondition(subject => !ReferenceEquals(subject, otherCollection)) .FailWith( "Did not expect {context:collection} {0} to intersect with {1}{reason}, but they both reference the same object.", - subject => subject, subject => otherCollection) + subject => subject, _ => otherCollection) .Then .Given(subject => subject.Intersect(otherCollection)) .ForCondition(sharedItems => !sharedItems.Any()) .FailWith( "Did not expect {context:collection} to intersect with {0}{reason}, but found the following shared items {1}.", - sharedItems => otherCollection, sharedItems => sharedItems); + _ => otherCollection, sharedItems => sharedItems); return new AndConstraint((TAssertions)this); } diff --git a/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCount.cs b/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCount.cs index fc179a2ad8..fb9710a3c5 100644 --- a/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCount.cs +++ b/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCount.cs @@ -46,7 +46,7 @@ public void When_collection_has_a_count_that_is_different_from_the_number_of_ite // Assert action.Should().Throw() - .WithMessage("Expected collection*1*2*3* to contain 4 item(s) because we want to test the failure message, but found 3."); + .WithMessage("Expected collection to contain 4 item(s) because we want to test the failure message, but found 3: {1, 2, 3}."); } [Fact] @@ -70,7 +70,7 @@ public void When_collection_has_a_count_that_not_matches_the_predicate_it_should // Assert act.Should().Throw().WithMessage( - "Expected collection {1, 2, 3} to have a count (c >= 4) because a minimum of 4 is required, but count is 3."); + "Expected collection to have a count (c >= 4) because a minimum of 4 is required, but count is 3: {1, 2, 3}."); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountGreaterThan.cs b/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountGreaterThan.cs index 21970a52e5..627551d3fa 100644 --- a/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountGreaterThan.cs +++ b/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountGreaterThan.cs @@ -47,7 +47,7 @@ public void When_collection_has_a_count_greater_than_the_number_of_items_it_shou // Assert action.Should().Throw() - .WithMessage("*more than*3*because we want to test the failure message*3*"); + .WithMessage("Expected collection to contain more than 3 item(s) because we want to test the failure message, but found 3: {1, 2, 3}."); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountGreaterThanOrEqualTo.cs b/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountGreaterThanOrEqualTo.cs index 3e42d4625c..3d58081a19 100644 --- a/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountGreaterThanOrEqualTo.cs +++ b/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountGreaterThanOrEqualTo.cs @@ -46,7 +46,7 @@ public void When_collection_has_a_count_greater_than_or_equal_to_the_number_of_i // Assert action.Should().Throw() - .WithMessage("*at least*4*because we want to test the failure message*3*"); + .WithMessage("Expected collection to contain at least 4 item(s) because we want to test the failure message, but found 3: {1, 2, 3}."); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountLessThan.cs b/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountLessThan.cs index 15dd4f6285..121f52a30f 100644 --- a/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountLessThan.cs +++ b/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountLessThan.cs @@ -46,7 +46,7 @@ public void When_collection_has_a_count_less_than_the_number_of_items_it_should_ // Assert action.Should().Throw() - .WithMessage("*fewer than*3*because we want to test the failure message*3*"); + .WithMessage("Expected collection to contain fewer than 3 item(s) because we want to test the failure message, but found 3: {1, 2, 3}."); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountLessThanOrEqualTo.cs b/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountLessThanOrEqualTo.cs index f308100d53..e7858c6663 100644 --- a/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountLessThanOrEqualTo.cs +++ b/Tests/FluentAssertions.Specs/Collections/CollectionAssertionSpecs.HaveCountLessThanOrEqualTo.cs @@ -46,7 +46,7 @@ public void When_collection_has_a_count_less_than_or_equal_to_the_number_of_item // Assert action.Should().Throw() - .WithMessage("*at most*2*because we want to test the failure message*3*"); + .WithMessage("Expected collection to contain at most 2 item(s) because we want to test the failure message, but found 3: {1, 2, 3}."); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Collections/GenericCollectionAssertionOfStringSpecs.cs b/Tests/FluentAssertions.Specs/Collections/GenericCollectionAssertionOfStringSpecs.cs index da537fd411..828e14a0dd 100644 --- a/Tests/FluentAssertions.Specs/Collections/GenericCollectionAssertionOfStringSpecs.cs +++ b/Tests/FluentAssertions.Specs/Collections/GenericCollectionAssertionOfStringSpecs.cs @@ -1075,7 +1075,7 @@ public void // Assert action.Should().Throw() .WithMessage( - "Expected collection {\"one\", \"two\", \"three\"} to contain 4 item(s) because we want to test the failure message, but found 3."); + "Expected collection to contain 4 item(s) because we want to test the failure message, but found 3: {\"one\", \"two\", \"three\"}."); } [Fact] @@ -1089,7 +1089,7 @@ public void When_collection_has_a_count_that_not_matches_the_predicate_it_should // Assert act.Should().Throw().WithMessage( - "Expected collection {\"one\", \"two\", \"three\"} to have a count (c >= 4) because a minimum of 4 is required, but count is 3."); + "Expected collection to have a count (c >= 4) because a minimum of 4 is required, but count is 3: {\"one\", \"two\", \"three\"}."); } [Fact] diff --git a/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs index dcc60efcd1..a60f261f50 100644 --- a/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Collections/GenericDictionaryAssertionSpecs.cs @@ -172,7 +172,7 @@ public void // Assert action.Should().Throw() - .WithMessage("Expected dictionary {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"} to contain 4 item(s) because we want to test the failure message, but found 3."); + .WithMessage("Expected dictionary to contain 4 item(s) because we want to test the failure message, but found 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}."); } [Fact] @@ -206,7 +206,7 @@ public void When_dictionary_has_a_count_that_not_matches_the_predicate_it_should // Assert act.Should().Throw().WithMessage( - "Expected dictionary {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"} to have a count (c >= 4) because a minimum of 4 is required, but count is 3."); + "Expected dictionary to have a count (c >= 4) because a minimum of 4 is required, but count is 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}."); } [Fact] @@ -378,7 +378,7 @@ public void When_dictionary_has_a_count_greater_than_the_number_of_items_it_shou // Assert action.Should().Throw() - .WithMessage("*more than*3*because we want to test the failure message*3*"); + .WithMessage("Expected dictionary to contain more than 3 item(s) because we want to test the failure message, but found 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}."); } [Fact] @@ -447,7 +447,7 @@ public void When_dictionary_has_a_count_greater_than_or_equal_to_the_number_of_i // Assert action.Should().Throw() - .WithMessage("*at least*4*because we want to test the failure message*3*"); + .WithMessage("Expected dictionary to contain at least 4 item(s) because we want to test the failure message, but found 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}."); } [Fact] @@ -516,7 +516,7 @@ public void When_dictionary_has_a_count_less_than_the_number_of_items_it_should_ // Assert action.Should().Throw() - .WithMessage("*fewer than*3*because we want to test the failure message*3*"); + .WithMessage("Expected dictionary to contain fewer than 3 item(s) because we want to test the failure message, but found 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}."); } [Fact] @@ -585,7 +585,7 @@ public void When_dictionary_has_a_count_less_than_or_equal_to_the_number_of_item // Assert action.Should().Throw() - .WithMessage("*at most*2*because we want to test the failure message*3*"); + .WithMessage("Expected dictionary to contain at most 2 item(s) because we want to test the failure message, but found 3: {[1] = \"One\", [2] = \"Two\", [3] = \"Three\"}."); } [Fact] diff --git a/docs/_pages/releases.md b/docs/_pages/releases.md index a3d64705a0..f3b0f81e3b 100644 --- a/docs/_pages/releases.md +++ b/docs/_pages/releases.md @@ -12,8 +12,10 @@ sidebar: ### What's New * Adding `ThatAreAsync()` and `ThatAreNotAsync()` for filtering in method assertions - [#1725](https://github.com/fluentassertions/fluentassertions/pull/1725) * Adding `ThatAreVirtual()` and `ThatAreNotVirtual()` for filtering in method assertions - [#1744](https://github.com/fluentassertions/fluentassertions/pull/1744) +* Adding collection content to assertion messages for `HaveCountGreaterThan()`, `HaveCountGreaterThanOrEqualTo()`, `HaveCountLessThan()` and `HaveCountLessThanOrEqualTo()` - [#1760](https://github.com/fluentassertions/fluentassertions/pull/1760) ### Fixes * Prevent multiple enumeration of `IEnumerable`s in parameter-less `ContainSingle()` - [#1753](https://github.com/fluentassertions/fluentassertions/pull/1753) +* Change `HaveCount()` assertion message order to state expected and actual collection count before dumping its content` - [#1760](https://github.com/fluentassertions/fluentassertions/pull/1760) ## 6.2.0