@@ -15,7 +15,10 @@ import Prelude
1515
1616import Control.Monad.Gen.Class (chooseBool )
1717import 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 )
1922import Data.Char (toCharCode , fromCharCode )
2023import Data.Either (Either (..))
2124import Data.Foldable (foldl )
@@ -25,19 +28,20 @@ import Data.Int (toNumber)
2528import Data.Lazy (Lazy , defer , force )
2629import Data.List (List )
2730import Data.List.NonEmpty (NonEmptyList (..))
28- import Data.Maybe (Maybe (..))
31+ import Data.Maybe (Maybe (..), fromJust )
2932import Data.Newtype (wrap )
3033import Data.NonEmpty (NonEmpty (..), (:|))
3134import Data.Record (insert )
3235import Data.String (charCodeAt , fromCharArray , split )
36+ import Data.String.NonEmpty (NonEmptyString )
37+ import Data.String.NonEmpty as NES
3338import Data.Symbol (class IsSymbol , SProxy (..))
3439import Data.Tuple (Tuple (..))
35-
40+ import Partial.Unsafe (unsafePartial )
41+ import Test.QuickCheck.Gen (Gen , arrayOf , chooseInt , elements , listOf , oneOf , perturbGen , repeatable , sized , uniform )
3642import Type.Prelude (class RowToList )
3743import 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
8387instance 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+
8696instance arbChar :: Arbitrary Char where
8797 arbitrary = fromCharCode <$> chooseInt 0 65536
8898
@@ -109,6 +119,18 @@ instance arbArray :: Arbitrary a => Arbitrary (Array a) where
109119instance 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+
112134instance arbFunction :: (Coarbitrary a , Arbitrary b ) => Arbitrary (a -> b ) where
113135 arbitrary = repeatable (\a -> coarbitrary a arbitrary)
114136
0 commit comments