Permalink
Browse files

Rest arguments; more tests

  • Loading branch information...
1 parent bba0d8e commit 406d11f7f47a237862443abf44a54505d0b82e19 @davegurnell committed Oct 5, 2011
@@ -1,3 +1,6 @@
+import java.net.URLEncoder.{encode => urlEncode}
+import java.net.URLDecoder.{decode => urlDecode}
+
sealed trait Path {
/**
@@ -107,10 +110,34 @@ sealed abstract class PNil extends Path {
case object PNil extends PNil
+sealed abstract class PAny extends Path {
+
+ type Result = HCons[List[String], HNil]
+
+ def decode(path: List[String]): Option[Result] =
+ Some(HCons(path.map(str => urlDecode(str, "utf-8")), HNil))
+
+ def encode(args: Result): List[String] =
+ args.head.map(str => urlEncode(str, "utf-8"))
+
+ def :/:(arg: String) =
+ PLiteral(arg, this)
+
+ def :/:[T](arg: Arg[T]) =
+ PArg(arg, this)
+
+}
+
+case object PAny extends PAny
+
/*
Try this:
-val path = PNil / "add" / IntArg / "to" / IntArg
+val path = "add" :/: IntArg :/ :"to" :/: IntArg :/: PNil
+
+path.decode(List("a", "abc", "b", "123", "c"))
+
+path.encode(HCons("abc", HCons(123, HNil)))
*/
@@ -5,11 +5,11 @@ class HListSpec extends Specification {
"HLists can have heterogeneous arguments" in {
val list = 2.0 :: 1 :: HNil
- list.head must be_==(2.0)
- list.tail must be_==(HCons(1, HNil))
+ list.head mustEqual 2.0
+ list.tail mustEqual HCons(1, HNil)
- list.tail.head must be_==(1)
- list.tail.tail must be_==(HNil)
+ list.tail.head mustEqual 1
+ list.tail.tail mustEqual HNil
}
}
@@ -2,15 +2,32 @@ import org.specs._
class PathSpec extends Specification {
- val path = StringArg :/: IntArg :/: PNil
-
- "Path.encode works as expected" in {
- path.encode(HCons("abc", HCons(123, HNil))) mustEqual List("abc", "123")
+ val nilPath = "a" :/: StringArg :/: "b" :/: IntArg :/: "c" :/: PNil
+
+ "Nil-terminated path: decode works as expected" in {
+ nilPath.decode(List("abc", "123")) must beNone
+ nilPath.decode(List("a", "abc", "b", "123", "c")) must beSome(HCons("abc", HCons(123, HNil)))
+ nilPath.decode(List("a", "123", "b", "abc", "c")) must beNone
+ nilPath.decode(List("c", "123", "b", "abc", "a")) must beNone
+ }
+
+ "Nil-terminated path: encode works as expected" in {
+ nilPath.encode(HCons("abc", HCons(123, HNil))) mustEqual List("a", "abc", "b", "123", "c")
+ }
+
+ val restPath = "a" :/: StringArg :/: "b" :/: IntArg :/: "c" :/: PAny
+
+ "Any-terminated path: decode works as expected" in {
+ restPath.decode(List("abc", "123")) must beNone
+ restPath.decode(List("a", "abc", "b", "123", "c")) must beSome(HCons("abc", HCons(123, HCons(Nil, HNil))))
+ restPath.decode(List("a", "abc", "b", "123", "c", "d", "e")) must beSome(HCons("abc", HCons(123, HCons(List("d", "e"), HNil))))
+ restPath.decode(List("a", "123", "b", "abc", "c")) must beNone
+ restPath.decode(List("c", "123", "b", "abc", "a")) must beNone
}
- "Path.decode works as expected" in {
- path.decode(List("abc", "123")) must beSome(HCons("abc", HCons(123, HNil)))
- path.decode(List("123", "abc")) must beNone
+ "Any-terminated path: encode works as expected" in {
+ restPath.encode(HCons("abc", HCons(123, HCons(Nil, HNil)))) mustEqual List("a", "abc", "b", "123", "c")
+ restPath.encode(HCons("abc", HCons(123, HCons(List("d", "e"), HNil)))) mustEqual List("a", "abc", "b", "123", "c", "d", "e")
}
}

0 comments on commit 406d11f

Please sign in to comment.