New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Add collections methods BeProperSubsetOf / BeProperSupersetOf / BeSupersetOf #2432
base: develop
Are you sure you want to change the base?
Feature: Add collections methods BeProperSubsetOf / BeProperSupersetOf / BeSupersetOf #2432
Conversation
Qodana for .NET4 new problems were found
💡 Qodana analysis was run in the pull request mode: only the changed files were checked View the detailed Qodana reportTo be able to view the detailed Qodana report, you can either:
To get - name: 'Qodana Scan'
uses: JetBrains/qodana-action@v2023.2.8
with:
upload-result: true Contact Qodana teamContact us at qodana-support@jetbrains.com
|
5f23897
to
398bb8f
Compare
Pull Request Test Coverage Report for Build 6756573228
💛 - Coveralls |
These methods come from set theory, but work on var subject = new []{1, 1, 1, 2, 2, 3, 3};
subject.Should().BeProperSubsetOf(new []{1, 2, 3, 3, 3, 4}); Should we add test cases for it? |
of ICollection
@vbreuss done! thanks for the suggestion |
Src/FluentAssertions/Collections/GenericCollectionAssertions.cs
Outdated
Show resolved
Hide resolved
Src/FluentAssertions/Collections/GenericCollectionAssertions.cs
Outdated
Show resolved
Hide resolved
AssertBeSubsetOf(expectedProperSuperset, "proper subset", because, becauseArgs); | ||
|
||
ISet<T> expectedItems = expectedProperSuperset.ConvertOrCastToSet(); | ||
|
||
if (expectedItems.Intersect(Subject).Count() == expectedItems.Count) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this cause multiple enumeration of either Subject
or expectedProperSuperset
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, maybe we should create an ISet
and an ICollection
for the Subject
and expected
values?
"Expected superset to be a proper superset of {4, 3, 2, 1} because we want to test the failure message, " + | ||
"but items {1, 1, 1, 2, 2, 3, 3, 4} are equivalent to the superset"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we print the non-set version of expected?
This might be useful when object.Equals(object)
and object.ToString()
are not aligned as they are for int
, i.e. where ToString()
includes more/different information than what Equals
use to compare instances.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, maybe we should create an ISet
and an ICollection
for the Subject
and expected
values?
then we could print both versions of the values, both the unique values and the raw values,
does this make sense?
Src/FluentAssertions/Collections/GenericCollectionAssertions.cs
Outdated
Show resolved
Hide resolved
@@ -41,6 +41,9 @@ collection.Should().EndWith(8); | |||
collection.Should().EndWith(new[] { 5, 8 }); | |||
|
|||
collection.Should().BeSubsetOf(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, }); | |||
collection.Should().BeProperSubsetOf(new[] { 1, 2, 5, 6, 7, 8 }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I would add some comments here to explain the difference (again) between a normal subset/superset and a proper one.
* Introduced new collection assertions methods - [#2432](https://github.com/fluentassertions/fluentassertions/pull/2432) | ||
* `BeProperSubsetOf` | ||
* `BeProperSupersetOf` | ||
* `BeSupersetOf` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Introduced new collection assertions methods - [#2432](https://github.com/fluentassertions/fluentassertions/pull/2432) | |
* `BeProperSubsetOf` | |
* `BeProperSupersetOf` | |
* `BeSupersetOf` | |
* Introduced new collection assertions methods `BeProperSubsetOf`, `BeProperSupersetOf` and `BeSupersetOf - [#2432](https://github.com/fluentassertions/fluentassertions/pull/2432) | |
* `BeSupersetOf` |
{ | ||
Execute.Assertion | ||
.BecauseOf(because, becauseArgs) | ||
.FailWith("Expected {context:collection} to be a proper subset of {0}{reason}, but items {1} are equivalent to the subset", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Can we make the last part of the failure clearer?
@@ -3262,6 +3298,62 @@ public AndConstraint<TAssertions> StartWith(T element, string because = "", para | |||
return StartWith(new[] { element }, ObjectExtensions.GetComparer<T>(), because, becauseArgs); | |||
} | |||
|
|||
internal void AssertContainment(IEnumerable<T> expected, string containmentType, string because = "", params object[] becauseArgs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙃 We prefer to keep private methods just below the last usage.
🔧 Make it private
public class BeProperSubsetOf | ||
{ | ||
[Fact] | ||
public void When_collection_is_proper_subset_of_a_specified_collection_it_should_not_throw() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔧 We prefer to avoid using terms like when and should.
resolves #2363
IMPORTANT
./build.sh --target spellcheck
or.\build.ps1 --target spellcheck
before pushing and check the good outcome