diff options
author | Mathias <mathias@spray.cc> | 2011-10-04 10:19:50 +0200 |
---|---|---|
committer | Mathias <mathias@spray.cc> | 2011-10-04 10:19:50 +0200 |
commit | 3df2a713d9251d62efcbf12165c6b81460a02047 (patch) | |
tree | 4e5b4b3cf7a2160e25752236da3821ace2aa46fb /src/test/scala/cc/spray | |
parent | f21c3ebeede766e89a5409ef1bec21b4135ff62f (diff) | |
parent | e95eb217857ebf6eff343b4ed14815db804d9009 (diff) | |
download | spray-json-3df2a713d9251d62efcbf12165c6b81460a02047.tar.gz spray-json-3df2a713d9251d62efcbf12165c6b81460a02047.tar.bz2 spray-json-3df2a713d9251d62efcbf12165c6b81460a02047.zip |
Merge branch 'release/1.0.1'
Conflicts:
README.markdown
Diffstat (limited to 'src/test/scala/cc/spray')
9 files changed, 100 insertions, 19 deletions
diff --git a/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala b/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala new file mode 100644 index 0000000..7368128 --- /dev/null +++ b/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala @@ -0,0 +1,41 @@ +package cc.spray.json + +import org.specs2.mutable._ + +class AdditionalFormatsSpec extends Specification { + + case class Container[A](inner: Option[A]) + + object ReaderProtocol extends DefaultJsonProtocol { + implicit def containerReader[T :JsonFormat] = lift { + new JsonReader[Container[T]] { + def read(value: JsValue) = value match { + case JsObject(JsField("content", obj: JsValue) :: Nil) => Container(Some(jsonReader[T].read(obj))) + case _ => throw new DeserializationException("Unexpected format: " + value.toString) + } + } + } + } + + object WriterProtocol extends DefaultJsonProtocol { + implicit def containerWriter[T :JsonFormat] = lift { + new JsonWriter[Container[T]] { + def write(obj: Container[T]) = JsObject(JsField("content", obj.inner.toJson)) + } + } + } + + "The liftJsonWriter" should { + val obj = Container(Some(Container(Some(List(1, 2, 3))))) + + "properly write a Container[Container[List[Int]]] to JSON" in { + import WriterProtocol._ + obj.toJson.toString mustEqual """{"content":{"content":[1,2,3]}}""" + } + + "properly read a Container[Container[List[Int]]] from JSON" in { + import ReaderProtocol._ + JsonParser("""{"content":{"content":[1,2,3]}}""").fromJson[Container[Container[List[Int]]]] mustEqual obj + } + } +}
\ No newline at end of file diff --git a/src/test/scala/cc/spray/json/BasicFormatsSpec.scala b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala index c26d302..fcae4bb 100644 --- a/src/test/scala/cc/spray/json/BasicFormatsSpec.scala +++ b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala @@ -1,6 +1,6 @@ package cc.spray.json -import org.specs.Specification +import org.specs2.mutable._ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { diff --git a/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala b/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala index 126b96a..d0cb509 100644 --- a/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala +++ b/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala @@ -1,6 +1,6 @@ package cc.spray.json -import org.specs.Specification +import org.specs2.mutable._ import java.util.Arrays class CollectionFormatsSpec extends Specification with DefaultJsonProtocol { diff --git a/src/test/scala/cc/spray/json/CompactPrinterSpec.scala b/src/test/scala/cc/spray/json/CompactPrinterSpec.scala index 4ca4aac..3bc4870 100644 --- a/src/test/scala/cc/spray/json/CompactPrinterSpec.scala +++ b/src/test/scala/cc/spray/json/CompactPrinterSpec.scala @@ -1,6 +1,6 @@ package cc.spray.json -import org.specs.Specification +import org.specs2.mutable._ class CompactPrinterSpec extends Specification { diff --git a/src/test/scala/cc/spray/json/JsonParserSpec.scala b/src/test/scala/cc/spray/json/JsonParserSpec.scala index 6402b3d..0860d05 100644 --- a/src/test/scala/cc/spray/json/JsonParserSpec.scala +++ b/src/test/scala/cc/spray/json/JsonParserSpec.scala @@ -1,6 +1,6 @@ package cc.spray.json -import org.specs.Specification +import org.specs2.mutable._ import org.parboiled.common.FileUtils class JsonParserSpec extends Specification { diff --git a/src/test/scala/cc/spray/json/PrettyPrinterSpec.scala b/src/test/scala/cc/spray/json/PrettyPrinterSpec.scala index 541abc9..feed8a9 100644 --- a/src/test/scala/cc/spray/json/PrettyPrinterSpec.scala +++ b/src/test/scala/cc/spray/json/PrettyPrinterSpec.scala @@ -1,6 +1,6 @@ package cc.spray.json -import org.specs.Specification +import org.specs2.mutable._ class PrettyPrinterSpec extends Specification { diff --git a/src/test/scala/cc/spray/json/ProductFormatsSpec.scala b/src/test/scala/cc/spray/json/ProductFormatsSpec.scala index 5f253c2..9a692ec 100644 --- a/src/test/scala/cc/spray/json/ProductFormatsSpec.scala +++ b/src/test/scala/cc/spray/json/ProductFormatsSpec.scala @@ -1,31 +1,71 @@ package cc.spray.json -import org.specs.Specification +import org.specs2.mutable._ -class ProductFormatsSpec extends Specification with DefaultJsonProtocol { +class ProductFormatsSpec extends Specification { - case class Test2(a: Int, b: Double) - implicit val test2Format = jsonFormat(Test2, "a", "b") - - "A JsonFormat created with format, for a case class with 2 elements," should { - val obj = Test2(42, 4.2) + case class Test2(a: Int, b: Option[Double]) + case class Test3[A, B](as: List[A], bs: List[B]) + + trait TestProtocol { + this: DefaultJsonProtocol => + implicit val test2Format = jsonFormat(Test2, "a", "b") + implicit def test3Format[A: JsonFormat, B: JsonFormat] = jsonFormat(Test3.apply[A, B], "as", "bs") + } + object TestProtocol1 extends DefaultJsonProtocol with TestProtocol + object TestProtocol2 extends DefaultJsonProtocol with TestProtocol with NullOptions + + "A JsonFormat created with `jsonFormat`, for a case class with 2 elements," should { + import TestProtocol1._ + val obj = Test2(42, Some(4.2)) val json = JsObject(JsField("a", 42), JsField("b", 4.2)) "convert to a respective JsObject" in { obj.toJson mustEqual json } "convert a JsObject to the respective case class instance" in { - json.fromJson[Test2] mustEqual obj + json.fromJson[Test2] mustEqual obj } - "throw a DeserializationException if the JsObject does not define the right members" in ( - JsObject(JsField("a", 42), JsField("x", 4.2)).fromJson[Test2] must - throwA(new DeserializationException("Object is missing required member 'b'")) + "throw a DeserializationException if the JsObject does not all required members" in ( + JsObject(JsField("b", 4.2)).fromJson[Test2] must + throwA(new DeserializationException("Object is missing required member 'a'")) ) + "not require the presence of optional fields for deserialization" in { + JsObject(JsField("a", 42)).fromJson[Test2] mustEqual Test2(42, None) + } + "not render `None` members during serialization" in { + Test2(42, None).toJson mustEqual JsObject(JsField("a", 42)) + } "ignore additional members during deserialization" in { JsObject(JsField("a", 42), JsField("b", 4.2), JsField("c", 'no)).fromJson[Test2] mustEqual obj } + "not depend on any specific member order for deserialization" in { + JsObject(JsField("b", 4.2), JsField("a", 42)).fromJson[Test2] mustEqual obj + } "throw a DeserializationException if the JsValue is not a JsObject" in ( JsNull.fromJson[Test2] must throwA(new DeserializationException("Object expected")) ) } - + + "A JsonProtocol mixing in NullOptions" should { + "render `None` members to `null`" in { + import TestProtocol2._ + Test2(42, None).toJson mustEqual JsObject(JsField("a", 42), JsField("b", JsNull)) + } + } + + "A JsonFormat for a generic case class and created with `jsonFormat`" should { + import TestProtocol1._ + val obj = Test3(42 :: 43 :: Nil, "x" :: "y" :: "z" :: Nil) + val json = JsObject( + JsField("as", JsArray(JsNumber(42), JsNumber(43))), + JsField("bs", JsArray(JsString("x"), JsString("y"), JsString("z"))) + ) + "convert to a respective JsObject" in { + obj.toJson mustEqual json + } + "convert a JsObject to the respective case class instance" in { + json.fromJson[Test3[Int, String]] mustEqual obj + } + } + } diff --git a/src/test/scala/cc/spray/json/ReadmeSpec.scala b/src/test/scala/cc/spray/json/ReadmeSpec.scala index 8669c0d..843d1fc 100644 --- a/src/test/scala/cc/spray/json/ReadmeSpec.scala +++ b/src/test/scala/cc/spray/json/ReadmeSpec.scala @@ -1,6 +1,6 @@ package cc.spray.json -import org.specs.Specification +import org.specs2.mutable._ class ReadmeSpec extends Specification { diff --git a/src/test/scala/cc/spray/json/StandardFormatsSpec.scala b/src/test/scala/cc/spray/json/StandardFormatsSpec.scala index 8005c9f..ad9485c 100644 --- a/src/test/scala/cc/spray/json/StandardFormatsSpec.scala +++ b/src/test/scala/cc/spray/json/StandardFormatsSpec.scala @@ -1,6 +1,6 @@ package cc.spray.json -import org.specs.Specification +import org.specs2.mutable._ import scala.Right class StandardFormatsSpec extends Specification with DefaultJsonProtocol { |