Skip to content

Commit 41a1ceb

Browse files
committed
Merge pull request #24 from purescript/more-instances
Add Arb/CoArb instances for Unit and Ordering
2 parents 897c477 + 4ca4c03 commit 41a1ceb

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class Arbitrary t where
2828
arbitrary :: Gen t
2929
```
3030

31-
The `Arbitrary` class represents those types whose values can be
31+
The `Arbitrary` class represents those types whose values can be
3232
_randomly-generated_.
3333

3434
`arbitrary` uses the `Gen` monad to express a random generator for
35-
the type `t`. Combinators in the `Test.QuickCheck.Gen`
35+
the type `t`. Combinators in the `Test.QuickCheck.Gen`
3636
module can be used to construct random generators.
3737

3838
#### `CoArbitrary`
@@ -138,14 +138,42 @@ instance coarbString :: CoArbitrary String
138138
```
139139

140140

141+
#### `arbUnit`
142+
143+
``` purescript
144+
instance arbUnit :: Arbitrary Unit
145+
```
146+
147+
148+
#### `coarbUnit`
149+
150+
``` purescript
151+
instance coarbUnit :: CoArbitrary Unit
152+
```
153+
154+
155+
#### `arbOrdering`
156+
157+
``` purescript
158+
instance arbOrdering :: Arbitrary Ordering
159+
```
160+
161+
162+
#### `coarbOrdering`
163+
164+
``` purescript
165+
instance coarbOrdering :: CoArbitrary Ordering
166+
```
167+
168+
141169
#### `AlphaNumString`
142170

143171
``` purescript
144172
newtype AlphaNumString
145173
= AlphaNumString String
146174
```
147175

148-
A newtype for `String` whose `Arbitrary` instance generated random
176+
A newtype for `String` whose `Arbitrary` instance generated random
149177
alphanumeric strings.
150178

151179
#### `arbAlphaNumString`

src/Test/QuickCheck.purs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ import qualified Data.String.Unsafe as SU
3535

3636
import Test.QuickCheck.Gen
3737

38-
-- | The `Arbitrary` class represents those types whose values can be
38+
-- | The `Arbitrary` class represents those types whose values can be
3939
-- | _randomly-generated_.
40-
-- |
40+
-- |
4141
-- | `arbitrary` uses the `Gen` monad to express a random generator for
42-
-- | the type `t`. Combinators in the `Test.QuickCheck.Gen`
42+
-- | the type `t`. Combinators in the `Test.QuickCheck.Gen`
4343
-- | module can be used to construct random generators.
4444
class Arbitrary t where
4545
arbitrary :: Gen t
@@ -100,7 +100,26 @@ instance arbString :: Arbitrary String where
100100
instance coarbString :: CoArbitrary String where
101101
coarbitrary s = coarbitrary $ (S.charCodeAt 0 <$> S.split "" s)
102102

103-
-- | A newtype for `String` whose `Arbitrary` instance generated random
103+
instance arbUnit :: Arbitrary Unit where
104+
arbitrary = return unit
105+
106+
instance coarbUnit :: CoArbitrary Unit where
107+
coarbitrary _ = perturbGen 1
108+
109+
instance arbOrdering :: Arbitrary Ordering where
110+
arbitrary = do
111+
n <- chooseInt 1 3
112+
return $ case n of
113+
1 -> LT
114+
2 -> EQ
115+
3 -> GT
116+
117+
instance coarbOrdering :: CoArbitrary Ordering where
118+
coarbitrary LT = perturbGen 1
119+
coarbitrary EQ = perturbGen 2
120+
coarbitrary GT = perturbGen 3
121+
122+
-- | A newtype for `String` whose `Arbitrary` instance generated random
104123
-- | alphanumeric strings.
105124
newtype AlphaNumString = AlphaNumString String
106125

0 commit comments

Comments
 (0)