Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A very basic implementation of QuickCheck in PureScript
PureScript JavaScript

Docs

latest commit 897c477575
@paf31 paf31 authored

README.md

Module Documentation

Module Test.QuickCheck

This module is a partial port of the Haskell QuickCheck library.

QuickCheck provides a way to write property-based tests.

The Arbitrary and CoArbitrary type classes allow us to create random data with which we can run our tests. This module provides instances of both classes for PureScript's core data structures, as well as functions for writing new instances.

Test suites can use the quickCheck and quickCheckPure functions to test properties.

For example:

main = quickCheck \n -> n + 1 > n

Arbitrary

class Arbitrary t where
  arbitrary :: Gen t

The Arbitrary class represents those types whose values can be randomly-generated.

arbitrary uses the Gen monad to express a random generator for the type t. Combinators in the Test.QuickCheck.Gen module can be used to construct random generators.

CoArbitrary

class CoArbitrary t where
  coarbitrary :: forall r. t -> Gen r -> Gen r

The CoArbitrary class represents types which appear on the left of an Arbitrary function arrow.

To construct an Arbitrary instance for the type a -> b, we need to use the input of type a to perturb a random generator for b. This is the role of the coarbitrary function.

CoArbitrary instances can be written using the perturbGen function.

Result

data Result
  = Success 
  | Failed String

The result of a test: success or failure (with an error message).

showResult

instance showResult :: Show Result

(<?>)

(<?>) :: Boolean -> String -> Result

This operator attaches an error message to a failed test.

For example:

test x = myProperty x <?> ("myProperty did not hold for " <> show x)

arbChar

instance arbChar :: Arbitrary S.Char

coarbChar

instance coarbChar :: CoArbitrary S.Char

arbNumber

instance arbNumber :: Arbitrary Number

coarbNumber

instance coarbNumber :: CoArbitrary Number

arbBoolean

instance arbBoolean :: Arbitrary Boolean

coarbBoolean

instance coarbBoolean :: CoArbitrary Boolean

arbString

instance arbString :: Arbitrary String

coarbString

instance coarbString :: CoArbitrary String

AlphaNumString

newtype AlphaNumString
  = AlphaNumString String

A newtype for String whose Arbitrary instance generated random alphanumeric strings.

arbAlphaNumString

instance arbAlphaNumString :: Arbitrary AlphaNumString

coarbAlphaNumString

instance coarbAlphaNumString :: CoArbitrary AlphaNumString

arbTuple

instance arbTuple :: (Arbitrary a, Arbitrary b) => Arbitrary (Tuple a b)

coarbTuple

instance coarbTuple :: (CoArbitrary a, CoArbitrary b) => CoArbitrary (Tuple a b)

arbEither

instance arbEither :: (Arbitrary a, Arbitrary b) => Arbitrary (Either a b)

coarbEither

instance coarbEither :: (CoArbitrary a, CoArbitrary b) => CoArbitrary (Either a b)

arbMaybe

instance arbMaybe :: (Arbitrary a) => Arbitrary (Maybe a)

coarbMaybe

instance coarbMaybe :: (CoArbitrary a) => CoArbitrary (Maybe a)

arbFunction

instance arbFunction :: (CoArbitrary a, Arbitrary b) => Arbitrary (a -> b)

coarbFunction

instance coarbFunction :: (Arbitrary a, CoArbitrary b) => CoArbitrary (a -> b)

arbArray

instance arbArray :: (Arbitrary a) => Arbitrary [a]

coarbArray

instance coarbArray :: (CoArbitrary a) => CoArbitrary [a]

Testable

class Testable prop where
  test :: prop -> Gen Result

The Testable class represents testable properties.

A testable property is a function of zero or more Arbitrary arguments, returning a Boolean or Result.

Testable properties can be passed to the quickCheck function.

testableResult

instance testableResult :: Testable Result

testableBoolean

instance testableBoolean :: Testable Boolean

testableFunction

instance testableFunction :: (Arbitrary t, Testable prop) => Testable (t -> prop)

quickCheckPure

quickCheckPure :: forall prop. (Testable prop) => Number -> Number -> prop -> [Result]

Test a property, returning all test results as an array.

