Question

Specs2 does provide a containsAllOf matcher but I can't get it to work the way I want. I have two Strings A and B, and I want to test that all Chars that occur in B are present in A.

My best try so far was

A.toCharArray.toSeq must containAllOf(B.toCharArray.toSeq)

But this fails with errors like

WrappedArray(a, b, c, d, ...) does not contain S, a, V, H, I, ... and must not contain ...

  1. The WrappedArray does contain the characters it allegedly doesn't according to Specs2
  2. Why is there a test for elements A must not contain? I don't want ot test equality, I want to test if B is a "subset" of A (but not in the strict set-theoretic definition of set)

How can I write a working spec for this scenario?

Was it helpful?

Solution 2

Currently, I've chosen this variant:

B.toSeq.map(c => A must contain(c))

I'm still open to suggestions, but if there is no cleaner solution I might (for the first time) answer my own question.

Or is this one of the cases where I suspect Specs2 to test a thing, when it actually doesn't?

OTHER TIPS

This does not use containsAllOf but should work:

/* turn A into a Map for quick contains lookup */
val theAs = A.map(x=> (x,x)).toMap

/* if resulting list has length greater than 0, 
 * then there is something in A that is not in B*/
B.filterNot(theAs.contains).length must === 0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top