Skip to content

Commit 9238e7b

Browse files
committed
Tests.QuickCheckUtils: i Arbitrary (NotEmpty {,L}Text): refact
1 parent a4a0282 commit 9238e7b

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

tests/Tests/QuickCheckUtils.hs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,24 @@ instance Arbitrary BigInt where
113113
newtype NotEmpty a = NotEmpty { notEmpty :: a }
114114
deriving (Eq, Ord, Show)
115115

116+
toNotEmptyBy :: Functor m => ([Char] -> a) -> m (NonEmptyList Char) -> m (NotEmpty a)
117+
toNotEmptyBy f = fmap (coerce f)
118+
119+
arbitraryNotEmptyBy :: ([Char] -> a) -> Gen (NotEmpty a)
120+
arbitraryNotEmptyBy f = toNotEmptyBy f arbitrary
121+
122+
shrinkNotEmptyBy :: ([Char] -> a) -> (a -> [Char]) -> NotEmpty a -> [NotEmpty a]
123+
shrinkNotEmptyBy g f =
124+
toNotEmptyBy g . shrink . coerce f
116125

117126
instance Arbitrary (NotEmpty T.Text) where
118-
arbitrary = fmap (NotEmpty . T.pack . getNonEmpty) arbitrary
119-
shrink = fmap (NotEmpty . T.pack . getNonEmpty)
120-
. shrink . NonEmpty . T.unpack . notEmpty
127+
arbitrary = arbitraryNotEmptyBy T.pack
128+
shrink = shrinkNotEmptyBy T.pack T.unpack
121129

122130
instance Arbitrary (NotEmpty TL.Text) where
123-
arbitrary = fmap (NotEmpty . TL.pack . getNonEmpty) arbitrary
124-
shrink = fmap (NotEmpty . TL.pack . getNonEmpty)
125-
. shrink . NonEmpty . TL.unpack . notEmpty
131+
arbitrary = arbitraryNotEmptyBy TL.pack
132+
shrink = shrinkNotEmptyBy TL.pack TL.unpack
133+
126134

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

0 commit comments

Comments
 (0)