scalacheck

Why does a Scalacheck Prop value not get evaluated?

烂漫一生 提交于 2019-12-12 03:49:35
问题 The official scalacheck documentation gives the following example: property("stringLength") = Prop.forAll { s: String => val len = s.length (s+s).length == len+len } I read that this can also be written as: val stringLength = Prop.forAll { s: String => val len = s.length (s+s).length == len+len } How can I run the second form of test code? When I execute sbt test , nothing happens with the second version. 回答1: The problem is that the second version is simply declaring a val that holds a

Request was not handled with spray-testkit

感情迁移 提交于 2019-12-11 12:32:54
问题 My service route: get( path("add" / IntNumber / IntNumber)( (a, b) => complete((a + b).toString()) ) ) ~ post( path("add") ( formFields('a.as[Int], 'b.as[Int]) { (a, b) => complete((a + b).toString()) }) ) my spec: import spray.http.FormData class RouteDefinitionSpec extends org.specs2.mutable.Specification with org.specs2.ScalaCheck with spray.testkit.Specs2RouteTest with RouteDefinition { def actorRefFactory = system "the route" should { "add with get requests" in { prop { (a: Int, b: Int)

Create an Arbitrary instance for a case class that holds a `Numeric` in ScalaCheck?

∥☆過路亽.° 提交于 2019-12-11 07:36:57
问题 I'm specifically trying to define Semigroup and a Sum type which 'is a' Semigroup and check the Associative property of Semigroup generically using ScalaCheck. I first wrote this out in Haskell because I find it easier to think of these things first in Haskell syntax and then translate them to Scala. So in Haskell, I wrote the following which works in GHCi: newtype Sum a = Sum a deriving (Show, Eq) instance Num a => Num (Sum a) where (+) (Sum x) (Sum y) = Sum (x + y) class Semigroup a where (

generating permutations with scalacheck

浪尽此生 提交于 2019-12-10 17:12:46
问题 I have some generators like this: val fooRepr = oneOf(a, b, c, d, e) val foo = for (s <- choose(1, 5); c <- listOfN(s, fooRepr)) yield c.mkString("$") This leads to duplicates ... I might get two a's, etc. What I really want is to generate random permutation with exactly 0 or 1 or each of a, b, c, d, or e (with at least one of something), in any order. I was thinking there must be an easy way, but I'm struggling to even find a hard way. :) Edited: Ok, this seems to work: val foo = for (s <-

Generating arbitrary (legal) Unicode character with scalacheck?

心不动则不痛 提交于 2019-12-10 17:09:38
问题 I'm trying to create a generator that produces (non-zero-length) legal unicode strings, with scalacheck 1.6.6 and specs 1.7 (scala 2.8.1). I hoped I could just create generators like: object Generators { def unicodeChar: Gen[Char] = choose(Math.MIN_CHAR, Math.MAX_CHAR).map(_.toChar).filter( c => Character.isDefined(c)) def unicodeStr: Gen[String] = for(cs <- listOf1(unicodeChar)) yield cs.mkString } ...then use them from specs like: import org.specs.Specification import org.specs.matcher

ScalaCheck generate BST

孤街浪徒 提交于 2019-12-10 15:48:46
问题 I'm trying to create a Gen for BST with ScalaCheck, but when I call .sample method it gives me java.lang.NullPointerException. Where do I wrong? sealed trait Tree case class Node(left: Tree, right: Tree, v: Int) extends Tree case object Leaf extends Tree import org.scalacheck._ import Gen._ import Arbitrary.arbitrary case class GenerateBST() { def genValue(left: Tree, right: Tree): Gen[Int] = (left, right) match { case (Node(_, _, min), Node(_, _, max)) => arbitrary[Int].suchThat(x => x > min

Scalacheck generate Gen.alphastr with the same length

亡梦爱人 提交于 2019-12-10 12:53:29
问题 I need to generate strings with the same length. I can't realize how. Many thanks val s = for { x <- Gen.alphaStr } yield ... 回答1: example code: import org.scalacheck.Gen import org.scalacheck.Prop.forAll // strGen generates a fixed length random string val strGen = (n: Int) => Gen.listOfN(n, Gen.alphaChar).map(_.mkString) val fixedLengthStr = forAll(strGen(10)){ s => s.length == 10 } fixedLengthStr.check to inspect a generated string use: strGen(5).sample 回答2: There is a suchThat(f: T =>

Scalacheck won't properly report the failing case

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 02:23:09
问题 I've wrote the following spec "An IP4 address" should "belong to just one class" in { val addrs = for { a <- Gen.choose(0, 255) b <- Gen.choose(0, 255) c <- Gen.choose(0, 255) d <- Gen.choose(0, 255) } yield s"$a.$b.$c.$d" forAll (addrs) { ip4s => var c: Int = 0 if (IP4_ClassA.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassB.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassC.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassD.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassE.unapply(ip4s)

How can I reduce the number of test cases ScalaCheck generates?

喜你入骨 提交于 2019-12-07 10:03:21
问题 I'm trying to solve two ScalaCheck (+ specs2) problems: Is there any way to change the number of cases that ScalaCheck generates? How can I generate strings that contain some Unicode characters? For example, I'd like to generate about 10 random strings that include both alphanumeric and Unicode characters. This code, however, always generates 100 random strings, and they are strictly alpha character based: "make a random string" in { def stringGenerator = Gen.alphaStr.suchThat(_.length < 40)

How can I reduce the number of test cases ScalaCheck generates?

╄→гoц情女王★ 提交于 2019-12-05 19:14:50
I'm trying to solve two ScalaCheck (+ specs2) problems: Is there any way to change the number of cases that ScalaCheck generates? How can I generate strings that contain some Unicode characters? For example, I'd like to generate about 10 random strings that include both alphanumeric and Unicode characters. This code, however, always generates 100 random strings, and they are strictly alpha character based: "make a random string" in { def stringGenerator = Gen.alphaStr.suchThat(_.length < 40) implicit def randomString: Arbitrary[String] = Arbitrary(stringGenerator) "the string" ! prop { (s: