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