EzDevInfo.com

scalacheck

Property-based testing for Scala ScalaCheck

High-Order ScalaCheck

Consider the following definition of a category:

trait Category[~>[_, _]] {
  def id[A]: A ~> A
  def compose[A, B, C](f: A ~> B)(g: B ~> C): A ~> C
}

Here's an instance for unary functions:

object Category {
  implicit def fCat = new Category[Function1] {
    def id[A] = identity
    def compose[A, B, C](f: A => B)(g: B => C) = g.compose(f)
  }
}

Now, categories are subject to some laws. Relating composition (.) and identity (id):

forall f: categoryArrow -> id . f == f . id == f

I want to test this with ScalaCheck. Let's try for functions over integers:

"Categories" should {
  import Category._

  val intG  = { (_ : Int) - 5 }

  "left identity" ! check {
    forAll { (a: Int) => fCat.compose(fCat.id[Int])(intG)(a) == intG(a) }      
  }

  "right identity" ! check {
    forAll { (a: Int) => fCat.compose(intG)(fCat.id)(a) == intG(a) }      
  }
}

But these are quantified over (i) a specific type (Int), and (ii) a specific function (intG). So here's my question: how far can I go in terms of generalizing the above tests, and how? Or, in other words, would it be possible to create a generator of arbitrary A => B functions, and provide those to ScalaCheck?


Source: (StackOverflow)

ScalaCheck - Ordered array generator

I am trying out ScalaCheck for the first time and I want to generate an ordered array of Ints.

I read the documentation and did some search but I didn't find a way to do it.

Can someone shed some light on this?

Thanks


Source: (StackOverflow)

Advertisements

Generate Strings from Grammar in ScalaCheck

In Scala, I have a grammar implemented using the Parser Combinators library. Now, what I want to do is generate random strings given a grammar from the parser combinators library.

It seems to me, that what the ScalaCheck library does it somehow the opposite of Parser Combinators in that it combines generators instead of parsers.

Is there already a way to generate strings using the Parser Combinators or ScalaCheck, or is there a straightforward way of transforming a Parser Combinator into a generator?


Source: (StackOverflow)

How can I test Java programs with ScalaCheck?

I have read in the ScalaCheck user guide that it's a tool for testing Scala and Java programs.

I wonder, is it just marketing, or testing Java-only codebase with it would be a reasonable idea? And if so, what's the best way to integrate it with the Java project?


Source: (StackOverflow)

Scalacheck json and case classes

I'm writing a service that takes a case class and serializes it to json, that I will then send to an instance running Elastic Search.

I'd like scalacheck to generate several case classes, with random missing data, like this:

