Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Specs2 compare sequence elements with one of the existing matchers?

Tags:

scala

specs2

I want to compare 2 sequences of doubles in a specs2 test, something along the lines of:

actualValues must containTheSameElementsAs(expectedValues, _ beCloseTo _)

I can write a manual comparison like

actualValues must containTheSameElementsAs(expectedValues, (a, b) => math.abs(a - b) < 0.001)

but that seems a bit pointless as beCloseTo is already available.

On a wider note, is there a good source of documentation for Specs2? I looked at the user guide but the search in that for "containsAllOf" shows no results and the matchers section doesn't have any entry about collections as far as I can tell.

like image 457
danio Avatar asked Jan 27 '26 10:01

danio


1 Answers

Most of collection matches should be expressible with contain + combinators. In this case you can write

List(1.3, 1.7) must contain(beCloseTo(1.5 +/- 0.5)).forall

where forall tests each element. You could replace forall with atLeastOnce if you wanted to test that only one element satisfies your property.

You could also write it like this if you want the "forall" word to appear sooner in your statement

forall(List(1.0, 2.0)) { n =>
  n must beCloseTo(1.5 +/- 0.5)
}

This comes from the MatcherImplicits trait mixed into the Specification but I agree, this is not well-documented.

like image 170
Eric Avatar answered Jan 30 '26 11:01

Eric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!