Skip to content
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

BeProperSubsetOf and BeProperSupersetOf #2363

Open
Meir017 opened this issue Oct 5, 2023 · 7 comments · May be fixed by #2432
Open

BeProperSubsetOf and BeProperSupersetOf #2363

Meir017 opened this issue Oct 5, 2023 · 7 comments · May be fixed by #2432
Assignees
Labels
api-approved API was approved, it can be implemented

Comments

@Meir017
Copy link
Member

Meir017 commented Oct 5, 2023

Background and motivation

Asserting proper subset and proper superset on collections

proper subset - a collection is included in a superset collection but not equivalent to the superset collection
proper superset - a collection is contains a subset collection but not equivalent to the subset collection

API Proposal

GenericCollectionAssertions<TCollection, T, TAssertions>
{
    public AndConstraint<TAssertions> BeProperSubsetOf(IEnumerable<T> expectedSuperset, string because = "", params object[] becauseArgs);
    public AndConstraint<TAssertions> BeProperSupersetOf(IEnumerable<T> expectedSubset, string because = "", params object[] becauseArgs);
}

API Usage

ISet<int> subset = new HashSet<int>{ 1, 2, 3 };
ISet<int> superset = new HashSet<int>{ 2, 3, 4 };

subset.Should().BeProperSubsetOf(new[] { 1, 2, 3 }); // will throw
subset.Should().BeProperSubsetOf(new[] { 1, 2, 3, 4 }); // will pass

superset.Should().BeProperSupersetOf(new[] { 2, 3, 4 }); // will throw
superset.Should().BeProperSupersetOf(new[] { 2, 3, 4, 5 }); // will pass

Alternative Designs

subset.Should().BeSubsetOf(new[] { 1, 2, 3 }).And.NotBeEquivalentTo(new[] { 1, 2, 3 }); // for ProperSubset
new[] { 2, 3, 4 }.Should().BeSubsetOf(superset).And.NotBeEquivalentTo(superset); // for ProperSuperset

Risks

No response

Are you willing to help with a proof-of-concept (as PR in that or a separate repo) first and as pull-request later on?

Yes

@Meir017 Meir017 added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Oct 5, 2023
@jnyrup jnyrup changed the title [API Proposal]: [API Proposal]: BeProperSubsetOf and BeProperSupersetOf Oct 5, 2023
@dennisdoomen dennisdoomen changed the title [API Proposal]: BeProperSubsetOf and BeProperSupersetOf BeProperSubsetOf and BeProperSupersetOf Oct 6, 2023
@dennisdoomen dennisdoomen changed the title BeProperSubsetOf and BeProperSupersetOf BeProperSubsetOf and BeProperSupersetOf Oct 6, 2023
@dennisdoomen
Copy link
Member

dennisdoomen commented Oct 6, 2023

I like it, although it is a bit niche. What about you @jnyrup ?

@jnyrup
Copy link
Member

jnyrup commented Oct 8, 2023

I like it, although it is a bit niche. What about you @jnyrup ?

I'm also quite niche.
I like set theory, so I've been waiting for years for someone else to also propose this.

Should we also add BeSupersetOf for symmetry even though that would just be forwarding to the existing Contain?

public AndConstraint<TAssertions> Contain(IEnumerable<T> expected, string because = "", params object[] becauseArgs)

@Meir017
Copy link
Member Author

Meir017 commented Oct 9, 2023

@jnyrup maybe adding BeSupersetOf could be added separately (different PR)?

@dennisdoomen
Copy link
Member

Assuming you include BeSupersetOf, I'm fine with this proposal. If @jnyrup is too, it's approved.

@jnyrup
Copy link
Member

jnyrup commented Oct 15, 2023

I'm good with it.

@dennisdoomen dennisdoomen added api-approved API was approved, it can be implemented and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Oct 15, 2023
@dennisdoomen
Copy link
Member

Can we assign this one to you @Meir017 ?

@Meir017
Copy link
Member Author

Meir017 commented Nov 1, 2023

@dennisdoomen yes please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved, it can be implemented
Projects
None yet
3 participants