Skip to content

Commit

Permalink
Tests.QuickCheckUtils: i Arbitrary (NotEmpty {,L}Text): refact
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Latukha committed Oct 4, 2021
1 parent 2b6e60a commit 44af64c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions tests/Tests/QuickCheckUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,24 @@ instance Arbitrary BigInt where
newtype NotEmpty a = NotEmpty { notEmpty :: a }
deriving (Eq, Ord, Show)

toNotEmptyBy :: Functor m => ([Char] -> a) -> m (NonEmptyList Char) -> m (NotEmpty a)
toNotEmptyBy f = fmap (coerce f)

arbitraryNotEmptyBy :: ([Char] -> a) -> Gen (NotEmpty a)
arbitraryNotEmptyBy f = toNotEmptyBy f arbitrary

shrinkNotEmptyBy :: ([Char] -> a) -> (a -> [Char]) -> NotEmpty a -> [NotEmpty a]
shrinkNotEmptyBy g f =
toNotEmptyBy g . shrink . coerce f

instance Arbitrary (NotEmpty T.Text) where
arbitrary = fmap (NotEmpty . T.pack . getNonEmpty) arbitrary
shrink = fmap (NotEmpty . T.pack . getNonEmpty)
. shrink . NonEmpty . T.unpack . notEmpty
arbitrary = arbitraryNotEmptyBy T.pack
shrink = shrinkNotEmptyBy T.pack T.unpack

instance Arbitrary (NotEmpty TL.Text) where
arbitrary = fmap (NotEmpty . TL.pack . getNonEmpty) arbitrary
shrink = fmap (NotEmpty . TL.pack . getNonEmpty)
. shrink . NonEmpty . TL.unpack . notEmpty
arbitrary = arbitraryNotEmptyBy TL.pack
shrink = shrinkNotEmptyBy TL.pack TL.unpack


data DecodeErr = Lenient | Ignore | Strict | Replace
deriving (Show, Eq, Bounded, Enum)
Expand Down

0 comments on commit 44af64c

Please sign in to comment.