Skip to content

Commit

Permalink
Change HaveCount assertion message order to state number before dumpi… (
Browse files Browse the repository at this point in the history
#1760)

Change HaveCount assertion message order to state number before dumping object contents
  • Loading branch information
iliashkolyar committed Dec 28, 2021
1 parent c55a7a4 commit f6c65c1
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 26 deletions.
24 changes: 12 additions & 12 deletions Src/FluentAssertions/Collections/GenericCollectionAssertions.cs
Expand Up @@ -1230,8 +1230,8 @@ public AndConstraint<TAssertions> 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>((TAssertions)this);
Expand Down Expand Up @@ -1269,8 +1269,8 @@ public AndConstraint<TAssertions> 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);
}
}

Expand Down Expand Up @@ -1299,7 +1299,7 @@ public AndConstraint<TAssertions> 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();

Expand Down Expand Up @@ -1331,7 +1331,7 @@ public AndConstraint<TAssertions> 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();

Expand Down Expand Up @@ -1360,7 +1360,7 @@ public AndConstraint<TAssertions> 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();

Expand Down Expand Up @@ -1392,7 +1392,7 @@ public AndConstraint<TAssertions> 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();

Expand Down Expand Up @@ -2431,7 +2431,7 @@ public AndConstraint<TAssertions> NotEqual(IEnumerable<T> 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>((TAssertions)this);
}
Expand Down Expand Up @@ -2493,7 +2493,7 @@ public AndConstraint<TAssertions> 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)
Expand Down Expand Up @@ -2532,13 +2532,13 @@ public AndConstraint<TAssertions> 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>((TAssertions)this);
}
Expand Down
Expand Up @@ -46,7 +46,7 @@ public void When_collection_has_a_count_that_is_different_from_the_number_of_ite

// Assert
action.Should().Throw<XunitException>()
.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]
Expand All @@ -70,7 +70,7 @@ public void When_collection_has_a_count_that_not_matches_the_predicate_it_should

// Assert
act.Should().Throw<XunitException>().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]
Expand Down
Expand Up @@ -47,7 +47,7 @@ public void When_collection_has_a_count_greater_than_the_number_of_items_it_shou

// Assert
action.Should().Throw<XunitException>()
.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]
Expand Down
Expand Up @@ -46,7 +46,7 @@ public void When_collection_has_a_count_greater_than_or_equal_to_the_number_of_i

// Assert
action.Should().Throw<XunitException>()
.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]
Expand Down
Expand Up @@ -46,7 +46,7 @@ public void When_collection_has_a_count_less_than_the_number_of_items_it_should_

// Assert
action.Should().Throw<XunitException>()
.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]
Expand Down
Expand Up @@ -46,7 +46,7 @@ public void When_collection_has_a_count_less_than_or_equal_to_the_number_of_item

// Assert
action.Should().Throw<XunitException>()
.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]
Expand Down
Expand Up @@ -1075,7 +1075,7 @@ public void
// Assert
action.Should().Throw<XunitException>()
.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]
Expand All @@ -1089,7 +1089,7 @@ public void When_collection_has_a_count_that_not_matches_the_predicate_it_should

// Assert
act.Should().Throw<XunitException>().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]
Expand Down
Expand Up @@ -172,7 +172,7 @@ public void

// Assert
action.Should().Throw<XunitException>()
.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]
Expand Down Expand Up @@ -206,7 +206,7 @@ public void When_dictionary_has_a_count_that_not_matches_the_predicate_it_should

// Assert
act.Should().Throw<XunitException>().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]
Expand Down Expand Up @@ -378,7 +378,7 @@ public void When_dictionary_has_a_count_greater_than_the_number_of_items_it_shou

// Assert
action.Should().Throw<XunitException>()
.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]
Expand Down Expand Up @@ -447,7 +447,7 @@ public void When_dictionary_has_a_count_greater_than_or_equal_to_the_number_of_i

// Assert
action.Should().Throw<XunitException>()
.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]
Expand Down Expand Up @@ -516,7 +516,7 @@ public void When_dictionary_has_a_count_less_than_the_number_of_items_it_should_

// Assert
action.Should().Throw<XunitException>()
.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]
Expand Down Expand Up @@ -585,7 +585,7 @@ public void When_dictionary_has_a_count_less_than_or_equal_to_the_number_of_item

// Assert
action.Should().Throw<XunitException>()
.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]
Expand Down
2 changes: 2 additions & 0 deletions docs/_pages/releases.md
Expand Up @@ -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

Expand Down

0 comments on commit f6c65c1

Please sign in to comment.