diff --git a/README.md b/README.md index 75c7bea..8fe8661 100755 --- a/README.md +++ b/README.md @@ -28,11 +28,11 @@ class Arbitrary t where arbitrary :: Gen t ``` -The `Arbitrary` class represents those types whose values can be +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` +the type `t`. Combinators in the `Test.QuickCheck.Gen` module can be used to construct random generators. #### `CoArbitrary` @@ -138,6 +138,34 @@ instance coarbString :: CoArbitrary String ``` +#### `arbUnit` + +``` purescript +instance arbUnit :: Arbitrary Unit +``` + + +#### `coarbUnit` + +``` purescript +instance coarbUnit :: CoArbitrary Unit +``` + + +#### `arbOrdering` + +``` purescript +instance arbOrdering :: Arbitrary Ordering +``` + + +#### `coarbOrdering` + +``` purescript +instance coarbOrdering :: CoArbitrary Ordering +``` + + #### `AlphaNumString` ``` purescript @@ -145,7 +173,7 @@ newtype AlphaNumString = AlphaNumString String ``` -A newtype for `String` whose `Arbitrary` instance generated random +A newtype for `String` whose `Arbitrary` instance generated random alphanumeric strings. #### `arbAlphaNumString` diff --git a/src/Test/QuickCheck.purs b/src/Test/QuickCheck.purs index ad83f31..ba0c5b2 100644 --- a/src/Test/QuickCheck.purs +++ b/src/Test/QuickCheck.purs @@ -35,11 +35,11 @@ import qualified Data.String.Unsafe as SU import Test.QuickCheck.Gen --- | The `Arbitrary` class represents those types whose values can be +-- | 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` +-- | the type `t`. Combinators in the `Test.QuickCheck.Gen` -- | module can be used to construct random generators. class Arbitrary t where arbitrary :: Gen t @@ -100,7 +100,26 @@ instance arbString :: Arbitrary String where instance coarbString :: CoArbitrary String where coarbitrary s = coarbitrary $ (S.charCodeAt 0 <$> S.split "" s) --- | A newtype for `String` whose `Arbitrary` instance generated random +instance arbUnit :: Arbitrary Unit where + arbitrary = return unit + +instance coarbUnit :: CoArbitrary Unit where + coarbitrary _ = perturbGen 1 + +instance arbOrdering :: Arbitrary Ordering where + arbitrary = do + n <- chooseInt 1 3 + return $ case n of + 1 -> LT + 2 -> EQ + 3 -> GT + +instance coarbOrdering :: CoArbitrary Ordering where + coarbitrary LT = perturbGen 1 + coarbitrary EQ = perturbGen 2 + coarbitrary GT = perturbGen 3 + +-- | A newtype for `String` whose `Arbitrary` instance generated random -- | alphanumeric strings. newtype AlphaNumString = AlphaNumString String