Skip to content

Commit

Permalink
Add scalacheck tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joni Freeman committed Aug 12, 2009
1 parent eca4bf9 commit e0e4f58
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lift-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
<version>1.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalacheck</groupId>
<artifactId>scalacheck</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions lift-json/src/main/scala/net/liftweb/json/Json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions lift-json/src/test/scala/net/liftweb/json/Generators.scala
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions lift-json/src/test/scala/net/liftweb/json/ParserTest.scala
Original file line number Diff line number Diff line change
@@ -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)
}
24 changes: 24 additions & 0 deletions lift-json/src/test/scala/net/liftweb/json/PrintingTest.scala
Original file line number Diff line number Diff line change
@@ -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(_)))
}

0 comments on commit e0e4f58

Please sign in to comment.