From 64e67cfd70f18b67948fd3f7b03e39af9b5d31c3 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 28 Feb 2013 23:28:55 -0600 Subject: cleanups --- src/test/scala/basic.scala | 126 -------------------- src/test/scala/helpers.scala | 40 ------- src/test/scala/optional.scala | 85 -------------- src/test/scala/org/perl8/router/BasicTest.scala | 127 +++++++++++++++++++++ src/test/scala/org/perl8/router/OptionalTest.scala | 86 ++++++++++++++ src/test/scala/org/perl8/router/test.scala | 40 +++++++ 6 files changed, 253 insertions(+), 251 deletions(-) delete mode 100644 src/test/scala/basic.scala delete mode 100644 src/test/scala/helpers.scala delete mode 100644 src/test/scala/optional.scala create mode 100644 src/test/scala/org/perl8/router/BasicTest.scala create mode 100644 src/test/scala/org/perl8/router/OptionalTest.scala create mode 100644 src/test/scala/org/perl8/router/test.scala (limited to 'src/test') diff --git a/src/test/scala/basic.scala b/src/test/scala/basic.scala deleted file mode 100644 index 56655e4..0000000 --- a/src/test/scala/basic.scala +++ /dev/null @@ -1,126 +0,0 @@ -import org.scalatest.FunSuite -import router.test._ - -import router.Router - -class Basic extends FunSuite { - val yearRx = """\d{4}""".r - val monthRx = """\d|10|11|12""".r - val dayRx = """\d|[12]\d|30|31""".r - - val router = new Router[Boolean] - - router addRoute ( - "blog", - true, - defaults = Map( - "controller" -> "blog", - "action" -> "index" - ) - ) - - router addRoute ( - "blog/:year/:month/:day", - true, - defaults = Map( - "controller" -> "blog", - "action" -> "show_date" - ), - validations = Map( - "year" -> yearRx, - "month" -> monthRx, - "day" -> dayRx - ) - ) - - router addRoute ( - "blog/:action/:id", - true, - defaults = Map( - "controller" -> "blog" - ), - validations = Map( - "action" -> """\D+""".r, - "id" -> """\d+""".r - ) - ) - - router addRoute ( - "test/?:x/?:y", - true, - defaults = Map( - "controller" -> "test", - "x" -> "x", - "y" -> "y" - ) - ) - - test ("routes match properly") { - assert( - router matches "blog", Map( - "controller" -> "blog", - "action" -> "index" - ) - ) - - assert( - router matches "blog/2006/12/5", Map( - "controller" -> "blog", - "action" -> "show_date", - "year" -> "2006", - "month" -> "12", - "day" -> "5" - ) - ) - - assert( - router matches "blog/1920/12/10", Map( - "controller" -> "blog", - "action" -> "show_date", - "year" -> "1920", - "month" -> "12", - "day" -> "10" - ) - ) - - assert( - router matches "blog/edit/5", Map( - "controller" -> "blog", - "action" -> "edit", - "id" -> "5" - ) - ) - - assert( - router matches "blog/show/123", Map( - "controller" -> "blog", - "action" -> "show", - "id" -> "123" - ) - ) - - assert( - router matches "blog/some_crazy_long_winded_action_name/12356789101112131151", Map( - "controller" -> "blog", - "action" -> "some_crazy_long_winded_action_name", - "id" -> "12356789101112131151" - ) - ) - - assert( - router matches "blog/delete/5", Map( - "controller" -> "blog", - "action" -> "delete", - "id" -> "5" - ) - ) - - 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 deleted file mode 100644 index f2f561c..0000000 --- a/src/test/scala/helpers.scala +++ /dev/null @@ -1,40 +0,0 @@ -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 deleted file mode 100644 index ade237c..0000000 --- a/src/test/scala/optional.scala +++ /dev/null @@ -1,85 +0,0 @@ -import org.scalatest.FunSuite -import router.test._ - -import router.Router - -class Optional extends FunSuite { - val router = new Router[Boolean] - - router addRoute ( - ":controller/?:action", - true, - defaults = Map( - "action" -> "index" - ), - validations = Map( - "action" -> """\D+""".r - ) - ) - - router addRoute ( - ":controller/:id/?:action", - true, - defaults = Map( - "action" -> "show" - ), - validations = Map( - "id" -> """\d+""".r - ) - ) - - test ("routes match properly") { - assert( - router matches "people", Map( - "controller" -> "people", - "action" -> "index" - ) - ) - - assert( - router matches "people/new", Map( - "controller" -> "people", - "action" -> "new" - ) - ) - - assert( - router matches "people/create", Map( - "controller" -> "people", - "action" -> "create" - ) - ) - - assert( - router matches "people/56", Map( - "controller" -> "people", - "action" -> "show", - "id" -> "56" - ) - ) - - assert( - router matches "people/56/edit", Map( - "controller" -> "people", - "action" -> "edit", - "id" -> "56" - ) - ) - - assert( - router matches "people/56/remove", Map( - "controller" -> "people", - "action" -> "remove", - "id" -> "56" - ) - ) - - assert( - router matches "people/56/update", Map( - "controller" -> "people", - "action" -> "update", - "id" -> "56" - ) - ) - } -} diff --git a/src/test/scala/org/perl8/router/BasicTest.scala b/src/test/scala/org/perl8/router/BasicTest.scala new file mode 100644 index 0000000..b00d1ae --- /dev/null +++ b/src/test/scala/org/perl8/router/BasicTest.scala @@ -0,0 +1,127 @@ +package org.perl8.router + +import org.scalatest.FunSuite + +import org.perl8.router.test._ + +class BasicTest extends FunSuite { + val yearRx = """\d{4}""".r + val monthRx = """\d|10|11|12""".r + val dayRx = """\d|[12]\d|30|31""".r + + val router = new Router[Boolean] + + router addRoute ( + "blog", + true, + defaults = Map( + "controller" -> "blog", + "action" -> "index" + ) + ) + + router addRoute ( + "blog/:year/:month/:day", + true, + defaults = Map( + "controller" -> "blog", + "action" -> "show_date" + ), + validations = Map( + "year" -> yearRx, + "month" -> monthRx, + "day" -> dayRx + ) + ) + + router addRoute ( + "blog/:action/:id", + true, + defaults = Map( + "controller" -> "blog" + ), + validations = Map( + "action" -> """\D+""".r, + "id" -> """\d+""".r + ) + ) + + router addRoute ( + "test/?:x/?:y", + true, + defaults = Map( + "controller" -> "test", + "x" -> "x", + "y" -> "y" + ) + ) + + test ("routes match properly") { + assert( + router matches "blog", Map( + "controller" -> "blog", + "action" -> "index" + ) + ) + + assert( + router matches "blog/2006/12/5", Map( + "controller" -> "blog", + "action" -> "show_date", + "year" -> "2006", + "month" -> "12", + "day" -> "5" + ) + ) + + assert( + router matches "blog/1920/12/10", Map( + "controller" -> "blog", + "action" -> "show_date", + "year" -> "1920", + "month" -> "12", + "day" -> "10" + ) + ) + + assert( + router matches "blog/edit/5", Map( + "controller" -> "blog", + "action" -> "edit", + "id" -> "5" + ) + ) + + assert( + router matches "blog/show/123", Map( + "controller" -> "blog", + "action" -> "show", + "id" -> "123" + ) + ) + + assert( + router matches "blog/some_crazy_long_winded_action_name/12356789101112131151", Map( + "controller" -> "blog", + "action" -> "some_crazy_long_winded_action_name", + "id" -> "12356789101112131151" + ) + ) + + assert( + router matches "blog/delete/5", Map( + "controller" -> "blog", + "action" -> "delete", + "id" -> "5" + ) + ) + + assert( + router matches "test/x1", Map( + "controller" -> "test", + "x" -> "x1", + "y" -> "y" + ) + ) + } +} diff --git a/src/test/scala/org/perl8/router/OptionalTest.scala b/src/test/scala/org/perl8/router/OptionalTest.scala new file mode 100644 index 0000000..1073487 --- /dev/null +++ b/src/test/scala/org/perl8/router/OptionalTest.scala @@ -0,0 +1,86 @@ +package org.perl8.router + +import org.scalatest.FunSuite + +import org.perl8.router.test._ + +class OptionalTest extends FunSuite { + val router = new Router[Boolean] + + router addRoute ( + ":controller/?:action", + true, + defaults = Map( + "action" -> "index" + ), + validations = Map( + "action" -> """\D+""".r + ) + ) + + router addRoute ( + ":controller/:id/?:action", + true, + defaults = Map( + "action" -> "show" + ), + validations = Map( + "id" -> """\d+""".r + ) + ) + + test ("routes match properly") { + assert( + router matches "people", Map( + "controller" -> "people", + "action" -> "index" + ) + ) + + assert( + router matches "people/new", Map( + "controller" -> "people", + "action" -> "new" + ) + ) + + assert( + router matches "people/create", Map( + "controller" -> "people", + "action" -> "create" + ) + ) + + assert( + router matches "people/56", Map( + "controller" -> "people", + "action" -> "show", + "id" -> "56" + ) + ) + + assert( + router matches "people/56/edit", Map( + "controller" -> "people", + "action" -> "edit", + "id" -> "56" + ) + ) + + assert( + router matches "people/56/remove", Map( + "controller" -> "people", + "action" -> "remove", + "id" -> "56" + ) + ) + + assert( + router matches "people/56/update", Map( + "controller" -> "people", + "action" -> "update", + "id" -> "56" + ) + ) + } +} diff --git a/src/test/scala/org/perl8/router/test.scala b/src/test/scala/org/perl8/router/test.scala new file mode 100644 index 0000000..e1fff79 --- /dev/null +++ b/src/test/scala/org/perl8/router/test.scala @@ -0,0 +1,40 @@ +package org.perl8.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) +} -- cgit v1.2.3-54-g00ecf