diff options
author | Jesse Luehrs <doy@tozt.net> | 2013-02-13 19:34:07 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2013-02-13 19:34:07 -0600 |
commit | d00a11fd101e0894f1279dad3c19c6dcdaf71f6b (patch) | |
tree | d4e266ca3ef03ae1b6c1008b1d3020cb7bfac6f2 /src/test/scala | |
download | scala-path-router-d00a11fd101e0894f1279dad3c19c6dcdaf71f6b.tar.gz scala-path-router-d00a11fd101e0894f1279dad3c19c6dcdaf71f6b.zip |
initial sketch
Diffstat (limited to 'src/test/scala')
-rw-r--r-- | src/test/scala/basic.scala | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/src/test/scala/basic.scala b/src/test/scala/basic.scala new file mode 100644 index 0000000..15280e5 --- /dev/null +++ b/src/test/scala/basic.scala @@ -0,0 +1,143 @@ +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/:year/:month/:day", + true, + defaults = Map( + "controller" -> "blog", + "action" -> "show_date" + ), + validations = Map( + "year" -> yearRx, + "month" -> monthRx, + "day" -> dayRx + ) + ) + + router insertRoute ( + "blog", + true, + defaults = Map( + "controller" -> "blog", + "action" -> "index" + ) + ) + + router insertRoute ( + "blog/:action/:id", + true, + at = 2, + defaults = Map( + "controller" -> "blog" + ), + validations = Map( + "action" -> """\D+""".r, + "id" -> """\d+""".r + ) + ) + + router insertRoute ( + "test/?:x/?:y", + true, + at = 1000000, + defaults = Map( + "controller" -> "test", + "x" -> "x", + "y" -> "y" + ) + ) + + def testRoute (router: Router[Boolean], path: String, mapping: Map[String, String]) { + 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) + } + + test ("routes are created in the correct order") { + assert(router.routes(0).path === "blog") + assert(router.routes(2).path === "blog/:action/:id") + assert(router.routes(3).path === "test/?:x/?:y") + } + + test ("routes match properly") { + testRoute( + router, "blog", Map( + "controller" -> "blog", + "action" -> "index" + ) + ) + + testRoute( + router, "blog/2006/12/5", Map( + "controller" -> "blog", + "action" -> "show_date", + "year" -> "2006", + "month" -> "12", + "day" -> "5" + ) + ) + + testRoute( + router, "blog/1920/12/10", Map( + "controller" -> "blog", + "action" -> "show_date", + "year" -> "1920", + "month" -> "12", + "day" -> "10" + ) + ) + + testRoute( + router, "blog/edit/5", Map( + "controller" -> "blog", + "action" -> "edit", + "id" -> "5" + ) + ) + + testRoute( + router, "blog/show/123", Map( + "controller" -> "blog", + "action" -> "show", + "id" -> "123" + ) + ) + + testRoute( + router, "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( + "controller" -> "blog", + "action" -> "delete", + "id" -> "5" + ) + ) + + // TODO support optionals + // testRoute( + // router, "test/x1", Map( + // "controller" -> "test", + // "x" -> "x1", + // "y" -> "y" + // ) + // ) + } +} |