diff options
Diffstat (limited to 'src/test/scala/cc')
7 files changed, 407 insertions, 0 deletions
diff --git a/src/test/scala/cc/spray/json/CompactPrinterSpec.scala b/src/test/scala/cc/spray/json/CompactPrinterSpec.scala new file mode 100644 index 0000000..a517cd1 --- /dev/null +++ b/src/test/scala/cc/spray/json/CompactPrinterSpec.scala @@ -0,0 +1,48 @@ +package cc.spray.json + +import org.specs.Specification + +class CompactPrinterSpec extends Specification { + + "The CompactPrinter" should { + "print JsNull to 'null'" in { + CompactPrinter(JsNull) mustEqual "null" + } + "print JsTrue to 'true'" in { + CompactPrinter(JsTrue) mustEqual "true" + } + "print JsFalse to 'false'" in { + CompactPrinter(JsFalse) mustEqual "false" + } + "print JsNumber(0) to '0'" in { + CompactPrinter(JsNumber(0)) mustEqual "0" + } + "print JsNumber(1.23) to '1.23'" in { + CompactPrinter(JsNumber(1.23)) mustEqual "1.23" + } + "print JsNumber(1.23) to '1.23'" in { + CompactPrinter(JsNumber(1.23)) mustEqual "1.23" + } + "print JsNumber(-1E10) to '-1E10'" in { + CompactPrinter(JsNumber(-1E10)) mustEqual "-1.0E+10" + } + "print JsNumber(12.34e-10) to '12.34e-10'" in { + CompactPrinter(JsNumber(12.34e-10)) mustEqual "1.234E-9" + } + "print JsString(\"xyz\") to \"xyz\"" in { + CompactPrinter(JsString("xyz")) mustEqual "\"xyz\"" + } + "properly escape special chars in JsString" in { + CompactPrinter(JsString("\"\\\b\f\n\r\t\u12AB")) mustEqual """"\"\\\b\f\n\r\t""" + "\\u12ab\"" + } + "properly print a simple JsObject" in ( + CompactPrinter(JsObject(JsField("key", 42), JsField("key2", "value"))) + mustEqual """{"key":42,"key2":"value"}""" + ) + "properly print a simple JsArray" in ( + CompactPrinter(JsArray(JsNull, JsNumber(1.23), JsObject(JsField("key", true)))) + mustEqual """[null,1.23,{"key":true}]""" + ) + } + +}
\ No newline at end of file diff --git a/src/test/scala/cc/spray/json/JsonParserSpec.scala b/src/test/scala/cc/spray/json/JsonParserSpec.scala new file mode 100644 index 0000000..dd61f94 --- /dev/null +++ b/src/test/scala/cc/spray/json/JsonParserSpec.scala @@ -0,0 +1,45 @@ +package cc.spray.json + +import org.specs.Specification + +class JsonParserSpec extends Specification { + + "The JsonParser" should { + "parse 'null' to JsNull" in { + JsonParser("null") mustEqual JsNull + } + "parse 'true' to JsTrue" in { + JsonParser("true") mustEqual JsTrue + } + "parse 'false' to JsFalse" in { + JsonParser("false") mustEqual JsFalse + } + "parse '0' to JsNumber" in { + JsonParser("0") mustEqual JsNumber(0) + } + "parse '1.23' to JsNumber" in { + JsonParser("1.23") mustEqual JsNumber(1.23) + } + "parse '-1E10' to JsNumber" in { + JsonParser("-1E10") mustEqual JsNumber("-1E+10") + } + "parse '12.34e-10' to JsNumber" in { + JsonParser("12.34e-10") mustEqual JsNumber("1.234E-9") + } + "parse \"xyz\" to JsString" in { + JsonParser("\"xyz\"") mustEqual JsString("xyz") + } + "parse escapes in a JsString" in { + JsonParser(""""\"\\/\b\f\n\r\t\u12Ab"""") mustEqual JsString("\"\\/\b\f\n\r\t\u12ab") + } + "properly parse a simple JsObject" in ( + JsonParser(""" { "key" :42, "key2": "value" }""") mustEqual + JsObject(JsField("key", 42), JsField("key2", "value")) + ) + "properly parse a simple JsArray" in ( + JsonParser("""[null, 1.23 ,{"key":true } ] """) mustEqual + JsArray(JsNull, JsNumber(1.23), JsObject(JsField("key", true))) + ) + } + +}
\ No newline at end of file diff --git a/src/test/scala/cc/spray/json/PrettyPrinterSpec.scala b/src/test/scala/cc/spray/json/PrettyPrinterSpec.scala new file mode 100644 index 0000000..541abc9 --- /dev/null +++ b/src/test/scala/cc/spray/json/PrettyPrinterSpec.scala @@ -0,0 +1,50 @@ +package cc.spray.json + +import org.specs.Specification + +class PrettyPrinterSpec extends Specification { + + "The PrettyPrinter" should { + "print a more complicated JsObject nicely aligned" in { + PrettyPrinter { + JsonParser { + """|{ + | "simpleKey" : "some value", + | "key with spaces": null, + | "zero": 0, + | "number": -1.2323424E-5, + | "Boolean yes":true, + | "Boolean no": false, + | "Unic\u00f8de" : "Long string with newline\nescape", + | "key with \"quotes\"" : "string", + | "sub object" : { + | "sub key": 26.5, + | "a": "b", + | "array": [1, 2, { "yes":1, "no":0 }, ["a", "b", null], false] + | } + |}""".stripMargin + } + } mustEqual { + """|{ + | "simpleKey": "some value", + | "key with spaces": null, + | "zero": 0, + | "number": -0.000012323424, + | "Boolean yes": true, + | "Boolean no": false, + | "Unic\u00f8de": "Long string with newline\nescape", + | "key with \"quotes\"": "string", + | "sub object": { + | "sub key": 26.5, + | "a": "b", + | "array": [1, 2, { + | "yes": 1, + | "no": 0 + | }, ["a", "b", null], false] + | } + |}""".stripMargin.replace("\u00f8", "\\u00f8") + } + } + } + +}
\ 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 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 |