22
33## Module Test.QuickCheck
44
5+
6+ This module is a partial port of the Haskell QuickCheck library.
7+
8+ QuickCheck provides a way to write _ property-based_ tests.
9+
10+ The ` Arbitrary ` and ` CoArbitrary ` type classes allow us to create
11+ random data with which we can run our tests. This module provides
12+ instances of both classes for PureScript's core data structures,
13+ as well as functions for writing new instances.
14+
15+ Test suites can use the ` quickCheck ` and ` quickCheckPure ` functions
16+ to test properties.
17+
18+ For example:
19+
20+ ``` purescript
21+ main = quickCheck \n -> n + 1 > n
22+ ```
23+
524#### ` Arbitrary `
625
726``` purescript
827class Arbitrary t where
928 arbitrary :: Gen t
1029```
1130
31+ The ` Arbitrary ` class represents those types whose values can be
32+ _ randomly-generated_ .
33+
34+ ` arbitrary ` uses the ` Gen ` monad to express a random generator for
35+ the type ` t ` . Combinators in the ` Test.QuickCheck.Gen `
36+ module can be used to construct random generators.
1237
1338#### ` CoArbitrary `
1439
@@ -17,6 +42,14 @@ class CoArbitrary t where
1742 coarbitrary :: forall r. t -> Gen r -> Gen r
1843```
1944
45+ The ` CoArbitrary ` class represents types which appear on the left of
46+ an ` Arbitrary ` function arrow.
47+
48+ To construct an ` Arbitrary ` instance for the type ` a -> b ` , we need to
49+ use the input of type ` a ` to _ perturb_ a random generator for ` b ` . This
50+ is the role of the ` coarbitrary ` function.
51+
52+ ` CoArbitrary ` instances can be written using the ` perturbGen ` function.
2053
2154#### ` Result `
2255
@@ -26,6 +59,7 @@ data Result
2659 | Failed String
2760```
2861
62+ The result of a test: success or failure (with an error message).
2963
3064#### ` showResult `
3165
@@ -40,6 +74,13 @@ instance showResult :: Show Result
4074(<?>) :: Boolean -> String -> Result
4175```
4276
77+ This operator attaches an error message to a failed test.
78+
79+ For example:
80+
81+ ``` purescript
82+ test x = myProperty x <?> ("myProperty did not hold for " <> show x)
83+ ```
4384
4485#### ` arbChar `
4586
@@ -104,6 +145,8 @@ newtype AlphaNumString
104145 = AlphaNumString String
105146```
106147
148+ A newtype for ` String ` whose ` Arbitrary ` instance generated random
149+ alphanumeric strings.
107150
108151#### ` arbAlphaNumString `
109152
@@ -196,6 +239,12 @@ class Testable prop where
196239 test :: prop -> Gen Result
197240```
198241
242+ The ` Testable ` class represents _ testable properties_ .
243+
244+ A testable property is a function of zero or more ` Arbitrary ` arguments,
245+ returning a ` Boolean ` or ` Result ` .
246+
247+ Testable properties can be passed to the ` quickCheck ` function.
199248
200249#### ` testableResult `
201250
@@ -224,27 +273,38 @@ instance testableFunction :: (Arbitrary t, Testable prop) => Testable (t -> prop
224273quickCheckPure :: forall prop. (Testable prop) => Number -> Number -> prop -> [Result]
225274```
226275
276+ Test a property, returning all test results as an array.
277+
278+ The first argument is the _ random seed_ to be passed to the random generator.
279+ The second argument is the number of tests to run.
227280
228281#### ` QC `
229282
230283``` purescript
231284type QC a = forall eff. Eff (err :: Exception, random :: Random, trace :: Trace | eff) a
232285```
233286
287+ A type synonym which represents the effects used by the ` quickCheck ` function.
234288
235289#### ` quickCheck' `
236290
237291``` purescript
238292quickCheck' :: forall prop. (Testable prop) => Number -> prop -> QC Unit
239293```
240294
295+ A variant of the ` quickCheck ` function which accepts an extra parameter
296+ representing the number of tests which should be run.
241297
242298#### ` quickCheck `
243299
244300``` purescript
245301quickCheck :: forall prop. (Testable prop) => prop -> QC Unit
246302```
247303
304+ Test a property.
305+
306+ This function generates a new random seed, runs 100 tests and
307+ prints the test results to the console.
248308
249309#### ` (===) `
250310
@@ -265,173 +325,208 @@ Self-documenting inequality assertion
265325
266326## Module Test.QuickCheck.Gen
267327
328+
329+ This module defines the random generator monad used by the ` Test.QuickCheck `
330+ module, as well as helper functions for constructing random generators.
331+
268332#### ` LCG `
269333
270334``` purescript
271335type LCG = Number
272336```
273337
338+ A seed for the random number generator
274339
275340#### ` Size `
276341
277342``` purescript
278343type Size = Number
279344```
280345
346+ Tests are parameterized by the ` Size ` of the randomly-generated data,
347+ the meaning of which depends on the particular generator used.
281348
282349#### ` GenState `
283350
284351``` purescript
285352type GenState = { size :: Size, newSeed :: LCG }
286353```
287354
355+ The state of the random generator monad
288356
289357#### ` GenOut `
290358
291359``` purescript
292360type GenOut a = { value :: a, state :: GenState }
293361```
294362
363+ The output of the random generator monad
295364
296365#### ` Gen `
297366
298367``` purescript
299368data Gen a
300369```
301370
371+ The random generator monad
372+
373+ ` Gen ` is a state monad which encodes a linear congruential generator.
302374
303375#### ` repeatable `
304376
305377``` purescript
306378repeatable :: forall a b. (a -> Gen b) -> Gen (a -> b)
307379```
308380
381+ Create a random generator for a function type.
309382
310383#### ` stateful `
311384
312385``` purescript
313386stateful :: forall a. (GenState -> Gen a) -> Gen a
314387```
315388
389+ Create a random generator which uses the generator state explicitly.
316390
317391#### ` variant `
318392
319393``` purescript
320394variant :: forall a. LCG -> Gen a -> Gen a
321395```
322396
397+ Modify a random generator by setting a new random seed.
323398
324399#### ` sized `
325400
326401``` purescript
327402sized :: forall a. (Size -> Gen a) -> Gen a
328403```
329404
405+ Create a random generator which depends on the size parameter.
330406
331407#### ` resize `
332408
333409``` purescript
334410resize :: forall a. Size -> Gen a -> Gen a
335411```
336412
413+ Modify a random generator by setting a new size parameter.
337414
338415#### ` choose `
339416
340417``` purescript
341418choose :: Number -> Number -> Gen Number
342419```
343420
421+ Create a random generator which samples a range of ` Number ` s i
422+ with uniform probability.
344423
345424#### ` chooseInt `
346425
347426``` purescript
348427chooseInt :: Number -> Number -> Gen Number
349428```
350429
430+ Create a random generator which chooses an integer from a range.
351431
352432#### ` oneOf `
353433
354434``` purescript
355435oneOf :: forall a. Gen a -> [Gen a] -> Gen a
356436```
357437
438+ Create a random generator which selects and executes a random generator from
439+ a non-empty collection of random generators with uniform probability.
358440
359441#### ` frequency `
360442
361443``` purescript
362444frequency :: forall a. Tuple Number (Gen a) -> [Tuple Number (Gen a)] -> Gen a
363445```
364446
447+ Create a random generator which selects and executes a random generator from
448+ a non-empty, weighted collection of random generators.
365449
366450#### ` arrayOf `
367451
368452``` purescript
369453arrayOf :: forall a. Gen a -> Gen [a]
370454```
371455
456+ Create a random generator which generates an array of random values.
372457
373458#### ` arrayOf1 `
374459
375460``` purescript
376461arrayOf1 :: forall a. Gen a -> Gen (Tuple a [a])
377462```
378463
464+ Create a random generator which generates a non-empty array of random values.
379465
380466#### ` vectorOf `
381467
382468``` purescript
383469vectorOf :: forall a. Number -> Gen a -> Gen [a]
384470```
385471
472+ Create a random generator which generates a vector of random values of a specified size.
386473
387474#### ` elements `
388475
389476``` purescript
390477elements :: forall a. a -> [a] -> Gen a
391478```
392479
480+ Create a random generator which selects a value from a non-empty collection with
481+ uniform probability.
393482
394483#### ` runGen `
395484
396485``` purescript
397486runGen :: forall a. Gen a -> GenState -> GenOut a
398487```
399488
489+ Run a random generator
400490
401491#### ` evalGen `
402492
403493``` purescript
404494evalGen :: forall a. Gen a -> GenState -> a
405495```
406496
497+ Run a random generator, keeping only the randomly-generated result
407498
408499#### ` showSample' `
409500
410501``` purescript
411502showSample' :: forall r a. (Show a) => Size -> Gen a -> Eff (trace :: Trace | r) Unit
412503```
413504
505+ Print a random sample to the console
414506
415507#### ` showSample `
416508
417509``` purescript
418510showSample :: forall r a. (Show a) => Gen a -> Eff (trace :: Trace | r) Unit
419511```
420512
513+ Print a random sample of 10 values to the console
421514
422515#### ` uniform `
423516
424517``` purescript
425518uniform :: Gen Number
426519```
427520
521+ A random generator which approximates a uniform random variable on ` [0, 1] `
428522
429523#### ` perturbGen `
430524
431525``` purescript
432526perturbGen :: forall a. Number -> Gen a -> Gen a
433527```
434528
529+ Perturb a random generator by modifying the current seed
435530
436531#### ` functorGen `
437532
0 commit comments