summaryrefslogtreecommitdiff
path: root/src/test/scala/cc
diff options
context:
space:
mode:
authorJohannes Rudolph <johannes.rudolph@gmail.com>2013-08-20 10:20:27 +0200
committerJohannes Rudolph <johannes.rudolph@gmail.com>2013-08-20 10:20:27 +0200
commitc788149a29ee0cfb60bd06361340fb7585c9ce5b (patch)
tree1a2f2b0fb3f29b182904cf5e90c3cab2892466cd /src/test/scala/cc
parent81eda9a391fcf0c35ab0909641e2e60f7b93ac96 (diff)
downloadspray-json-c788149a29ee0cfb60bd06361340fb7585c9ce5b.tar.gz
spray-json-c788149a29ee0cfb60bd06361340fb7585c9ce5b.tar.bz2
spray-json-c788149a29ee0cfb60bd06361340fb7585c9ce5b.zip
move test files into right directory according to package
Diffstat (limited to 'src/test/scala/cc')
-rw-r--r--src/test/scala/cc/spray/json/lenses/JsonLensesSpec.scala198
-rw-r--r--src/test/scala/cc/spray/json/lenses/JsonLensesTest.scala45
-rw-r--r--src/test/scala/cc/spray/json/lenses/JsonPathExamplesSpec.scala92
-rw-r--r--src/test/scala/cc/spray/json/lenses/JsonPathSpecs.scala43
-rw-r--r--src/test/scala/cc/spray/json/lenses/SpecHelpers.scala23
5 files changed, 0 insertions, 401 deletions
diff --git a/src/test/scala/cc/spray/json/lenses/JsonLensesSpec.scala b/src/test/scala/cc/spray/json/lenses/JsonLensesSpec.scala
deleted file mode 100644
index 1c3f7c1..0000000
--- a/src/test/scala/cc/spray/json/lenses/JsonLensesSpec.scala
+++ /dev/null
@@ -1,198 +0,0 @@
-package spray.json
-package lenses
-
-import DefaultJsonProtocol._
-
-import org.specs2.mutable.Specification
-import spray.json.DeserializationException
-
-class JsonLensesSpec extends Specification with SpecHelpers {
-
- import JsonLenses._
-
- val n = field("n")
-
- "Lenses" should {
- "access" in {
- "field" in {
- "existing" in {
- """{"n": 2}""".extract[Int]('n) must be_==(2)
- }
- "missing" in {
- """{"n": 2}""".extract[Int]('z) must throwAn[Exception]( """Expected field 'z' in '{"n":2}'""")
- }
- "wrong type" in {
- """{"n": 2}""".extract[String]('n) must throwA[DeserializationException]("Expected String as JsString, but got 2")
- }
- }
- "optional field" in {
- """[{"b": 4}, {"c": 5}]""".extract[Int](* / 'b.?) must be_==(Seq(4))
- }
- "field of member" in {
- """{"n": {"b": 4}}""".extract[Int]("n" / "b") must be_==(4)
- }
- "element of array" in {
- "existing" in {
- """["a", "b", 2, 5, 8, 3]""".extract[Int](element(3)) must be_==(5)
- }
- "out of bounds" in {
- """["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[Int](JsonLenses.find(JsonLenses.value.is[Int](_ < 4))) must beSome(2)
- }
- "if type is wrong" in {
- """[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[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[Int](lens) must beSome(5)
- }
- "missing in first find" in {
- """[{"a": 2, "b": {"c": [5]}}, 13]""".extract[Int](lens) must beNone
- }
- "missing in second find" in {
- """[{"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[Int](*) must be_==(Seq(18, 23, 2, 5, 8, 3))
- }
- "which is a scalar element" in {
- """{"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[Int]((* / "a")) must be_==(Seq(1, 2))
- }
- "nested" in {
- """[[1, 2], [3, 4]]""".extract[Int]((* / *)) must be_==(Seq(1, 2, 3, 4))
- }
- "if outer is no array" in {
- """{"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[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 {
-
- }*/
- }
-
- "modify" in {
- "set field" in {
- "existing" in {
- """{"n": 12}""" update (n ! set(23)) must be_json( """{"n": 23}""")
- }
- "missing" in {
- """{"n": {"b": 4}}""" update ("n" / "c" ! set(23)) must be_json( """{"n": {"b": 4, "c": 23}}""")
- }
- "twice" in {
- val a = field("a")
- """{"a": 5}""" update (a ! set(23) && a ! set(15)) must be_json( """{"a": 15}""")
- }
- }
- "update field" in {
- "existing" in {
- """{"n": 12}""" update (n ! modify[Int](_ + 1)) must be_json( """{"n": 13}""")
- }
- "wrong type" in {
- """{"n": 12}""" update (n ! modify[String](_ + "test")) must throwA[RuntimeException]("spray.json.DeserializationException: Expected String as JsString, but got 12")
- }
- "missing" in {
- """{"n": 12}""" update (field("z") ! modify[Int](_ + 1)) must throwAn[Exception]( """Expected field 'z' in '{"n":12}'""")
- }
- }
- "optional field" in {
- "modify" in {
- """[{"b": 4}, {"c": 5}]""".update((* / 'b.?) ! modify[Int](_ + 12)) must be_json("""[{"b": 16}, {"c": 5}]""")
- }
- "create" in {
- """[{"b": 4}, {"c": 5}]""".update((* / 'b.?) ! set(38)) must be_json("""[{"b": 38}, {"c": 5, "b": 38}]""")
- }
- "set or update with default" in {
- """[{"b": 4}, {"c": 5}]""".update((* / 'b.?) ! setOrUpdateField(38)(1 + )) must be_json("""[{"b": 5}, {"c": 5, "b": 38}]""")
- }
-
- "create nested (current behavior)" in {
- // One could think that nested `optionalField`s and `set` would create intermediate
- // objects as well. However, this is currently not possible, since
- // 1. the signature of UpdateLens.updated doesn't allow to operate on
- // missing parents, which would be necessary to let child lenses
- // control the creation of parents.
- // 2. the combine lens would then have to support it
- //
- // This test remains here as witness to the current behavior.
-
- """[{"b": {}}, {"c": 5}]""".update((* / 'b.? / 'd.?) ! set(38)) must be_json(
- """[{"b":{"d":38}},{"c":5}]""")
- }
- "delete some" in {
- def f(i: Int): Option[Int] =
- Some(i).filter(_ % 2 == 0)
- """[{"b": 4}, {"b": 3}]""".update((* / 'b.?) ! modifyOrDeleteField(f)) must be_json("""[{"b": 4}, {}]""")
- }
- }
- "set field of member" in {
- """{"n": {"b": 4}}""" update ("n" / "b" ! set(23)) must be_json( """{"n": {"b": 23}}""")
- }
- "update field of member" in {
- "existing" in {
- """{"n": {"b": 4}}""" update ("n" / "b" ! modify[Int](1 +)) must be_json( """{"n": {"b": 5}}""")
- }
- "parent missing" in {
- """{"x": {"b": 4}}""" update ("n" / "b" ! modify[Int](1 +)) must throwAn[Exception]( """Expected field 'n' in '{"x":{"b":4}}'""")
- }
- }
- "set element of array" in {
- """["a", "b", 2, 5, 8, 3]""" update (element(3) ! set(35)) must be_json( """["a", "b", 2, 35, 8, 3]""")
- }
- "change a found element" in {
- "in a homogenuous array" in {
- "if found" in {
- """[12, 39, 2, 5, 8, 3]""" update (JsonLenses.find(JsonLenses.value.is[Int](_ < 4)) ! set("test")) must be_json(
- """[12, 39, "test", 5, 8, 3]""")
- }
- "if not found" in {
- """[12, 39, 2, 5, 8, 3]""" update (JsonLenses.find(JsonLenses.value.is[Int](_ == 434)) ! set("test")) must be_json(
- """[12, 39, 2, 5, 8, 3]""")
- }
- }
- "in an inhomogenuous array" in {
- """["a", "b", 2, 5, 8, 3]""" update (JsonLenses.find(JsonLenses.value.is[Int](_ < 4)) ! set("test")) must be_json(
- """["a", "b", "test", 5, 8, 3]"""
- )
- }
- "nested" 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]""" update (lens ! set(42)) must be_json( """[{"a": 12, "b": {"c": [2, 42]}}, 13]""")
- }
- "missing in first find" in {
- """[{"a": 2, "b": {"c": [5]}}, 13]""" update (lens ! set(42)) must be_json( """[{"a": 2, "b": {"c": [5]}}, 13]""")
- }
- "missing in second find" in {
- """[{"a": 2, "b": {"c": [7]}}, 13]""" update (lens ! set(42)) must be_json( """[{"a": 2, "b": {"c": [7]}}, 13]""")
- }
- }
- }
- }
- }
-}
-
-
diff --git a/src/test/scala/cc/spray/json/lenses/JsonLensesTest.scala b/src/test/scala/cc/spray/json/lenses/JsonLensesTest.scala
deleted file mode 100644
index 2cb887b..0000000
--- a/src/test/scala/cc/spray/json/lenses/JsonLensesTest.scala
+++ /dev/null
@@ -1,45 +0,0 @@
-package spray.json
-package lenses
-
-import Predef.{augmentString => _, wrapString => _, _}
-import DefaultJsonProtocol._
-import spray.json.{JsValue, JsonParser}
-
-object JsonLensesTest extends App {
-
- import JsonLenses._
-
- "n" ! set(3)
- "els" ! append {
- "name" ! set("Peter") &&
- "money" ! set(2)
- }
- "els" / element(1) ! update {
- "money" ! set(38) &&
- "name" ! set("Testperson")
- } && "n" ! modify[Int](_ + 1)
-
- val json = JsonParser("test")
- val newJson = json("els" / "money") = 12
-
- val i = json.extract[Int]("els" / "money")
-
- ("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" / filter("money".is[Int](_ < 30)) / "name" ! modify[String]("Richman " + _)
-
- //: JsValue => JsValue
-
- def updateMoney(x: Int) =
- "money" ! modify[Int](_ + x)
-
- "els" / * ! update(updateMoney(12))
- "els" / * ! extract("name") {
- name: String =>
- updateMoney(name.length)
- }
-}
diff --git a/src/test/scala/cc/spray/json/lenses/JsonPathExamplesSpec.scala b/src/test/scala/cc/spray/json/lenses/JsonPathExamplesSpec.scala
deleted file mode 100644
index 707671b..0000000
--- a/src/test/scala/cc/spray/json/lenses/JsonPathExamplesSpec.scala
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Tests imported from http://code.google.com/p/json-path/ to
- * test some 'real-world' examples. json-path is licensed under
- * the Apache 2 license. http://www.apache.org/licenses/LICENSE-2.0
- */
-package spray.json
-package lenses
-
-import org.specs2.mutable.Specification
-import spray.json.{JsValue, JsonParser, DefaultJsonProtocol}
-
-class JsonPathExamplesSpec extends Specification with SpecHelpers {
-
- import JsonLenses._
- import DefaultJsonProtocol._
-
- val json = JsonParser(
- """
- |{ "store": {
- | "book": [
- | { "category": "reference",
- | "author": "Nigel Rees",
- | "title": "Sayings of the Century",
- | "price": 8.95
- | },
- | { "category": "fiction",
- | "author": "Evelyn Waugh",
- | "title": "Sword of Honour",
- | "price": 12.99,
- | "isbn": "0-553-21311-3"
- | }
- | ],
- | "bicycle": {
- | "color": "red",
- | "price": 19.95
- | }
- | }
- |}
- """.stripMargin)
-
- "Examples" should {
- "with Scala syntax" in {
- "All authors" in {
- json.extract[String](("store" / "book" / * / "author")) must be_==(Seq("Nigel Rees", "Evelyn Waugh"))
- }
- "Author of first book" in {
- json.extract[String](("store" / "book" / element(0) / "author")) must be_==("Nigel Rees")
- }
- "Books with category 'reference'" in {
- 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[String](("store" / "book" / filter("price".is[Double](_ >= 10)) / "title")) must be_==(Seq("Sword of Honour"))
- }
- "All books that have isbn" in {
- json.extract[String](("store" / "book" / filter("isbn".is[JsValue](_ => true)) / "title")) must be_==(Seq("Sword of Honour"))
- }
- "Isbn of books that have isbn" in {
- json.extract[String](("store" / "book" / * / "isbn".?)) must be_==(Seq("0-553-21311-3"))
- }
- "All prices" in todo
- }
- "With Json-Path syntax" in {
- import JsonLenses.fromPath
-
- "All authors" in {
- json.extract[String](fromPath("$.store.book[*].author")) must be_==(Seq("Nigel Rees", "Evelyn Waugh"))
- }
- "Author of first book" in {
- json.extract[String](fromPath("$.store.book[0].author")) must be_==(Seq("Nigel Rees"))
- }
- "Books with category 'reference'" in {
- json.extract[String](fromPath("$.store.book[?(@.category == 'reference')].title")) must be_==(Seq("Sayings of the Century"))
- }
- "Books that cost more than 10 USD" in {
- json.extract[String](fromPath("$.store.book[?(@.price > 10)].title")) must be_==(Seq("Sword of Honour"))
- }
- "All books that have isbn" in {
- json.extract[String](fromPath("$.store.book[?(@.isbn)].title")) must be_==(Seq("Sword of Honour"))
- }
- "Isbn of books that have isbn" in {
- val lens = fromPath("$.store.book[*].isbn")
- json.extract[String](lens) must be_==(Seq("0-553-21311-3"))
-
- val expected =
- JsonParser("""{"store":{"bicycle":{"color":"red","price":19.95},"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99,"isbn":"0-553-21311-3?"}]}}""")
- json.update(lens ! modify[String](_ + "?")) must be_==(expected)
- }
- "All prices" in todo
- }
- }
-}
diff --git a/src/test/scala/cc/spray/json/lenses/JsonPathSpecs.scala b/src/test/scala/cc/spray/json/lenses/JsonPathSpecs.scala
deleted file mode 100644
index 0c07f48..0000000
--- a/src/test/scala/cc/spray/json/lenses/JsonPathSpecs.scala
+++ /dev/null
@@ -1,43 +0,0 @@
-package spray.json
-package lenses
-
-import org.specs2.mutable.Specification
-
-class JsonPathSpecs extends Specification {
-
- import JsonPath._
-
- "JsonPath parser" should {
- "simple field selection" in {
- parse("$.test") must be_==(Selection(Root, ByField("test")))
- }
- "multiple field selection" in {
- parse("$.test.test2") must be_==(Selection(Selection(Root, ByField("test")), ByField("test2")))
- }
- "wildcard selection" in {
- parse("$.a[*].c") must be_==(Selection(Selection(Selection(Root, ByField("a")), AllElements), ByField("c")))
- }
- "element selection" in {
- "of root" in {
- parse("$[2]") must be_==(Selection(Root, ByIndex(2)))
- }
- "of field" in {
- parse("$['abc']") must be_==(Selection(Root, ByField("abc")))
- }
- "by predicate" in {
- "eq" in {
- parse("$[?(@.id == 'test')]") must be_==(Selection(Root, ByPredicate(Eq(PathExpr(Selection(Root, ByField("id"))), Constant(JsString("test"))))))
- }
- "lt" in {
- parse("$[?(@.id < 12)]") must be_==(Selection(Root, ByPredicate(Lt(PathExpr(Selection(Root, ByField("id"))), Constant(JsNumber(12))))))
- }
- "exists" in {
- parse("$[?(@.id)]") must be_==(Selection(Root, ByPredicate(Exists(Selection(Root, ByField("id"))))))
- }
- }
- }
- }
-
- def parse(str: String) =
- JsonPathParser(str)
-}
diff --git a/src/test/scala/cc/spray/json/lenses/SpecHelpers.scala b/src/test/scala/cc/spray/json/lenses/SpecHelpers.scala
deleted file mode 100644
index 1114965..0000000
--- a/src/test/scala/cc/spray/json/lenses/SpecHelpers.scala
+++ /dev/null
@@ -1,23 +0,0 @@
-package spray.json
-package lenses
-
-import org.specs2.mutable.Specification
-import spray.json.JsonParser
-
-trait SpecHelpers {
- self: Specification =>
-
- import JsonLenses._
-
- def be_json(json: String) =
- be_==(JsonParser(json))
-
- import org.specs2.matcher.{BeMatching, Matcher}
-
- override def throwA[E <: Throwable](message: String = ".*")(implicit m: ClassManifest[E]): Matcher[Any] = {
- import java.util.regex.Pattern
- throwA(m).like {
- case e => createExpectable(e.getMessage).applyMatcher(new BeMatching(".*" + Pattern.quote(message) + ".*"))
- }
- }
-}