diff options
Diffstat (limited to 'src/test/scala/helpers.scala')
-rw-r--r-- | src/test/scala/helpers.scala | 51 |
1 files changed, 38 insertions, 13 deletions
diff --git a/src/test/scala/helpers.scala b/src/test/scala/helpers.scala index f4d7ceb..f2f561c 100644 --- a/src/test/scala/helpers.scala +++ b/src/test/scala/helpers.scala @@ -1,15 +1,40 @@ -import org.scalatest.FunSuite - -import router.Router - -trait RouterHelpers { this: FunSuite => - def testRoute (router: Router[Boolean], path: String, mapping: Map[String, String]) { - assert(path === router.uriFor(mapping).get) - val om = router.route(path) - assert(om.isDefined) - val m = om.get - assert(m.mapping.size == mapping.size) - assert(m.mapping.forall { case (k, v) => mapping(k) == v }) - assert(m.target === true) +package router + +object test { + import language.implicitConversions + + class RouterHelperOps[T] (router: Router[T]) { + private def assert (condition: Boolean, msg: String) = { + if (condition) { None } else { Some(msg) } + } + + def matches (path: String) = { + assert(router.route(path).isDefined, s"route failed to match $path") + } + + def matches (path: String, mapping: Map[String, String]) = { + (router.uriFor(mapping), router.route(path)) match { + case (Some(uriFor), Some(m)) => { + None.orElse({ + assert(uriFor == path, s"uriFor returned $uriFor, expected $path") + }).orElse({ + assert( + m.mapping.size == mapping.size && + m.mapping.forall { case (k, v) => mapping(k) == v }, + s"route returned ${m.mapping}, expected $mapping" + ) + }) + } + case (None, None) => + Some(s"uriFor and route both failed to match $path") + case (None, _) => + Some(s"uriFor failed to match $path") + case (_, None) => + Some(s"route failed to match $path") + } + } } + + implicit def routerToOps[T] (router: Router[T]): RouterHelperOps[T] = + new RouterHelperOps(router) } |