summaryrefslogtreecommitdiff
path: root/src/test/scala/cc/spray/json/formats
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
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')
-rw-r--r--src/test/scala/cc/spray/json/formats/BasicFormatsSpec.scala123
-rw-r--r--src/test/scala/cc/spray/json/formats/CollectionFormatsSpec.scala67
-rw-r--r--src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala32
-rw-r--r--src/test/scala/cc/spray/json/formats/StandardFormatsSpec.scala42
4 files changed, 264 insertions, 0 deletions
diff --git a/src/test/scala/cc/spray/json/formats/BasicFormatsSpec.scala b/src/test/scala/cc/spray/json/formats/BasicFormatsSpec.scala
new file mode 100644
index 0000000..8f190b4
--- /dev/null
+++ b/src/test/scala/cc/spray/json/formats/BasicFormatsSpec.scala
@@ -0,0 +1,123 @@
+package cc.spray.json
+package formats
+
+import org.specs.Specification
+
+class BasicFormatsSpec extends Specification with BasicFormats {
+
+ "The IntJsonFormat" should {
+ "convert an Int to a JsNumber" in {
+ 42.toJson mustEqual JsNumber(42)
+ }
+ "convert a JsNumber to an Int" in {
+ JsNumber(42).fromJson[Int] mustEqual 42
+ }
+ }
+
+ "The LongJsonFormat" should {
+ "convert a Long to a JsNumber" in {
+ 42L.toJson mustEqual JsNumber(42L)
+ }
+ "convert a JsNumber to a Long" in {
+ JsNumber(42L).fromJson[Long] mustEqual 42L
+ }
+ }
+
+ "The FloatJsonFormat" should {
+ "convert a Float to a JsNumber" in {
+ 4.2f.toJson mustEqual JsNumber(4.2f)
+ }
+ "convert a JsNumber to a Float" in {
+ JsNumber(4.2f).fromJson[Float] mustEqual 4.2f
+ }
+ }
+
+ "The DoubleJsonFormat" should {
+ "convert a Double to a JsNumber" in {
+ 4.2.toJson mustEqual JsNumber(4.2)
+ }
+ "convert a JsNumber to a Double" in {
+ JsNumber(4.2).fromJson[Double] mustEqual 4.2
+ }
+ }
+
+ "The ByteJsonFormat" should {
+ "convert a Byte to a JsNumber" in {
+ 42.asInstanceOf[Byte].toJson mustEqual JsNumber(42)
+ }
+ "convert a JsNumber to a Byte" in {
+ JsNumber(42).fromJson[Byte] mustEqual 42
+ }
+ }
+
+ "The ShortJsonFormat" should {
+ "convert a Short to a JsNumber" in {
+ 42.asInstanceOf[Short].toJson mustEqual JsNumber(42)
+ }
+ "convert a JsNumber to a Short" in {
+ JsNumber(42).fromJson[Short] mustEqual 42
+ }
+ }
+
+ "The BigDecimalJsonFormat" should {
+ "convert a BigDecimal to a JsNumber" in {
+ BigDecimal(42).toJson mustEqual JsNumber(42)
+ }
+ "convert a JsNumber to a BigDecimal" in {
+ JsNumber(42).fromJson[BigDecimal] mustEqual BigDecimal(42)
+ }
+ }
+
+ "The BigIntJsonFormat" should {
+ "convert a BigInt to a JsNumber" in {
+ BigInt(42).toJson mustEqual JsNumber(42)
+ }
+ "convert a JsNumber to a BigInt" in {
+ JsNumber(42).fromJson[BigInt] mustEqual BigInt(42)
+ }
+ }
+
+ "The UnitJsonFormat" should {
+ "convert Unit to a JsNumber(1)" in {
+ ().toJson mustEqual JsNumber(1)
+ }
+ "convert a JsNumber to Unit" in {
+ JsNumber(1).fromJson[Unit] mustEqual ()
+ }
+ }
+
+ "The BooleanJsonFormat" should {
+ "convert true to a JsTrue" in { true.toJson mustEqual JsTrue }
+ "convert false to a JsFalse" in { false.toJson mustEqual JsFalse }
+ "convert a JsTrue to true" in { JsTrue.fromJson[Boolean] mustEqual true }
+ "convert a JsFalse to false" in { JsFalse.fromJson[Boolean] mustEqual false }
+ }
+
+ "The CharJsonFormat" should {
+ "convert a Char to a JsString" in {
+ 'c'.toJson mustEqual JsString("c")
+ }
+ "convert a JsString to a Char" in {
+ JsString("c").fromJson[Char] mustEqual 'c'
+ }
+ }
+
+ "The StringJsonFormat" should {
+ "convert a String to a JsString" in {
+ "Hello".toJson mustEqual JsString("Hello")
+ }
+ "convert a JsString to a String" in {
+ JsString("Hello").fromJson[String] mustEqual "Hello"
+ }
+ }
+
+ "The SymbolJsonFormat" should {
+ "convert a Symbol to a JsString" in {
+ 'Hello.toJson mustEqual JsString("Hello")
+ }
+ "convert a JsString to a Symbol" in {
+ JsString("Hello").fromJson[Symbol] mustEqual 'Hello
+ }
+ }
+
+} \ No newline at end of file
diff --git a/src/test/scala/cc/spray/json/formats/CollectionFormatsSpec.scala b/src/test/scala/cc/spray/json/formats/CollectionFormatsSpec.scala
new file mode 100644
index 0000000..2546146
--- /dev/null
+++ b/src/test/scala/cc/spray/json/formats/CollectionFormatsSpec.scala
@@ -0,0 +1,67 @@
+package cc.spray.json
+package formats
+
+import org.specs.Specification
+import java.util.Arrays
+
+class CollectionFormatsSpec extends Specification with CollectionFormats with BasicFormats {
+
+ "The listFormat" should {
+ val list = List(1, 2, 3)
+ val json = JsArray(JsNumber(1), JsNumber(2), JsNumber(3))
+ "convert a List[Int] to a JsArray of JsNumbers" in {
+ list.toJson mustEqual json
+ }
+ "convert a JsArray of JsNumbers to a List[Int]" in {
+ json.fromJson[List[Int]] mustEqual list
+ }
+ }
+
+ "The arrayFormat" should {
+ val array = Array(1, 2, 3)
+ val json = JsArray(JsNumber(1), JsNumber(2), JsNumber(3))
+ "convert an Array[Int] to a JsArray of JsNumbers" in {
+ array.toJson mustEqual json
+ }
+ "convert a JsArray of JsNumbers to an Array[Int]" in {
+ Arrays.equals(json.fromJson[Array[Int]], array) must beTrue
+ }
+ }
+
+ "The mapFormat" should {
+ val map = Map("a" -> 1, "b" -> 2, "c" -> 3)
+ val json = JsObject(JsField("a", 1), JsField("b", 2), JsField("c", 3))
+ "convert a Map[String, Long] to a JsObject" in {
+ map.toJson mustEqual json
+ }
+ "be able to convert a JsObject to a Map[String, Long]" in {
+ json.fromJson[Map[String, Long]] mustEqual map
+ }
+ "throw an Exception when trying to serialize a map whose key are not serialized to JsStrings" in {
+ Map(1 -> "a").toJson must throwA(new SerializationException("Map key must be formatted as JsString, not '1'"))
+ }
+ }
+
+ "The immutableSetFormat" should {
+ val set = Set(1, 2, 3)
+ val json = JsArray(JsNumber(1), JsNumber(2), JsNumber(3))
+ "convert a Set[Int] to a JsArray of JsNumbers" in {
+ set.toJson mustEqual json
+ }
+ "convert a JsArray of JsNumbers to a Set[Int]" in {
+ json.fromJson[Set[Int]] mustEqual set
+ }
+ }
+
+ "The mutableSetFormat" should {
+ val set = collection.mutable.Set(1, 2, 3)
+ val json = JsArray(JsNumber(3), JsNumber(1), JsNumber(2))
+ "convert a collection.mutable.Set[Int] to a JsArray of JsNumbers" in {
+ set.toJson mustEqual json
+ }
+ "convert a JsArray of JsNumbers to a collection.mutable.Set[Int]" in {
+ json.fromJson[collection.mutable.Set[Int]] mustEqual set
+ }
+ }
+
+} \ No newline at end of file
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"))
+ )
+ }
+
+}
diff --git a/src/test/scala/cc/spray/json/formats/StandardFormatsSpec.scala b/src/test/scala/cc/spray/json/formats/StandardFormatsSpec.scala
new file mode 100644
index 0000000..201d997
--- /dev/null
+++ b/src/test/scala/cc/spray/json/formats/StandardFormatsSpec.scala
@@ -0,0 +1,42 @@
+package cc.spray.json
+package formats
+
+import org.specs.Specification
+
+class StandardFormatsSpec extends Specification with StandardFormats with BasicFormats {
+
+ "The optionFormat" should {
+ "convert None to JsNull" in {
+ None.asInstanceOf[Option[Int]].toJson mustEqual JsNull
+ }
+ "convert JsNull to None" in {
+ JsNull.fromJson[Option[Int]] mustEqual None
+ }
+ "convert Some(Hello) to JsString(Hello)" in {
+ Some("Hello").asInstanceOf[Option[String]].toJson mustEqual JsString("Hello")
+ }
+ "convert JsString(Hello) to Some(Hello)" in {
+ JsString("Hello").fromJson[Option[String]] mustEqual Some("Hello")
+ }
+ }
+
+ "The tuple1Format" should {
+ "convert (42) to a JsNumber" in {
+ Tuple1(42).toJson mustEqual JsNumber(42)
+ }
+ "be able to convert a JsNumber to a Tuple1[Int]" in {
+ JsNumber(42).fromJson[Tuple1[Int]] mustEqual Tuple1(42)
+ }
+ }
+
+ "The tuple2Format" should {
+ val json = JsArray(JsNumber(42), JsNumber(4.2))
+ "convert (42, 4.2) to a JsArray" in {
+ (42, 4.2).toJson mustEqual json
+ }
+ "be able to convert a JsArray to a (Int, Double)]" in {
+ json.fromJson[(Int, Double)] mustEqual (42, 4.2)
+ }
+ }
+
+} \ No newline at end of file