From 5f6afe658a47bc7c8cce6f47357ead086495f6d3 Mon Sep 17 00:00:00 2001 From: Mathias Date: Mon, 9 May 2011 10:11:53 +0200 Subject: Squash obsolete 'formats' package also in test sources --- .../scala/cc/spray/json/BasicFormatsSpec.scala | 122 ++++++++++++++++++++ .../cc/spray/json/CollectionFormatsSpec.scala | 66 +++++++++++ .../scala/cc/spray/json/GenericFormatsSpec.scala | 31 ++++++ .../scala/cc/spray/json/StandardFormatsSpec.scala | 41 +++++++ .../cc/spray/json/formats/BasicFormatsSpec.scala | 123 --------------------- .../spray/json/formats/CollectionFormatsSpec.scala | 67 ----------- .../cc/spray/json/formats/GenericFormatsSpec.scala | 32 ------ .../spray/json/formats/StandardFormatsSpec.scala | 42 ------- 8 files changed, 260 insertions(+), 264 deletions(-) create mode 100644 src/test/scala/cc/spray/json/BasicFormatsSpec.scala create mode 100644 src/test/scala/cc/spray/json/CollectionFormatsSpec.scala create mode 100644 src/test/scala/cc/spray/json/GenericFormatsSpec.scala create mode 100644 src/test/scala/cc/spray/json/StandardFormatsSpec.scala delete mode 100644 src/test/scala/cc/spray/json/formats/BasicFormatsSpec.scala delete mode 100644 src/test/scala/cc/spray/json/formats/CollectionFormatsSpec.scala delete mode 100644 src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala delete mode 100644 src/test/scala/cc/spray/json/formats/StandardFormatsSpec.scala (limited to 'src/test/scala/cc/spray') diff --git a/src/test/scala/cc/spray/json/BasicFormatsSpec.scala b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala new file mode 100644 index 0000000..7207db5 --- /dev/null +++ b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala @@ -0,0 +1,122 @@ +package cc.spray.json + +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/CollectionFormatsSpec.scala b/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala new file mode 100644 index 0000000..58175ac --- /dev/null +++ b/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala @@ -0,0 +1,66 @@ +package cc.spray.json + +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/GenericFormatsSpec.scala b/src/test/scala/cc/spray/json/GenericFormatsSpec.scala new file mode 100644 index 0000000..b284d42 --- /dev/null +++ b/src/test/scala/cc/spray/json/GenericFormatsSpec.scala @@ -0,0 +1,31 @@ +package cc.spray.json + +import org.specs.Specification + +class GenericFormatsSpec extends Specification with GenericFormats with BasicFormats { + + 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) + 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/StandardFormatsSpec.scala b/src/test/scala/cc/spray/json/StandardFormatsSpec.scala new file mode 100644 index 0000000..00561e0 --- /dev/null +++ b/src/test/scala/cc/spray/json/StandardFormatsSpec.scala @@ -0,0 +1,41 @@ +package cc.spray.json + +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 diff --git a/src/test/scala/cc/spray/json/formats/BasicFormatsSpec.scala b/src/test/scala/cc/spray/json/formats/BasicFormatsSpec.scala deleted file mode 100644 index 8f190b4..0000000 --- a/src/test/scala/cc/spray/json/formats/BasicFormatsSpec.scala +++ /dev/null @@ -1,123 +0,0 @@ -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 deleted file mode 100644 index 2546146..0000000 --- a/src/test/scala/cc/spray/json/formats/CollectionFormatsSpec.scala +++ /dev/null @@ -1,67 +0,0 @@ -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 deleted file mode 100644 index 60f4494..0000000 --- a/src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala +++ /dev/null @@ -1,32 +0,0 @@ -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 = jsonFormat(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 deleted file mode 100644 index 201d997..0000000 --- a/src/test/scala/cc/spray/json/formats/StandardFormatsSpec.scala +++ /dev/null @@ -1,42 +0,0 @@ -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 -- cgit v1.2.3