Sorry for the delay in responding to this. I for some reason didn't
get notified by Google Groups that it was pending. I only found it
yesterday. The link to your goals isn't working. Can you send another
link?
Thanks.
Bill
> --
> You received this message because you are subscribed to the Google
> Groups "scalatest-users" group.
> To post to this group, send email to scalate...@googlegroups.com
> To unsubscribe from this group, send email to
> scalatest-use...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/scalatest-users?hl=en
> ScalaTest itself, and documentation, is available here:
> http://www.artima.com/scalatest
--
Bill Venners
Artima, Inc.
http://www.artima.com
Can you submit a couple examples of how you'd like the resulting code
to look? In other words, once your custom matcher is done, how would
someone use it?
Thanks.
Bill
Here's an interpreter session showing one way to do this:
scala> import org.scalatest._
import org.scalatest._
scala> import matchers.ShouldMatchers._
import matchers.ShouldMatchers._
scala> import matchers.Matcher
import matchers.Matcher
scala> import matchers.MatchResult
import matchers.MatchResult
scala> val firstArray = Array.tabulate[Double](6){ index => index -
0.00001 }.tail
firstArray: Array[Double] = Array(0.99999, 1.99999, 2.99999, 3.99999, 4.99999)
scala> val secondArray = Array.tabulate[Double](6){ index => index * 1.0 }.tail
secondArray: Array[Double] = Array(1.0, 2.0, 3.0, 4.0, 5.0)
scala> val thirdArray = Array.tabulate[Double](6){ index => index -
0.00002 }.tail
thirdArray: Array[Double] = Array(0.99998, 1.99998, 2.99998, 3.99998, 4.99998)
scala> val tolerance = 1e-5
tolerance: Double = 1.0E-5
scala> def pretty(a: Array[Double]) = a.mkString("Array(", ",", ")")
pretty: (a: Array[Double])String
scala> def equalWithTolerance(right: Array[Double], tol: Double) =
| Matcher { (left: Array[Double]) =>
| MatchResult(
| (left zip right) forall { case (a, b) => a <= b + tol &&
a >= b - tol },
| pretty(left) + " did not equal " + pretty(right) + " with
tolerance " + tol,
| pretty(left) + " equaled " + pretty(right) + " with
tolerance " + tol
| )
| }
equalWithTolerance: (right: Array[Double], tol:
Double)org.scalatest.matchers.Matcher[Array[Double]]
scala> firstArray should equalWithTolerance (secondArray, tolerance)
scala> firstArray should not (equalWithTolerance (secondArray, tolerance))
org.scalatest.TestFailedException:
Array(0.99999,1.99999,2.99999,3.99999,4.99999) equaled
Array(1.0,2.0,3.0,4.0,5.0) with tolerance 1.0E-5
at org.scalatest.matchers.Matchers$class.newTestFailedException(Matchers.scala:150)
...
scala> thirdArray should equalWithTolerance (secondArray, tolerance)
org.scalatest.TestFailedException:
Array(0.99998,1.99998,2.99998,3.99998,4.99998) did not equal
Array(1.0,2.0,3.0,4.0,5.0) with tolerance 1.0E-5
at org.scalatest.matchers.Matchers$class.newTestFailedException(Matchers.scala:150)
....
scala> thirdArray should not (equalWithTolerance (secondArray, tolerance))
You have to prettify the array toString because otherwise you get the
default Java toString. Possibly you'd want to make the error message
more descriptive by identifying the initial failing one, or all the
failing ones perhaps. Otherwise it might be hard to see which element
or elements are out of tolerance.
Bill
Yes, very good idea. I didn't realize such an example wasn't in the
docs. I'll add it.
Bill