object Functions {
implicit def nameIt[From, To](f: From => To) = new {
def named(name: String) = new Function[From, To](name, f)
}
}
//...
class ValidatingFunction[X, Y](name: String, f: X => Y) extends CheckThis[X, Y] {
private def expectMatch[X, Y](f: X => Y)(x: X, y: Y) {
f(x) aka (name + "(" + x + ")=" + y) must_== y
}
def maps(samples:(X, Y)*) {
check(expectMatch(f), samples:_*)
}
}
implicit def funValidates[X, Y](f: Function[X, Y]) = new ValidatingFunction[X, Y](f.name, f)
//... and in the tests:
"SomeWeirdFunction()" should {
"do its job properly" in {
(SomeWeirdFunction _) named "SomeWeirdFunction" maps (
"1234500000" -> "1234500000",
"012" -> "12",
"0000000440" -> "440",
"00000000" -> "",
"" -> "",
"the end" -> "the end")
}
}