Skip to content

Commit

Permalink
Merge pull request #24 from purescript/more-instances
Browse files Browse the repository at this point in the history
Add Arb/CoArb instances for Unit and Ordering
  • Loading branch information
garyb committed Apr 4, 2015
2 parents 897c477 + 4ca4c03 commit 41a1ceb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -138,14 +138,42 @@ 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
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`
Expand Down
27 changes: 23 additions & 4 deletions src/Test/QuickCheck.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 41a1ceb

Please sign in to comment.