From 287656ae92ba8a2fc8aaeca6fd4d1a9aba133ec5 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 15 Feb 2013 16:41:06 -0600 Subject: try out a different testing interface --- src/test/scala/basic.scala | 35 ++++++++++++++--------------- src/test/scala/helpers.scala | 51 ++++++++++++++++++++++++++++++++----------- src/test/scala/optional.scala | 31 +++++++++++++------------- 3 files changed, 72 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/test/scala/basic.scala b/src/test/scala/basic.scala index 57a58b8..56655e4 100644 --- a/src/test/scala/basic.scala +++ b/src/test/scala/basic.scala @@ -1,8 +1,9 @@ import org.scalatest.FunSuite +import router.test._ import router.Router -class Basic extends FunSuite with RouterHelpers { +class Basic extends FunSuite { val yearRx = """\d{4}""".r val monthRx = """\d|10|11|12""".r val dayRx = """\d|[12]\d|30|31""".r @@ -55,15 +56,15 @@ class Basic extends FunSuite with RouterHelpers { ) test ("routes match properly") { - testRoute( - router, "blog", Map( + assert( + router matches "blog", Map( "controller" -> "blog", "action" -> "index" ) ) - testRoute( - router, "blog/2006/12/5", Map( + assert( + router matches "blog/2006/12/5", Map( "controller" -> "blog", "action" -> "show_date", "year" -> "2006", @@ -72,8 +73,8 @@ class Basic extends FunSuite with RouterHelpers { ) ) - testRoute( - router, "blog/1920/12/10", Map( + assert( + router matches "blog/1920/12/10", Map( "controller" -> "blog", "action" -> "show_date", "year" -> "1920", @@ -82,40 +83,40 @@ class Basic extends FunSuite with RouterHelpers { ) ) - testRoute( - router, "blog/edit/5", Map( + assert( + router matches "blog/edit/5", Map( "controller" -> "blog", "action" -> "edit", "id" -> "5" ) ) - testRoute( - router, "blog/show/123", Map( + assert( + router matches "blog/show/123", Map( "controller" -> "blog", "action" -> "show", "id" -> "123" ) ) - testRoute( - router, "blog/some_crazy_long_winded_action_name/12356789101112131151", Map( + assert( + router matches "blog/some_crazy_long_winded_action_name/12356789101112131151", Map( "controller" -> "blog", "action" -> "some_crazy_long_winded_action_name", "id" -> "12356789101112131151" ) ) - testRoute( - router, "blog/delete/5", Map( + assert( + router matches "blog/delete/5", Map( "controller" -> "blog", "action" -> "delete", "id" -> "5" ) ) - testRoute( - router, "test/x1", Map( + assert( + router matches "test/x1", Map( "controller" -> "test", "x" -> "x1", "y" -> "y" 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) } diff --git a/src/test/scala/optional.scala b/src/test/scala/optional.scala index 7832ba9..ade237c 100644 --- a/src/test/scala/optional.scala +++ b/src/test/scala/optional.scala @@ -1,8 +1,9 @@ import org.scalatest.FunSuite +import router.test._ import router.Router -class Optional extends FunSuite with RouterHelpers { +class Optional extends FunSuite { val router = new Router[Boolean] router addRoute ( @@ -28,53 +29,53 @@ class Optional extends FunSuite with RouterHelpers { ) test ("routes match properly") { - testRoute( - router, "people", Map( + assert( + router matches "people", Map( "controller" -> "people", "action" -> "index" ) ) - testRoute( - router, "people/new", Map( + assert( + router matches "people/new", Map( "controller" -> "people", "action" -> "new" ) ) - testRoute( - router, "people/create", Map( + assert( + router matches "people/create", Map( "controller" -> "people", "action" -> "create" ) ) - testRoute( - router, "people/56", Map( + assert( + router matches "people/56", Map( "controller" -> "people", "action" -> "show", "id" -> "56" ) ) - testRoute( - router, "people/56/edit", Map( + assert( + router matches "people/56/edit", Map( "controller" -> "people", "action" -> "edit", "id" -> "56" ) ) - testRoute( - router, "people/56/remove", Map( + assert( + router matches "people/56/remove", Map( "controller" -> "people", "action" -> "remove", "id" -> "56" ) ) - testRoute( - router, "people/56/update", Map( + assert( + router matches "people/56/update", Map( "controller" -> "people", "action" -> "update", "id" -> "56" -- cgit v1.2.3-54-g00ecf