The first argument is the random seed to be passed to the random generator. The second argument is the number of tests to run.

QC

type QC a = forall eff. Eff (err :: Exception, random :: Random, trace :: Trace | eff) a

A type synonym which represents the effects used by the quickCheck function.

quickCheck'

quickCheck' :: forall prop. (Testable prop) => Number -> prop -> QC Unit

A variant of the quickCheck function which accepts an extra parameter representing the number of tests which should be run.

quickCheck

quickCheck :: forall prop. (Testable prop) => prop -> QC Unit

Test a property.

This function generates a new random seed, runs 100 tests and prints the test results to the console.

(===)

(===) :: forall a b. (Eq a, Show a) => a -> a -> Result

Self-documenting equality assertion

(/==)

(/==) :: forall a b. (Eq a, Show a) => a -> a -> Result

Self-documenting inequality assertion

Module Test.QuickCheck.Gen

This module defines the random generator monad used by the Test.QuickCheck module, as well as helper functions for constructing random generators.

LCG

type LCG = Number

A seed for the random number generator

Size

type Size = Number

Tests are parameterized by the Size of the randomly-generated data, the meaning of which depends on the particular generator used.

GenState

type GenState = { size :: Size, newSeed :: LCG }

The state of the random generator monad

GenOut

type GenOut a = { value :: a, state :: GenState }

The output of the random generator monad

Gen

data Gen a

The random generator monad

Gen is a state monad which encodes a linear congruential generator.

repeatable

repeatable :: forall a b. (a -> Gen b) -> Gen (a -> b)

Create a random generator for a function type.

stateful

stateful :: forall a. (GenState -> Gen a) -> Gen a

Create a random generator which uses the generator state explicitly.

variant

variant :: forall a. LCG -> Gen a -> Gen a

Modify a random generator by setting a new random seed.

sized

sized :: forall a. (Size -> Gen a) -> Gen a

Create a random generator which depends on the size parameter.

resize

resize :: forall a. Size -> Gen a -> Gen a

Modify a random generator by setting a new size parameter.

choose

choose :: Number -> Number -> Gen Number

Create a random generator which samples a range of Numbers i with uniform probability.

chooseInt

chooseInt :: Number -> Number -> Gen Number

Create a random generator which chooses an integer from a range.

oneOf

oneOf :: forall a. Gen a -> [Gen a] -> Gen a

Create a random generator which selects and executes a random generator from a non-empty collection of random generators with uniform probability.

frequency

frequency :: forall a. Tuple Number (Gen a) -> [Tuple Number (Gen a)] -> Gen a

Create a random generator which selects and executes a random generator from a non-empty, weighted collection of random generators.

arrayOf

arrayOf :: forall a. Gen a -> Gen [a]

Create a random generator which generates an array of random values.

arrayOf1

arrayOf1 :: forall a. Gen a -> Gen (Tuple a [a])

Create a random generator which generates a non-empty array of random values.

vectorOf

vectorOf :: forall a. Number -> Gen a -> Gen [a]

Create a random generator which generates a vector of random values of a specified size.

elements

elements :: forall a. a -> [a] -> Gen a

Create a random generator which selects a value from a non-empty collection with uniform probability.

runGen

runGen :: forall a. Gen a -> GenState -> GenOut a

Run a random generator

evalGen

evalGen :: forall a. Gen a -> GenState -> a

Run a random generator, keeping only the randomly-generated result

showSample'

showSample' :: forall r a. (Show a) => Size -> Gen a -> Eff (trace :: Trace | r) Unit

Print a random sample to the console

showSample

showSample :: forall r a. (Show a) => Gen a -> Eff (trace :: Trace | r) Unit

Print a random sample of 10 values to the console

uniform

uniform :: Gen Number

A random generator which approximates a uniform random variable on [0, 1]

perturbGen

perturbGen :: forall a. Number -> Gen a -> Gen a

Perturb a random generator by modifying the current seed

functorGen

instance functorGen :: Functor Gen

applyGen

instance applyGen :: Apply Gen

applicativeGen

instance applicativeGen :: Applicative Gen

bindGen

instance bindGen :: Bind Gen

monadGen

instance monadGen :: Monad Gen
Something went wrong with that request. Please try again.