I was looking for a ScalaTest matcher to check that a list contains all of the needed elements (given within another list), but that may also be others.
val list = List(1,2,3,4)
val wanted = List(1,2,3)
list should contain allOf ( wanted.head, wanted.tail.head, wanted.tail.tail :_* ) // ugly workaround
val list = List.empty[String]
//list should contain allOf("a") // does not compile
list should contain allOf("a","b")
list should contain allOf("a","b","c")
val wanted = List("a","b","c")
//list should contain allOf( wanted ) // does not compile
list should contain allOf( wanted.head, wanted.tail ) // compiles, but tests the wrong thing; against List(head,List(tail))
wanted.foreach( list should contain(_) )
Bill Venners had this to say on the ScalaTest mailing list:
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:
xSet should contain allElementsOf (ySet)
Link to the message.