- Creating a Specs2 matcher in a modular way
- type TF = A => Double
- (f: TF) must computeSameResultsAs(g: TF,tolerance: Double, tests: Set[A])
- (f: TF) must computeSameResultsAs(g: TF)
- .withTolerance(tolerance)
- .onValues(tests: Set[A])
- def computeSameResultsAs[A](ref: A => Double, tolerance: Double, args: Set[A]): Matcher[A => Double] =
- args.map(beCloseOnArg(ref, tolerance, _)).reduce(_ and _)
- def beCloseOnArg[A](ref: A => Double, tolerance: Double, arg: A): Matcher[A => Double] =
- closeTo(ref(arg), tolerance) ^^ ((_: A => Double).apply(arg))
- def beCloseOnArg[A](ref: A => Double, tolerance: Double, arg: A): Matcher[A => Double] =
- closeTo(ref(arg), tolerance) ^^ ((_: A => Double).apply(arg) aka "result on argument " + arg)
- def computeSameResultsAs[A](g: A => Double,
- tolerance: Double = 0.0,
- values: Seq[A] = Seq()) = TFMatcher(g, tolerance, values)
- case class TFMatcher[A](g: A => Double,
- tolerance: Double = 0.0,
- values: Seq[A] = Seq()) extends Matcher[A => Double] {
- def apply[S <: A => Double](f: Expectable[S]) = {
- // see definition below
- }
- def withTolerance(t: Double) = TFMatcher(g, t, values)
- def onValues(tests: A*) = TFMatcher(g, tolerance, tests)
- }
- val f = (i: Int) => i.toDouble
- val g = (i: Int) => i.toDouble + 0.1
- "f must be close to another similar function with a tolerance" in {
- f must computeSameResultsAs[Int](g).withTolerance(0.5).onValues(1, 2, 3)
- }
- def apply[S <: A => Double](f: Expectable[S]) = {
- val res = ((v: A) => beCloseTo(g(v) +/- tolerance).apply(theValue(f.value(v)))).forall(values)
- val message = "f is "+(if (res.isSuccess) "" else "not ")+
- "close to g with a tolerance of "+tolerance+" "+
- "on values "+values.mkString(",")+": "+res.message
- result(res.isSuccess, message, message, f)
- }
- ((v: A) => beCloseTo(g(v) +/- tolerance).apply(theValue(f.value(v)))).forall(values)
- val f = (i: Int) => i.toDouble
- val g = (i: Int) => i.toDouble + 1.0
- "f must be close to another similar function with a tolerance" in {
- f must computeSameResultsAs[Int](g, tolerance = 0.5, values = Seq(1, 2, 3))
- }
- def computeSameResultsAs[A](ref: A => Double, tolerance: Double, values: Seq[A]): Matcher[A => Double] = (f: A => Double) => {
- verifyFunction((a: A) => (beCloseTo(ref(a) +/- tolerance)).apply(theValue(f(a)))).forall(values)
- }
- In the sequence '1, 2, 3', the 1st element is failing: 1.0 is not close to 2.0 +/- 0.5
- implicit def functionResultToMatcher[T](f: T => MatchResult[_]): Matcher[T] = (t: T) => {
- val result = f(t)
- (result.isSuccess, result.message)
- }
- 1.0 is not close to 2.0 +/- 0.5; 2.0 is not close to 3.0 +/- 0.5; 3.0 is not close to 4.0 +/- 0.5
- def computeSameResultsAs[A](ref: A => Double, tolerance: Double, values: Seq[A]): Matcher[A => Double] = (f: A => Double) => {
- ((a: A) => beCloseTo(ref(a) +/- tolerance) ^^ f).forall(values)
- }
- def computeSameResultsAs[A](ref: A => Double, tolerance: Double, values: Seq[A]): Matcher[A => Double] = (f: A => Double) => {
- ((a: A) => beCloseTo(ref(a) +/- tolerance) ^^ ((a1: A) => f(a) aka "the value")).forall(values)
- }
- In the sequence '1, 2, 3', the 1st element is failing: the value '1.0' is not close to 2.0 +/- 0.5