scalachecklib
package scalachecklib
- Alphabetic
- Public
- Protected
Value Members
- object ArbitrarySection extends Checkers with Matchers with Section
There is a special generator,
org.scalacheck.Arbitrary.arbitrary
, which generates arbitrary values of any supported type.The
arbitrary
GeneratorThere is a special generator,
org.scalacheck.Arbitrary.arbitrary
, which generates arbitrary values of any supported type.val evenInteger = Arbitrary.arbitrary[Int] suchThat (_ % 2 == 0) val squares = for { xs <- Arbitrary.arbitrary[List[Int]] } yield xs.map(x => x*x)
The
arbitrary
generator is the generator used by ScalaCheck when it generates values for property parameters. Most of the times, you have to supply the type of the value toarbitrary
, like above, since Scala often can't infer the type automatically. You can usearbitrary
for any type that has an implicitArbitrary
instance. As mentioned earlier, ScalaCheck has default support for common types, but it is also possible to define your own implicitArbitrary
instances for unsupported types. See the following implicit Arbitrary definition for booleans, that comes from the ScalaCheck implementation.implicit lazy val arbBool: Arbitrary[Boolean] = Arbitrary(oneOf(true, false))
- object GeneratorsHelper
- object GeneratorsSection extends Checkers with Matchers with Section
Generators are responsible for generating test data in ScalaCheck, and are represented by the
org.scalacheck.Gen
class.Generators are responsible for generating test data in ScalaCheck, and are represented by the
org.scalacheck.Gen
class. ou need to know how to use this class if you want ScalaCheck to generate data of types that are not supported by default, or if you want to use theforAll
method mentioned above, to state properties about a specific subset of a type. In theGen
object, there are several methods for creating new and modifying existing generators. We will show how to use some of them in this section. For a more complete reference of what is available, please see the API scaladoc.A generator can be seen simply as a function that takes some generation parameters, and (maybe) returns a generated value. That is, the type
Gen[T]
may be thought of as a function of typeGen.Params => Option[T]
. However, theGen
class contains additional methods to make it possible to map generators, use them in for-comprehensions and so on. Conceptually, though, you should think of generators simply as functions, and the combinators in theGen
object can be used to create or modify the behaviour of such generator functions. - object PropertiesSection extends Checkers with Matchers with Section
A property is the testable unit in ScalaCheck, and is represented by the
org.scalacheck.Prop
class.A property is the testable unit in ScalaCheck, and is represented by the
org.scalacheck.Prop
class. There are several ways to create properties in ScalaCheck, one of them is to use theorg.scalacheck.Prop.forAll
method like in the example below.scala> val propConcatLists = forAll { (l1: List[Int], l2: List[Int]) => l1.size + l2.size == (l1 ::: l2).size }
We can use the
check
method to test it:scala> propConcatLists.check + OK, passed 100 tests.
OK, that seemed alright. Now, we'll define another property.
scala> val propSqrt = forAll { (n: Int) => scala.math.sqrt(n*n) == n }
And check it:
scala> propSqrt.check ! Falsified after 2 passed tests. > ARG_0: -1 > ARG_0_ORIGINAL: -488187735
Not surprisingly, the property doesn't hold. The argument
-1
falsifies it. You can also see that the argument-488187735
falsifies the property. That was the first argument ScalaCheck found, and it was then simplified to-1
.The
forAll
method creates universally quantified properties directly, but it is also possible to create new properties by combining other properties, or to use any of the specialised methods in theorg.scalacheck.Prop
object. - object ScalacheckDatetimeSection extends Checkers with Matchers with Section
scalacheck-datetime is a library for helping use datetime libraries with ScalaCheck
scalacheck-datetime is a library for helping use datetime libraries with ScalaCheck
The motivation behind this library is to provide a simple, easy way to provide generated date and time instances that are useful to your own domain.
For SBT, you can add the dependency to your project’s build file:
resolvers += Resolver.sonatypeRepo("releases") "com.47deg" %% "scalacheck-toolbox-datetime" % "0.3.1" % "test"
Please, visit the homepage for more information
- object ScalacheckLibrary extends Library
ScalaCheck is a tool for testing Scala and Java programs, based on property specifications and automatic test data generation.