diff options
Diffstat (limited to 'src/test/scala/spray')
-rw-r--r-- | src/test/scala/spray/json/JsonParserSpec.scala | 62 | ||||
-rw-r--r-- | src/test/scala/spray/json/PrettyPrinterSpec.scala | 74 | ||||
-rw-r--r-- | src/test/scala/spray/json/ReadmeSpec.scala | 2 | ||||
-rw-r--r-- | src/test/scala/spray/json/RoundTripSpecs.scala | 8 |
4 files changed, 83 insertions, 63 deletions
diff --git a/src/test/scala/spray/json/JsonParserSpec.scala b/src/test/scala/spray/json/JsonParserSpec.scala index c6c1589..4968eee 100644 --- a/src/test/scala/spray/json/JsonParserSpec.scala +++ b/src/test/scala/spray/json/JsonParserSpec.scala @@ -17,56 +17,78 @@ package spray.json import org.specs2.mutable._ -import org.parboiled.common.FileUtils class JsonParserSpec extends Specification { "The JsonParser" should { "parse 'null' to JsNull" in { - JsonParser("null") mustEqual JsNull + JsonParser("null") === JsNull } "parse 'true' to JsTrue" in { - JsonParser("true") mustEqual JsTrue + JsonParser("true") === JsTrue } "parse 'false' to JsFalse" in { - JsonParser("false") mustEqual JsFalse + JsonParser("false") === JsFalse } "parse '0' to JsNumber" in { - JsonParser("0") mustEqual JsNumber(0) + JsonParser("0") === JsNumber(0) } "parse '1.23' to JsNumber" in { - JsonParser("1.23") mustEqual JsNumber(1.23) + JsonParser("1.23") === JsNumber(1.23) } "parse '-1E10' to JsNumber" in { - JsonParser("-1E10") mustEqual JsNumber("-1E+10") + JsonParser("-1E10") === JsNumber("-1E+10") } "parse '12.34e-10' to JsNumber" in { - JsonParser("12.34e-10") mustEqual JsNumber("1.234E-9") + JsonParser("12.34e-10") === JsNumber("1.234E-9") } "parse \"xyz\" to JsString" in { - JsonParser("\"xyz\"") mustEqual JsString("xyz") + JsonParser("\"xyz\"") === JsString("xyz") } "parse escapes in a JsString" in { - JsonParser(""""\"\\/\b\f\n\r\t"""") mustEqual JsString("\"\\/\b\f\n\r\t") - JsonParser("\"L\\" + "u00e4nder\"") mustEqual JsString("Länder") + JsonParser(""""\"\\/\b\f\n\r\t"""") === JsString("\"\\/\b\f\n\r\t") + JsonParser("\"L\\" + "u00e4nder\"") === JsString("Länder") } "parse all representations of the slash (SOLIDUS) character in a JsString" in { - JsonParser( "\"" + "/\\/\\u002f" + "\"") mustEqual JsString("///") + JsonParser( "\"" + "/\\/\\u002f" + "\"") === JsString("///") } - "properly parse a simple JsObject" in ( - JsonParser(""" { "key" :42, "key2": "value" }""") mustEqual + "parse a simple JsObject" in ( + JsonParser(""" { "key" :42, "key2": "value" }""") === JsObject("key" -> JsNumber(42), "key2" -> JsString("value")) ) - "properly parse a simple JsArray" in ( - JsonParser("""[null, 1.23 ,{"key":true } ] """) mustEqual - JsArray(JsNull, JsNumber(1.23), JsObject("key" -> JsBoolean(true))) + "parse a simple JsArray" in ( + JsonParser("""[null, 1.23 ,{"key":true } ] """) === + JsArray(JsNull, JsNumber(1.23), JsObject("key" -> JsTrue)) ) + "parse directly from UTF-8 encoded bytes" in { + val json = JsObject( + "7-bit" -> JsString("This is regular 7-bit ASCII text."), + "2-bytes" -> JsString("2-byte UTF-8 chars like £, æ or Ö"), + "3-bytes" -> JsString("3-byte UTF-8 chars like ヨ, ᄅ or ᐁ.")) + JsonParser(json.prettyPrint.getBytes("UTF-8")) === json + } "be reentrant" in { - val largeJsonSource = FileUtils.readAllCharsFromResource("test.json") + val largeJsonSource = scala.io.Source.fromInputStream(getClass.getResourceAsStream("/test.json")).mkString List.fill(20)(largeJsonSource).par.map(JsonParser(_)).toList.map { _.asInstanceOf[JsObject].fields("questions").asInstanceOf[JsArray].elements.size - } mustEqual List.fill(20)(100) + } === List.fill(20)(100) } - } + "produce proper error messages" in { + def errorMessage(input: String) = + try JsonParser(input) catch { case e: JsonParser.ParsingException => e.getMessage } + + errorMessage("""[null, 1.23 {"key":true } ]""") === + """Unexpected character '{' at input index 12 (line 1, position 13), expected ']': + |[null, 1.23 {"key":true } ] + | ^ + |""".stripMargin + + errorMessage("""[null, 1.23, { key":true } ]""") === + """Unexpected character 'k' at input index 16 (line 1, position 17), expected '"': + |[null, 1.23, { key":true } ] + | ^ + |""".stripMargin + } + } }
\ No newline at end of file diff --git a/src/test/scala/spray/json/PrettyPrinterSpec.scala b/src/test/scala/spray/json/PrettyPrinterSpec.scala index 27137a8..6354ef0 100644 --- a/src/test/scala/spray/json/PrettyPrinterSpec.scala +++ b/src/test/scala/spray/json/PrettyPrinterSpec.scala @@ -16,49 +16,49 @@ package spray.json +import scala.collection.immutable.ListMap import org.specs2.mutable._ 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 + val JsObject(fields) = JsonParser { + """{ + | "Boolean no": false, + | "Boolean yes":true, + | "Unic\u00f8de" : "Long string with newline\nescape", + | "key with \"quotes\"" : "string", + | "key with spaces": null, + | "number": -1.2323424E-5, + | "simpleKey" : "some value", + | "sub object" : { + | "sub key": 26.5, + | "a": "b", + | "array": [1, 2, { "yes":1, "no":0 }, ["a", "b", null], false] + | }, + | "zero": 0 + |}""".stripMargin + } + PrettyPrinter(JsObject(ListMap(fields.toSeq.sortBy(_._1):_*))) mustEqual { + """{ + | "Boolean no": false, + | "Boolean yes": true, + | "Unic\u00f8de": "Long string with newline\nescape", + | "key with \"quotes\"": "string", + | "key with spaces": null, + | "number": -0.000012323424, + | "simpleKey": "some value", + | "sub object": { + | "sub key": 26.5, + | "a": "b", + | "array": [1, 2, { + | "yes": 1, + | "no": 0 + | }, ["a", "b", null], false] + | }, + | "zero": 0 + |}""".stripMargin } } } diff --git a/src/test/scala/spray/json/ReadmeSpec.scala b/src/test/scala/spray/json/ReadmeSpec.scala index 51a1ec5..306b656 100644 --- a/src/test/scala/spray/json/ReadmeSpec.scala +++ b/src/test/scala/spray/json/ReadmeSpec.scala @@ -60,7 +60,7 @@ class ReadmeSpec extends Specification { JsArray(JsString(c.name), JsNumber(c.red), JsNumber(c.green), JsNumber(c.blue)) def read(value: JsValue) = value match { - case JsArray(JsString(name) :: JsNumber(red) :: JsNumber(green) :: JsNumber(blue) :: Nil) => + case JsArray(Seq(JsString(name), JsNumber(red), JsNumber(green), JsNumber(blue))) => new Color(name, red.toInt, green.toInt, blue.toInt) case _ => deserializationError("Color expected") } diff --git a/src/test/scala/spray/json/RoundTripSpecs.scala b/src/test/scala/spray/json/RoundTripSpecs.scala index 51df48d..7609a53 100644 --- a/src/test/scala/spray/json/RoundTripSpecs.scala +++ b/src/test/scala/spray/json/RoundTripSpecs.scala @@ -8,9 +8,7 @@ object JsValueGenerators { import Gen._ import Arbitrary.arbitrary - // some characters have special meaning in parboiled - // see org.parboiled.support.Chars, we have to exclude those - val parseableString: Gen[String] = arbitrary[String].map(_.filterNot(_ > 0xfd00)) + val parseableString: Gen[String] = arbitrary[String] val genString: Gen[JsString] = parseableString.map(JsString(_)) val genBoolean: Gen[JsBoolean] = oneOf(JsFalse, JsTrue) val genLongNumber: Gen[JsNumber] = arbitrary[Long].map(JsNumber(_)) @@ -22,7 +20,7 @@ object JsValueGenerators { for { n <- choose(0, 15) els <- Gen.containerOfN[List, JsValue](n, genValue(depth - 1)) - } yield JsArray(els) + } yield JsArray(els.toVector) def genField(depth: Int): Gen[(String, JsValue)] = for { key <- parseableString @@ -34,7 +32,7 @@ object JsValueGenerators { for { n <- choose(0, 15) fields <- Gen.containerOfN[List, (String, JsValue)](n, genField(depth - 1)) - } yield JsObject(fields) + } yield JsObject(fields: _*) def genValue(depth: Int): Gen[JsValue] = oneOf( |