diff options
author | Johannes Rudolph <johannes_rudolph@gmx.de> | 2012-05-30 18:08:55 +0200 |
---|---|---|
committer | Johannes Rudolph <johannes_rudolph@gmx.de> | 2012-05-30 18:08:55 +0200 |
commit | a2f75d6dac9616ab6c6d7af9a8e841c65773b84a (patch) | |
tree | e168265069ca37fa1179c015282dd305421119bf /src/test | |
parent | ab8c6910df69e87041299d7c4426c05059a2b448 (diff) | |
download | spray-json-a2f75d6dac9616ab6c6d7af9a8e841c65773b84a.tar.gz spray-json-a2f75d6dac9616ab6c6d7af9a8e841c65773b84a.tar.bz2 spray-json-a2f75d6dac9616ab6c6d7af9a8e841c65773b84a.zip |
cleanup, documentation and syntax changes
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/scala/cc/spray/json/JsonLensesSpec.scala | 42 | ||||
-rw-r--r-- | src/test/scala/cc/spray/json/JsonLensesTest.scala | 8 | ||||
-rw-r--r-- | src/test/scala/cc/spray/json/JsonPathTests.scala | 10 | ||||
-rw-r--r-- | src/test/scala/cc/spray/json/SpecHelpers.scala | 12 |
4 files changed, 31 insertions, 41 deletions
diff --git a/src/test/scala/cc/spray/json/JsonLensesSpec.scala b/src/test/scala/cc/spray/json/JsonLensesSpec.scala index 0007cae..715fc2a 100644 --- a/src/test/scala/cc/spray/json/JsonLensesSpec.scala +++ b/src/test/scala/cc/spray/json/JsonLensesSpec.scala @@ -13,73 +13,73 @@ class JsonLensesSpec extends Specification with SpecHelpers { "access" in { "field" in { "existing" in { - """{"n": 2}""" extract "n".get[Int] must be_==(2) + """{"n": 2}""".extract[Int]('n) must be_==(2) } "missing" in { - """{"n": 2}""" extract "z".get[Int] must throwAn[Exception]("""Expected field 'z' in '{"n":2}'""") + """{"n": 2}""".extract[Int]('z) must throwAn[Exception]("""Expected field 'z' in '{"n":2}'""") } "wrong type" in { - """{"n": 2}""" extract "n".get[String] must throwA[DeserializationException]("Expected String as JsString, but got 2") + """{"n": 2}""".extract[String]('n) must throwA[DeserializationException]("Expected String as JsString, but got 2") } } "field of member" in { - """{"n": {"b": 4}}""" extract ("n" / "b").get[Int] must be_==(4) + """{"n": {"b": 4}}""".extract[Int]("n" / "b") must be_==(4) } "element of array" in { "existing" in { - """["a", "b", 2, 5, 8, 3]""" extract element(3).get[Int] must be_==(5) + """["a", "b", 2, 5, 8, 3]""".extract[Int](element(3)) must be_==(5) } "out of bounds" in { - """["a", "b", 2, 5, 8, 3]""" extract element(38).get[Int] must throwAn[IndexOutOfBoundsException]("Too little elements in array: [\"a\",\"b\",2,5,8,3] size: 6 index: 38") + """["a", "b", 2, 5, 8, 3]""".extract[Int](element(38)) must throwAn[IndexOutOfBoundsException]("Too little elements in array: [\"a\",\"b\",2,5,8,3] size: 6 index: 38") } } "finding an element" in { "in a homogenous array" in { "if type matches" in { - """[18, 23, 2, 5, 8, 3]""" extract JsonLenses.find(JsonLenses.value.is[Int](_ < 4)).get[Int] must beSome(2) + """[18, 23, 2, 5, 8, 3]""".extract[Int](JsonLenses.find(JsonLenses.value.is[Int](_ < 4))) must beSome(2) } "if type is wrong" in { - """[18, 23, 2, 5, 8, 3]""" extract JsonLenses.find(JsonLenses.value.is[String](_ < "unknown")).get[Int] must beNone + """[18, 23, 2, 5, 8, 3]""".extract[Int](JsonLenses.find(JsonLenses.value.is[String](_ < "unknown"))) must beNone } } "in an imhomogenous array" in { - """["a", "b", 2, 5, 8, 3]""" extract JsonLenses.find(JsonLenses.value.is[Int](_ < 4)).get[Int] must beSome(2) - """["a", "b", 2, 5, 8, 3]""" extract JsonLenses.find(JsonLenses.value.is[String](_ == "unknown")).get[Int] must beNone + """["a", "b", 2, 5, 8, 3]""".extract[Int](JsonLenses.find(JsonLenses.value.is[Int](_ < 4))) must beSome(2) + """["a", "b", 2, 5, 8, 3]""".extract[Int](JsonLenses.find(JsonLenses.value.is[String](_ == "unknown"))) must beNone } "nested finding" in { val lens = JsonLenses.find("a".is[Int](_ == 12)) / "b" / "c" / JsonLenses.find(JsonLenses.value.is[Int](_ == 5)) "existing" in { - """[{"a": 12, "b": {"c": [2, 5]}}, 13]""" extract lens.get[Int] must beSome(5) + """[{"a": 12, "b": {"c": [2, 5]}}, 13]""".extract[Int](lens) must beSome(5) } "missing in first find" in { - """[{"a": 2, "b": {"c": [5]}}, 13]""" extract lens.get[Int] must beNone + """[{"a": 2, "b": {"c": [5]}}, 13]""".extract[Int](lens) must beNone } "missing in second find" in { - """[{"a": 2, "b": {"c": [7]}}, 13]""" extract lens.get[Int] must beNone + """[{"a": 2, "b": {"c": [7]}}, 13]""".extract[Int](lens) must beNone } } } "all elements of an array" in { "simple" in { - """[18, 23, 2, 5, 8, 3]""" extract elements.get[Int] must be_==(Seq(18, 23, 2, 5, 8, 3)) + """[18, 23, 2, 5, 8, 3]""".extract[Int](*) must be_==(Seq(18, 23, 2, 5, 8, 3)) } "which is a scalar element" in { - """{"a": [1, 2, 3, 4]}""" extract ("a" / elements).get[Int] must be_==(Seq(1, 2, 3, 4)) + """{"a": [1, 2, 3, 4]}""".extract[Int](("a" / *)) must be_==(Seq(1, 2, 3, 4)) } "field of an array element" in { - """[{"a": 1}, {"a": 2}]""" extract (elements / "a").get[Int] must be_==(Seq(1, 2)) + """[{"a": 1}, {"a": 2}]""".extract[Int]((* / "a")) must be_==(Seq(1, 2)) } "nested" in { - """[[1, 2], [3, 4]]""" extract (elements / elements).get[Int] must be_==(Seq(1, 2, 3, 4)) + """[[1, 2], [3, 4]]""".extract[Int]((* / *)) must be_==(Seq(1, 2, 3, 4)) } "if outer is no array" in { - """{"a": 5}""" extract (elements / "a").get[Int] must throwAn[Exception]("""Not a json array: {"a":5}""") - """{"a": 5}""" extract (elements / elements).get[Int] must throwAn[Exception]("""Not a json array: {"a":5}""") + """{"a": 5}""".extract[Int]((* / "a")) must throwAn[Exception]("""Not a json array: {"a":5}""") + """{"a": 5}""".extract[Int]((* / *)) must throwAn[Exception]("""Not a json array: {"a":5}""") } "if inner is no array" in { - """[{}, {}]""" extract (elements / elements).get[Int] must throwAn[Exception]("""Not a json array: {}""") - """{"a": 5}""" extract ("a" / elements).get[Int] must throwAn[Exception]("""Not a json array: 5""") + """[{}, {}]""".extract[Int]((* / *)) must throwAn[Exception]("""Not a json array: {}""") + """{"a": 5}""".extract[Int](("a" / *)) must throwAn[Exception]("""Not a json array: 5""") } } /*"filtered elements of an array" in { diff --git a/src/test/scala/cc/spray/json/JsonLensesTest.scala b/src/test/scala/cc/spray/json/JsonLensesTest.scala index 33cb375..15bba11 100644 --- a/src/test/scala/cc/spray/json/JsonLensesTest.scala +++ b/src/test/scala/cc/spray/json/JsonLensesTest.scala @@ -20,14 +20,14 @@ object JsonLensesTest extends App { val json = JsonParser("test") val newJson = json("els" / "money") = 12 - val i = json[Int]("els" / "money") + val i = json.extract[Int]("els" / "money") - ("els" / element(1) / "money").get[Int]: (JsValue => Int) + ("els" / element(1) / "money").get[Int] _: (JsValue => Int) ("els" / find("money".is[Int](_ < 30)) / "name").get[String]: (JsValue => Option[String]) - ("els" / * / "money").get[Int]: (JsValue => Seq[Int]) - ("els" / filter("money".is[Int](_ < 30)) / "name").get[String]: (JsValue => Seq[String]) + ("els" / * / "money").get[Int] _: (JsValue => Seq[Int]) + ("els" / filter("money".is[Int](_ < 30)) / "name").get[String] _: (JsValue => Seq[String]) "els" / filter("money".is[Int](_ < 30)) / "name" ! updated[String]("Richman " + _) //: JsValue => JsValue diff --git a/src/test/scala/cc/spray/json/JsonPathTests.scala b/src/test/scala/cc/spray/json/JsonPathTests.scala index 6347002..1aaff10 100644 --- a/src/test/scala/cc/spray/json/JsonPathTests.scala +++ b/src/test/scala/cc/spray/json/JsonPathTests.scala @@ -37,19 +37,19 @@ class JsonPathTests extends Specification with SpecHelpers { "Examples" should { "All authors" in { - json extract ("store" / "book" / * / "author").get[String] must be_==(Seq("Nigel Rees", "Evelyn Waugh")) + json.extract[String](("store" / "book" / * / "author")) must be_==(Seq("Nigel Rees", "Evelyn Waugh")) } "Author of first book" in { - json extract ("store" / "book" / element(0) / "author").get[String] must be_==("Nigel Rees") + json.extract[String](("store" / "book" / element(0) / "author")) must be_==("Nigel Rees") } "Books with category 'reference'" in { - json extract ("store" / "book" / filter("category".is[String](_ == "reference")) / "title").get[String] must be_==(Seq("Sayings of the Century")) + json.extract[String](("store" / "book" / filter("category".is[String](_ == "reference")) / "title")) must be_==(Seq("Sayings of the Century")) } "Books that cost more than 10 USD" in { - json extract ("store" / "book" / filter("price".is[Double](_ >= 10)) / "title").get[String] must be_==(Seq("Sword of Honour")) + json.extract[String](("store" / "book" / filter("price".is[Double](_ >= 10)) / "title")) must be_==(Seq("Sword of Honour")) } "All books that have isbn" in { - json extract ("store" / "book" / filter("isbn".is[JsValue](_ => true)) / "title").get[String] must be_==(Seq("Sword of Honour")) + json.extract[String](("store" / "book" / filter("isbn".is[JsValue](_ => true)) / "title")) must be_==(Seq("Sword of Honour")) } "All prices" in todo } diff --git a/src/test/scala/cc/spray/json/SpecHelpers.scala b/src/test/scala/cc/spray/json/SpecHelpers.scala index 1c73637..6a6ea43 100644 --- a/src/test/scala/cc/spray/json/SpecHelpers.scala +++ b/src/test/scala/cc/spray/json/SpecHelpers.scala @@ -19,15 +19,5 @@ trait SpecHelpers { } } - case class RichTestString(string: String) { - def js = JsonParser(string) - - def extract[T: MonadicReader]: Extractor[T] = js.extract[T] - - def extract[T](f: JsValue => T): T = f(js) - - def update(updater: Update): JsValue = updater(js) - } - - implicit def richTestString(string: String): RichTestString = RichTestString(string) + implicit def richTestString(string: String): RichJsValue = RichJsValue(JsonParser(string)) } |