This repository has been archived by the owner on Apr 12, 2018. It is now read-only.
forked from aboisvert/liftweb
-
Notifications
You must be signed in to change notification settings - Fork 57
/
JValueGen.scala
70 lines (56 loc) · 2.27 KB
/
JValueGen.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright 2009-2010 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.liftweb {
package json {
import org.scalacheck._
import Gen._
import Arbitrary.arbitrary
trait JValueGen {
import JsonAST._
def genJValue: Gen[JValue] = frequency((5, genSimple), (1, wrap(genArray)), (1, wrap(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[JObject] = 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; id <- choose(0, 1000000)) yield JField(name+id, value)
def genJValueClass: Gen[Class[_ <: JValue]] = oneOf(
JNull.getClass.asInstanceOf[Class[JValue]], JNothing.getClass.asInstanceOf[Class[JValue]], classOf[JInt],
classOf[JDouble], classOf[JBool], classOf[JString], classOf[JField], classOf[JArray], classOf[JObject])
def listSize = choose(0, 5).sample.get
}
trait NodeGen {
import Xml.{XmlNode, XmlElem}
import scala.xml.{Node, NodeSeq, Text}
def genXml: Gen[Node] = frequency((2, wrap(genNode)), (3, genElem))
def genNode = for {
name <- genName
node <- Gen.containerOfN[List, Node](children, genXml) map { seq => new XmlNode(name, seq) }
} yield node
def genElem = for {
name <- genName
value <- arbitrary[String]
} yield new XmlElem(name, value)
def genName = frequency((2, identifier), (1, value("const")))
private def children = choose(1, 3).sample.get
}
}
}