summaryrefslogtreecommitdiffstats
path: root/src/test/scala/org/perl8
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/scala/org/perl8')
-rw-r--r--src/test/scala/org/perl8/router/BasicTest.scala127
-rw-r--r--src/test/scala/org/perl8/router/OptionalTest.scala86
-rw-r--r--src/test/scala/org/perl8/router/test.scala40
3 files changed, 253 insertions, 0 deletions
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)
+}