val searchDescAndBrand = SearchEntry("", "Ac Adapters", "Sony", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", 0L)
val searchBrand = SearchEntry("", ", "Sony", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", 0L)
val searchPartNumberAndBrand = SearchEntry("02DUYT", "", "Sony", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", 0L)

you get the idea, either fill in the values or leave them empty (the last one is a Long type.

This is the easy part, the problem is that the generated json doesn't just omit the "filed", but omits a whole section, for example:

    """
  |{
  |  "from" : 0,
  |  "size" : 10,
  |  "query" : {
  |    "bool" : {
  |      "must" : [
  |        {"match" : {
  |          "description" : {
  |            "query" : "Ac Adapters",
  |            "type" : "phrase"
  |          }
  |        }},
  |        {"match" : {
  |          "brand" : {
  |            "query" : "Sony",
  |            "type" : "phrase"
  |          }
  |        }}
  |      ]
  |    }
  |  }
  |}
  |
""".stripMargin)

if I had a case class with the first 3 fields with data, the json would be:

    """
  |{
  |  "from" : 0,
  |  "size" : 10,
  |  "query" : {
  |    "bool" : {
  |      "must" : [
  |        {"match" : {
  |          "part_number" : {
  |            "query" : "02D875",
  |            "type" : "phrase"
  |          }
  |        }},
  |        {"match" : {
  |          "description" : {
  |            "query" : "Ac Adapters",
  |            "type" : "phrase"
  |          }
  |        }},
  |        {"match" : {
  |          "brand" : {
  |            "query" : "Sony",
  |            "type" : "phrase"
  |          }
  |        }}
  |      ]
  |    }
  |  }
  |}
  |
""".stripMargin)

So, in short, having a value means adding

{"match" : {
  |          "<specific name here, based on which value we have>" : {
  |            "query" : "<value from scalacheck>",
  |            "type" : "phrase"
  |          }
  |        }}

to the result.

How would you handle such a use case?


Source: (StackOverflow)

Can ScalaCheck/Specs warnings safely be ignored when using SBT with ScalaTest?

I have a simple FunSuite-based ScalaTest:

package pdbartlett.hello_sbt                                                                        

import org.scalatest.FunSuite                                                                       

class SanityTest extends FunSuite {                                                                 

  test("a simple test") {                                                                           
    assert(true)                                                                                    
  }                                                                                                 

  test("a very slightly more complicated test - purposely fails") {                                 
    assert(42 === (6 * 9))                                                                          
  }                                                                                                 
}

Which I'm running with the following SBT project config:

import sbt._                                                                                        

class HelloSbtProject(info: ProjectInfo) extends DefaultProject(info) {                             

  // Dummy action, just to show config working OK.                                                  
  lazy val solveQ = task { println("42"); None }                                                    

  // Managed dependencies                                                                           
  val scalatest = "org.scalatest" % "scalatest" % "1.0" % "test"                                    
}

However, when I runsbt test I get the following warnings:

...
[info] == test-compile ==
[info]   Source analysis: 0 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling test sources...
[info] Nothing to compile.
[warn] Could not load superclass 'org.scalacheck.Properties' : java.lang.ClassNotFoundException: org.scalacheck.Properties
[warn] Could not load superclass 'org.specs.Specification' : java.lang.ClassNotFoundException: org.specs.Specification
[warn] Could not load superclass 'org.specs.Specification' : java.lang.ClassNotFoundException: org.specs.Specification
[info]   Post-analysis: 3 classes.
[info] == test-compile ==
...

For the moment I'm assuming these are just "noise" (caused by the unified test interface?) and that I can safely ignore them. But it is slightly annoying to some inner OCD part of me (though not so annoying that I'm prepared to add dependencies for the other frameworks).

Is this a correct assumption, or are there subtle errors in my test/config code? If it is safe to ignore, is there any other way to suppress these errors, or do people routinely include all three frameworks so they can pick and choose the best approach for different tests?

TIA, Paul.

(ADDED: scala v2.7.7 and sbt v0.7.4)


Source: (StackOverflow)

In the specs2 framework, why does using a Scope prevent execution of a forAll quantifier?

In the code below, how can I make Specs2 execute the first test? The "print ones" test passes when it should fail. The code inside the forAll() section is not executing because of the new Scope.

The println statements are only for tracing output. Please let me know if you see any lines starting with "one".

The empty Scope is just for demonstrating the problem. This is stripped-down from code where I actually use variables in a Scope.

import org.scalacheck.Gen
import org.scalacheck.Prop._
import org.specs2.ScalaCheck
import org.specs2.mutable.Specification
import org.specs2.specification.Scope

class TestingSpec extends Specification with ScalaCheck {
  "sample test" should {
    "print ones" in new Scope { 
      println("The first test passes, but should fail")
      forAll(Gen.posNum[Int]) { (x: Int) =>
          println("one: " + x)
          (0 < x) mustNotEqual true
      } 
    } 

    "print twos" in { 
      println("The second test passes and prints twos")
      forAll(Gen.posNum[Int]) { (x: Int) =>
        println("two: " + x)
        (0 < x) mustEqual true
      } 
    } 
  }
}

Here is my output:

sbt> testOnly TestingSpec
The second test passes and prints twos
The first test passes, but should fail
two: 1
two: 2
two: 1
    ...
two: 50
two: 34
two: 41
[info] TestingSpec
[info] 
[info] sample test should
[info]   + print ones
[info]   + print twos
[info] 
[info] Total for specification TestingSpec
[info] Finished in 96 ms
[info] 2 examples, 101 expectations, 0 failure, 0 error
[info] 
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 3 s, completed Apr 28, 2015 3:14:15 PM

P.S. I updated my project dependency from version 2.4.15 to specs2 3.5. Still has this issue...


Source: (StackOverflow)

How to specify number of checks to the forAll method in property checks

The forAll method takes generator(s) and performs number of checks of them. 100 checks by generator passed by default. Number of runs multiplied and you may quickly get too large if you use multiple generators.

