Skip to content

Commit 092b953

Browse files
authored
Merge pull request #91 from matthewleon/nonempty-instances
Arbitrary instances for NonEmpty*
2 parents ba48265 + bed5928 commit 092b953

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"package.json"
2121
],
2222
"dependencies": {
23-
"purescript-arrays": "^4.0.0",
23+
"purescript-arrays": "^4.3.0",
2424
"purescript-console": "^3.0.0",
2525
"purescript-either": "^3.0.0",
2626
"purescript-enums": "^3.0.0",
@@ -30,7 +30,7 @@
3030
"purescript-nonempty": "^4.0.0",
3131
"purescript-partial": "^1.2.0",
3232
"purescript-random": "^3.0.0",
33-
"purescript-strings": "^3.0.0",
33+
"purescript-strings": "^3.5.0",
3434
"purescript-transformers": "^3.0.0",
3535
"purescript-generics-rep": "^5.0.0",
3636
"purescript-typelevel-prelude": "^2.4.0",

src/Test/QuickCheck/Arbitrary.purs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import Prelude
1515

1616
import Control.Monad.Gen.Class (chooseBool)
1717
import Control.Monad.Gen.Common as MGC
18-
18+
import Control.Monad.ST (pureST)
19+
import Data.Array.NonEmpty (NonEmptyArray)
20+
import Data.Array.NonEmpty as NEA
21+
import Data.Array.ST (pushSTArray, unsafeFreeze, unsafeThaw)
1922
import Data.Char (toCharCode, fromCharCode)
2023
import Data.Either (Either(..))
2124
import Data.Foldable (foldl)
@@ -25,19 +28,20 @@ import Data.Int (toNumber)
2528
import Data.Lazy (Lazy, defer, force)
2629
import Data.List (List)
2730
import Data.List.NonEmpty (NonEmptyList(..))
28-
import Data.Maybe (Maybe(..))
31+
import Data.Maybe (Maybe(..), fromJust)
2932
import Data.Newtype (wrap)
3033
import Data.NonEmpty (NonEmpty(..), (:|))
3134
import Data.Record (insert)
3235
import Data.String (charCodeAt, fromCharArray, split)
36+
import Data.String.NonEmpty (NonEmptyString)
37+
import Data.String.NonEmpty as NES
3338
import Data.Symbol (class IsSymbol, SProxy(..))
3439
import Data.Tuple (Tuple(..))
35-
40+
import Partial.Unsafe (unsafePartial)
41+
import Test.QuickCheck.Gen (Gen, arrayOf, chooseInt, elements, listOf, oneOf, perturbGen, repeatable, sized, uniform)
3642
import Type.Prelude (class RowToList)
3743
import Type.Row (kind RowList, class RowLacks, Nil, Cons, RLProxy(..))
3844

39-
import Test.QuickCheck.Gen (Gen, elements, listOf, chooseInt, sized, perturbGen, repeatable, arrayOf, oneOf, uniform)
40-
4145
-- | The `Arbitrary` class represents those types whose values can be
4246
-- | _randomly-generated_.
4347
-- |
@@ -83,6 +87,12 @@ instance arbString :: Arbitrary String where
8387
instance coarbString :: Coarbitrary String where
8488
coarbitrary s = coarbitrary $ (charCodeAt zero <$> split (wrap "") s)
8589

90+
instance arbNonEmptyString :: Arbitrary NonEmptyString where
91+
arbitrary = NES.cons <$> arbitrary <*> arbitrary
92+
93+
instance coarbNonEmptyString :: Coarbitrary NonEmptyString where
94+
coarbitrary = coarbitrary <<< NES.toString
95+
8696
instance arbChar :: Arbitrary Char where
8797
arbitrary = fromCharCode <$> chooseInt 0 65536
8898

@@ -109,6 +119,18 @@ instance arbArray :: Arbitrary a => Arbitrary (Array a) where
109119
instance coarbArray :: Coarbitrary a => Coarbitrary (Array a) where
110120
coarbitrary = foldl (\f x -> f <<< coarbitrary x) id
111121

122+
instance arbNonEmptyArray :: Arbitrary a => Arbitrary (NonEmptyArray a) where
123+
arbitrary = do
124+
x <- arbitrary
125+
xs <- arbitrary
126+
pure $ unsafePartial fromJust $ NEA.fromArray $ pureST do
127+
mxs <- unsafeThaw xs
128+
_ <- pushSTArray mxs x
129+
unsafeFreeze mxs
130+
131+
instance coarbNonEmptyArray :: Coarbitrary a => Coarbitrary (NonEmptyArray a) where
132+
coarbitrary = coarbitrary <<< NEA.toArray
133+
112134
instance arbFunction :: (Coarbitrary a, Arbitrary b) => Arbitrary (a -> b) where
113135
arbitrary = repeatable (\a -> coarbitrary a arbitrary)
114136

0 commit comments

Comments
 (0)