Skip to content
Browse files

Added scala.xml.NodeSeq Arbitrary instance.

  • Loading branch information...
1 parent 5832d94 commit ac24df291f2c1b3d9b7bee6785c3c70fa59b8035 Georgii Leontiev committed Jul 30, 2012
Showing with 12 additions and 0 deletions.
  1. +12 −0 src/main/scala/org/scalacheck/Arbitrary.scala
View
12 src/main/scala/org/scalacheck/Arbitrary.scala
@@ -63,6 +63,7 @@ object Arbitrary {
import util.StdRand
import scala.collection.{immutable, mutable}
import java.util.Date
+ import scala.xml.{ Node, NodeSeq, XML }
/** Creates an Arbitrary instance */
def apply[T](g: => Gen[T]): Arbitrary[T] = new Arbitrary[T] {
@@ -442,4 +443,15 @@ object Arbitrary {
t9 <- arbitrary[T9]
} yield (t1,t2,t3,t4,t5,t6,t7,t8,t9))
+ /** Arbitrary instance of scala.xml.NodeSeq */
+ implicit def arbNodeSeq: Arbitrary[NodeSeq] = {
+ val validStr = """[a-zA-Z]+""".r
+ val strFilter = (s: String) validStr.pattern.matcher(s).matches
+ val genNode = for {
+ s1 <- arbitrary[String].filter(strFilter)
+ s2 <- arbitrary[String].filter(strFilter)
+ } yield XML.loadString("<" + s1 + ">" + s2 + "</" + s1 + ">")
+ val genNodeSeq = Gen.containerOf[Array, Node](genNode)
+ Arbitrary[NodeSeq](genNodeSeq map (_ toSeq))
+ }
}

0 comments on commit ac24df2

Please sign in to comment.
Something went wrong with that request. Please try again.