I'd like to sort generators based on they significance and give irrelevant less checks. So I need to specify somehow how many runs of each generator is required. I looked into API for generators and for forAll method but found no clues. Neither of them take parameters that may specify check running behavior.

scalatest provides wrapper for the scalacheck's forAll method. So I'm searching solution either for a wrapper or for a original.


Source: (StackOverflow)

Create custom Arbitrary generator for testing java code from ScalaTest ScalaCheck

Is it possible to create a custom Arbitrary Generator in a ScalaTest (which mixins Checkers for ScalaCheck property) which is testing Java code? for e.g. following are the required steps for each test within forAll

val fund = new Fund()
val fundAccount = new Account(Account.RETIREMENT)
val consumer = new Consumer("John")
.createAccount(fundAccount)
fund.addConsumer(consumer)
fundAccount.deposit(amount)

above is a prep code before asserting results etc.


Source: (StackOverflow)

Scalacheck generate Gen.alphastr with the same length

I need to generate strings with the same length. I can't realize how. Many thanks

val s = for {
  x <- Gen.alphaStr
} yield ...

Source: (StackOverflow)

Shrink macro for case class

I'm implementing Shrink instances for my case classes. It seems to me that a macro could do the job. Has someone implemented one yet?


Source: (StackOverflow)

Error with scala-2.8 and scalacheck: Prop has wrong version

scala
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import org.scalacheck.Prop.forAll                                      
error: error while loading Prop, Scala signature Prop has wrong version
 expected: 5.0
 found: 4.1 in /opt/scala-2.8/lib/scalacheck.jar(org/scalacheck/Prop.class)

It's the the actual scala-2.8, and scalacheck was updated with sbaz immediately before (sbaz update; sbaz upgrade).

Is scalacheck for scala-2.8 under development, and not suitable for 2.8, or what might be the problem?

Do I have to get the sources, and recompile scalacheck by myself?


Source: (StackOverflow)

Specs2 and scalacheck - must pass issue

I get not found: value pass error for the code snippet below.
Do I miss any import or this code is not valid for specs2 ?

import org.specs2.mutable.Specification
import org.specs2.ScalaCheck
import org.scalacheck.{Prop, Gen}

class TestSpec extends Specification with ScalaCheck {
  "Calling test spec" should {
    "always pass" in {  
      val prop = Prop.forAll((a:Int) => true)
      prop must pass
    }
  }
}

Source: (StackOverflow)

generating *interesting* strings at random in Java [closed]

I've been using ScalaCheck for automatic unit testing. Its default String generator (i.e., its default Arbitrary[String] instance) is a little too powerful, generally producing an unreadable jumble made up mainly of characters I'm not trying to support and my system can't even render.

I've set out to create some more Arbitrary[String] instances, and am trying to find out what's out there. Here are some examples of String classes that would be helpful for testing:

  • basic multilingual plane strings
  • astral strings
  • latinate strings (including extensions a/b)
  • French words
  • left-to-right language strings
  • right-to-left language strings
  • Chinese sentences
  • "web strings" (strings drawn from a character set that constitutes 99.9999% of web content)
  • use your imagination ...

Are there libraries out there that can make these, or similar strings, at random?


Source: (StackOverflow)

How to get ScalaTest correctly reporting tests results when using scalacheck with Propspec and PropertyCheck?

I'd like to test my scala program using property based testing with scalacheck. I wrote :

class MyProperties extends PropSpec with PropertyChecks {
    property("My property") {
        val myProperty: org.scalacheck.Prop = new MyProperty
        // some code I need to set myProperty
        myProperty.check
    }
}

But this seems wrong since when I run this class using ScalaTest, I get in the Console :

Run starting. Expected test count is: 1
MyProperties:

! Falsified after 51 passed tests.
> ARG_0: myGeneratedArgument
- My property
Run completed in 1 second, 623 milliseconds.
Total number of tests run: 1
Suites: completed 1, aborted 0
Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
All tests passed.

So the problem is: my property is falsified, but the test passes !?! Does someone see what's wrong with my code ?

Thanks...

EDIT: I tried to call myProperty instead of myProperty.check, but this is not much better, cause this way, generators seem to be ignored (only one test is launched instead of a hundred).


Source: (StackOverflow)