forked from aboisvert/liftweb
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joni Freeman
committed
Aug 12, 2009
1 parent
eca4bf9
commit e0e4f58
Showing
5 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
lift-json/src/test/scala/net/liftweb/json/Generators.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
lift-json/src/test/scala/net/liftweb/json/ParserTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
lift-json/src/test/scala/net/liftweb/json/PrintingTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(_))) | ||
} |