summaryrefslogtreecommitdiff
path: root/src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala
diff options
context:
space:
mode:
authorMathias <mathias@spray.cc>2011-05-06 11:11:37 +0200
committerMathias <mathias@spray.cc>2011-05-06 11:11:37 +0200
commit0ce9cf8fce1dc475f3bb2a517e0a7698c9e0a5d9 (patch)
tree62de608642a30ec478411d27e94179ec64c5bd17 /src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala
downloadspray-json-0ce9cf8fce1dc475f3bb2a517e0a7698c9e0a5d9.tar.gz
spray-json-0ce9cf8fce1dc475f3bb2a517e0a7698c9e0a5d9.tar.bz2
spray-json-0ce9cf8fce1dc475f3bb2a517e0a7698c9e0a5d9.zip
Initial commit (split off from main spray codebase)
Diffstat (limited to 'src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala')
-rw-r--r--src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala b/src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala
new file mode 100644
index 0000000..adc6fcb
--- /dev/null
+++ b/src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala
@@ -0,0 +1,32 @@
+package cc.spray.json
+package formats
+
+import org.specs.Specification
+
+class GenericFormatsSpec extends Specification with GenericFormats with BasicFormats {
+
+ case class Test2(a: Int, b: Double)
+ implicit val test2Format = format(Test2, "a", "b")
+
+ "A JsonFormat created with format, for a case class with 2 elements," should {
+ val obj = Test2(42, 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
+ }
+ "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'"))
+ )
+ "ignore additional members during deserialization" in {
+ JsObject(JsField("a", 42), JsField("b", 4.2), JsField("c", 'no)).fromJson[Test2] mustEqual obj
+ }
+ "throw a DeserializationException if the JsValue is not a JsObject" in (
+ JsNull.fromJson[Test2] must throwA(new DeserializationException("Object expected"))
+ )
+ }
+
+}