How to test for Set subset containment, using matchers?

1,368 views
Skip to first unread message

Asko Kauppi

unread,
Nov 8, 2014, 1:31:24 PM11/8/14
to scalate...@googlegroups.com
I don't think it can be done in ScalaTest 2.2.1, without making custom matchers. 

I think this is such a common scenario that I would appreciate ScalaTest providing built-in support for it.

The current alternatives I've seen (which work) are:

    assert( set.subsetOf( wanted_set ) )

But this uses assert, not matchers. I prefer matchers to show which things are being tested (assert I use for things that should really, always, be true). 

For lists:

    list should contain allOf ( wanted_list.head, wanted_list.tail.head, wanted_list.tail.tail :_* )

This is because the 'contain allOf' matcher doesn't work with Seq, but with varargs of 2 or more entries.

What would be a better way, using ScalaTest?

There's also an SO entry about this (feel welcome responding either on the mailing list, or in SO).


If this seems like an API omission in ScalaTest, I should probably make an issue, but I'm still thinking there must be a matcher for this?

- Asko Kauppi

Bill Venners

unread,
Nov 8, 2014, 2:36:28 PM11/8/14
to scalate...@googlegroups.com
Hi Asko,

Yes, we didn't want to hold up the 2.0 release to add that, but have
since added it. I believe we added it to master, though, not the 2.2.x
branch. Regardless, the syntax looks like:

scala> val xSet = Set(1, 2, 3, 4, 5)
xSet: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> val ySet = Set(3, 4)
ySet: scala.collection.immutable.Set[Int] = Set(3, 4)

scala> xSet should contain allElementsOf (ySet)

scala> ySet should contain allElementsOf (xSet)
org.scalatest.exceptions.TestFailedException: Set(3, 4) did not
contain all elements of List(5, 1, 2, 3, 4)
at org.scalatest.MatchersHelper$.newTestFailedException(MatchersHelper.scala:160)
at org.scalatest.words.ResultOfContainWord.allElementsOf(ResultOfContainWord.scala:296)
... 43 elided


Looks like we magically converted the set to a List somewhere, but you
get the idea. Essentially we added an *ElementsOf variant for all the
contain syntax, allOf => allElementsOf, oneOf => oneElementOf, etc.
Will be in 3.0.

Bill
> --
> You received this message because you are subscribed to the Google
> Groups "scalatest-users" group.
> To post to this group, send email to scalate...@googlegroups.com
> To unsubscribe from this group, send email to
> scalatest-use...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/scalatest-users?hl=en
> ScalaTest itself, and documentation, is available here:
> http://www.artima.com/scalatest
> ---
> You received this message because you are subscribed to the Google Groups
> "scalatest-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scalatest-use...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Bill Venners
Artima, Inc.
http://www.artima.com

Asko Kauppi

unread,
Nov 8, 2014, 3:41:32 PM11/8/14
to scalate...@googlegroups.com
That's great news, Bill!

- asko
Reply all
Reply to author
Forward
0 new messages