Specs2: How to turn Seq [Matcher [A]] into one Matcher [A]?

Given the sequence Seq[Matcher[A]] , I want to get a single Matcher[A] that will be successful when all matches within the sequence are successful.

Edit

The answer provided by me seems a bit awkward, and besides, it would be nice if all the unsuccessful sequence helpers would produce a result

+4
source share
2 answers

Ok, I found a way:

 (matchers: Seq[Matcher[A]]).reduce(_ and _) 

Somehow I thought that there should be another way, for example to write _.sequence .

+1
source

The problem with creating a new match from the pairing sequence is that it becomes harder to find which match failed.

The best option, in my opinion, is to compare with each match separately like this:

 val matchers: Seq[Matcher[Boolean]] = Seq( ((_: Boolean).equals(false), "was true 1"), ((_: Boolean).equals(true), "was false 2"), ((_: Boolean).equals(true), "was false 3") ) "work with matcher sequence" in { matchers.foreach(beMatching => false must beMatching) } 

You can see in the output that the responders are called separately, and the first failure causes a test failure with the message of this connector.

Depending on what you have, it may even be better to generate expectations for each match, so it will do them all and show you the correct overview, not just the first glitch. I have not gone so far (yet).

+1
source

All Articles