From e0e4f580e9c1cd463ec0d289d88968c7ebc676f3 Mon Sep 17 00:00:00 2001 From: Joni Freeman Date: Wed, 12 Aug 2009 11:21:22 +0300 Subject: [PATCH] Add scalacheck tests. --- lift-json/pom.xml | 6 +++++ .../main/scala/net/liftweb/json/Json.scala | 4 ++-- .../scala/net/liftweb/json/Generators.scala | 24 +++++++++++++++++++ .../scala/net/liftweb/json/ParserTest.scala | 22 +++++++++++++++++ .../scala/net/liftweb/json/PrintingTest.scala | 24 +++++++++++++++++++ 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 lift-json/src/test/scala/net/liftweb/json/Generators.scala create mode 100644 lift-json/src/test/scala/net/liftweb/json/ParserTest.scala create mode 100644 lift-json/src/test/scala/net/liftweb/json/PrintingTest.scala diff --git a/lift-json/pom.xml b/lift-json/pom.xml index 25b8a60600..107ff2c6c8 100644 --- a/lift-json/pom.xml +++ b/lift-json/pom.xml @@ -21,6 +21,12 @@ 1.5.0 test + + org.scalacheck + scalacheck + 1.5 + test + junit junit diff --git a/lift-json/src/main/scala/net/liftweb/json/Json.scala b/lift-json/src/main/scala/net/liftweb/json/Json.scala index afde6f19a4..75ecd48ba2 100644 --- a/lift-json/src/main/scala/net/liftweb/json/Json.scala +++ b/lift-json/src/main/scala/net/liftweb/json/Json.scala @@ -195,7 +195,7 @@ object JsonDSL extends Printer { trait Printer { import scala.text._ - def compact(d: Document) = { + def compact(d: Document): String = { def layout(doc: Document): String = doc match { case DocText(s) => s case DocCons(d1, d2) => layout(d1) + layout(d2) @@ -207,7 +207,7 @@ trait Printer { layout(d) } - def pretty(d: Document) = { + def pretty(d: Document): String = { val s = new java.io.StringWriter d.format(80, s) s.toString diff --git a/lift-json/src/test/scala/net/liftweb/json/Generators.scala b/lift-json/src/test/scala/net/liftweb/json/Generators.scala new file mode 100644 index 0000000000..de5d8f748d --- /dev/null +++ b/lift-json/src/test/scala/net/liftweb/json/Generators.scala @@ -0,0 +1,24 @@ +package net.liftweb.json + +import org.scalacheck._ +import Gen._ +import Arbitrary.arbitrary +import JsonAST._ + +trait JValueGen { + def genJValue: Gen[JValue] = frequency((5, genSimple), (1, lzy(genArray)), (1, lzy(genObject))) + def genSimple: Gen[JValue] = oneOf( + value(JNull), + arbitrary[Int].map(JInt(_)), + arbitrary[Double].map(JDouble(_)), + arbitrary[Boolean].map(JBool(_)), + arbitrary[String].map(JString(_))) + + def genArray: Gen[JValue] = for (l <- genList) yield JArray(l) + def genObject: Gen[JValue] = for (l <- genFieldList) yield JObject(l) + + def genList = Gen.containerOfN[List, JValue](listSize, genJValue) + def genFieldList = Gen.containerOfN[List, JField](listSize, genField) + def genField = for (name <- identifier; value <- genJValue) yield JField(name, value) + def listSize = choose(0, 5).sample.get +} diff --git a/lift-json/src/test/scala/net/liftweb/json/ParserTest.scala b/lift-json/src/test/scala/net/liftweb/json/ParserTest.scala new file mode 100644 index 0000000000..cda5266623 --- /dev/null +++ b/lift-json/src/test/scala/net/liftweb/json/ParserTest.scala @@ -0,0 +1,22 @@ +package net.liftweb.json + +import _root_.org.scalacheck._ +import _root_.org.scalacheck.Prop.forAll +import _root_.org.specs.Specification +import _root_.org.specs.runner.{Runner, JUnit} +import _root_.org.specs.ScalaCheck + +class ParserTest extends Runner(ParserSpec) with JUnit +object ParserSpec extends Specification with JValueGen with ScalaCheck { + import JsonAST._ + import JsonParser._ + + "Any valid json" should { + "be parseable" in { + val parsing = (json: JValue) => parse(JsonDSL.pretty(render(json))).isRight + forAll(parsing) must pass + } + } + + implicit def arbJValue: Arbitrary[JValue] = Arbitrary(genObject) +} diff --git a/lift-json/src/test/scala/net/liftweb/json/PrintingTest.scala b/lift-json/src/test/scala/net/liftweb/json/PrintingTest.scala new file mode 100644 index 0000000000..4f63f7c81a --- /dev/null +++ b/lift-json/src/test/scala/net/liftweb/json/PrintingTest.scala @@ -0,0 +1,24 @@ +package net.liftweb.json + +import _root_.org.scalacheck._ +import _root_.org.scalacheck.Prop.forAll +import _root_.org.specs.Specification +import _root_.org.specs.runner.{Runner, JUnit} +import _root_.org.specs.ScalaCheck + +class PrintingTest extends Runner(PrintingSpec) with JUnit +object PrintingSpec extends Specification with JValueGen with ScalaCheck { + import JsonAST._ + import scala.text.Document + + "rendering" should { + "not change semantics" in { + val rendering = (json: Document) => parse(JsonDSL.pretty(json)) == parse(JsonDSL.compact(json)) + forAll(rendering) must pass + } + } + + private def parse(json: String) = scala.util.parsing.json.JSON.parse(json) + + implicit def arbDoc: Arbitrary[Document] = Arbitrary(genJValue.map(render(_))) +}