8000 added is_subset_of() for testing set comparison by mario-campos · Pull Request #48 · assertpy/assertpy · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@mario-campos
Copy link

Currently, the way to test for subsets is a little roundabout:

assert_that(set([1,2,3]).isssubset(set([1,2,3,4]))).is_true()

With this Pull Request, it's a bit cleaner:

assert_that(set([1,2,3])).is_subset_of(set([1,2,3,4]))

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.5%) to 97.819% when pulling 2a7b7da on mario-campos:is_subset_of into 791bdfa on ActivisionGameScience:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.5%) to 97.819% when pulling 78114e6 on mario-campos:is_subset_of into 791bdfa on ActivisionGameScience:master.

@mario-campos
Copy link
Author

Regarding the 98% code coverage, that was reduced by 1.5% because I added more code to the code base, but if you examine closely, you'll see my additions are covered by my unit tests.

And regarding the Travis-CI error, you'll see the test cases pass 3/4 times. The one that failed was due to a failure in curl, not the test case itself. I think that, since the Python 3.4 tests passed, the Python 3.5 tests will pass as well.

@saturnboy
Copy link
Contributor

@mario-campos the is_in() and is_not_in() assertions exist perhaps these should be extended to support lists and sets.

This already works:

assert_that(1).is_in(0,1,2,3)
assert_that(1).is_not_in(-1,-2,-3)

Extend to lists and sets?

assert_that([1,2,3]).is_in([1,2,3,4])
assert_that(set([1,2,3])).is_in(set([1,2,3,4]))

@mario-campos
Copy link
Author

Yeah, that'll work too. I'll commit some more.

@mario-campos
Copy link
Author

I tried to extend is_in() and is_not_in(), but I ended up with a dilemma that I'm not sure how to solve. As it stands, is_in() is used to determine whether an item (val) is in a collection/tuple (items). Without the proposed extension, this assertion would be false:

assert_that({1,2,3}).is_in({}, {1}, {1,2}, {1,2,3,4})

because {1,2,3} is not equal (==) to any of those. However, with the extension, that same assertion would be true, since {1,2,3} is a subset of {1,2,3,4} and {1,2,3}.issubset({1,2,3,4}) evaluates to True.

The same can be said for lists: does this evaluate to true or false?

assert_that([1,2,3]).is_in([], [1,2], [1,2,3,4])

False if you're expecting [1,2,3] in ([], [1,2], [1,2,3,4]). True if you understand [1,2,3] as a slice of [1,2,3,4].

I think, by conflating is_in() for these two purposes, you may end up with ambiguous results. Maybe is_subset_of() would be better after all?

@saturnboy saturnboy closed this in 7000f23 Apr 5, 2016
@saturnboy
Copy link
Contributor

@mario-campos agreed, went back to is_subset_of()

Works on anything iterable:

assert_that(['a','b','c']).is_subset_of(['a','b','c','d'])
assert_that((1,2,3)).is_subset_of((1,2,3,4))
assert_that('foo').is_subset_of('abcdefghijklmnopqrstuvwxyz')
assert_that({1,2,3}).is_subset_of({1,2,3,4})

And if given multiple superset args, they are flattened together:

assert_that(['a','b','c']).is_subset_of('a', ['b','c'])
assert_that({1,2,3}).is_subset_of({1,2}, {3,4})

@mario-campos mario-campos deleted the is_subset_of branch April 5, 2016 19:04
@mario-campos
Copy link
Author

Thanks!

@saturnboy saturnboy added this to the v1.0 milestone Apr 8, 2016
@saturnboy saturnboy self-assigned this Apr 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0