registry-hedgehog 0.2.0.3 → 0.2.1.0
raw patch · 3 files changed
+40/−38 lines, 3 filesdep ~tasty-hedgehogPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: tasty-hedgehog
API changes (from Hackage documentation)
- Data.Registry.Hedgehog: setCycleChooser :: forall a ins out. (Typeable a, Contains (GenIO a) out) => Registry ins out -> IO (Registry ins out)
+ Data.Registry.Hedgehog: setCycleChooser :: forall a ins out. (Typeable a, Contains (GenIO a) out) => Registry ins out -> Registry ins out
- Data.Registry.Hedgehog: setDistinct :: forall a ins out. (Eq a, Typeable a, Contains (GenIO a) out) => Registry ins out -> IO (Registry ins out)
+ Data.Registry.Hedgehog: setDistinct :: forall a ins out. (Eq a, Typeable a, Contains (GenIO a) out) => Registry ins out -> Registry ins out
- Data.Registry.Hedgehog: setDistinctFor :: forall a b ins out. (Typeable a, Contains (GenIO a) out, Eq b, Typeable b, Contains (GenIO b) out) => Registry ins out -> IO (Registry ins out)
+ Data.Registry.Hedgehog: setDistinctFor :: forall a b ins out. (Typeable a, Contains (GenIO a) out, Eq b, Typeable b, Contains (GenIO b) out) => Registry ins out -> Registry ins out
Files
- registry-hedgehog.cabal +3/−3
- src/Data/Registry/Hedgehog.hs +30/−25
- test/Test/Data/Registry/HedgehogSpec.hs +7/−10
registry-hedgehog.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: fda61bb8a3bfeb3bcdb36cf42887c87cb1ea8b4d62ea9d7c72b825d1013ac02b+-- hash: 59c5449086a8961a437bf9bc91bda342652afd55b95b175fbf240a9f0c5537f8 name: registry-hedgehog-version: 0.2.0.3+version: 0.2.1.0 synopsis: utilities to work with Hedgehog generators and `registry` description: This library provides some functions to extract generators from a "Registry" and make stateful modifications of that Registry to precisely control the generation of data category: Control@@ -77,7 +77,7 @@ , registry-hedgehog , tasty , tasty-discover- , tasty-hedgehog+ , tasty-hedgehog >=1.0 && <2.0 , tasty-th , template-haskell >=2.13 && <3.0 , text
src/Data/Registry/Hedgehog.hs view
@@ -71,7 +71,7 @@ import Hedgehog.Internal.Property (forAllT) import Hedgehog.Range import Protolude as P-+import System.IO.Unsafe -- * CREATION / TWEAKING OF REGISTRY GENERATORS @@ -200,44 +200,49 @@ -- * CHOOSING VALUES DETERMINISTICALLY -- | Set a cycling chooser for a specific data type-setCycleChooser :: forall a ins out . (Typeable a, Contains (GenIO a) out) => Registry ins out -> IO (Registry ins out)-setCycleChooser r = do+{-# NOINLINE setCycleChooser #-}+setCycleChooser :: forall a ins out . (Typeable a, Contains (GenIO a) out) => Registry ins out -> Registry ins out+setCycleChooser r = unsafePerformIO $ do c <- cycleChooser pure $ specializeValTo @GenIO @(GenIO a) c r -- | Set a cycling chooser for a specific data type+{-# NOINLINE setCycleChooserS #-} setCycleChooserS :: forall a m ins out . (Typeable a, Contains (GenIO a) out, MonadState (Registry ins out) m, MonadIO m) => m ()-setCycleChooserS = do- r <- get- r' <- liftIO $ setCycleChooser @a r- put r'+setCycleChooserS =+ let c = unsafePerformIO cycleChooser+ in do r <- get+ let r' = specializeValTo @GenIO @(GenIO a) c r+ put r' -- * MAKING DISTINCT VALUES -- | Generate distinct values for a specific data type-setDistinct :: forall a ins out . (Eq a, Typeable a, Contains (GenIO a) out) => Registry ins out -> IO (Registry ins out)-setDistinct r = do- ref <- newIORef []- let g = makeFast @(GenIO a) r- pure $ setGenIO (distinctWith ref g) r+{-# NOINLINE setDistinct #-}+setDistinct :: forall a ins out . (Eq a, Typeable a, Contains (GenIO a) out) => Registry ins out -> Registry ins out+setDistinct = setDistinctWithRef @a (unsafePerformIO $ newIORef []) +setDistinctWithRef :: forall a ins out . (Eq a, Typeable a, Contains (GenIO a) out) => IORef [a] -> Registry ins out -> Registry ins out+setDistinctWithRef ref r = setGenIO (distinctWith ref (makeFast @(GenIO a) r)) r+ -- | Generate distinct values for a specific data type+{-# NOINLINE setDistinctS #-} setDistinctS :: forall a m ins out . (Eq a, Typeable a, Contains (GenIO a) out, MonadState (Registry ins out) m, MonadIO m) => m ()-setDistinctS = do- r <- get- r' <- liftIO $ setDistinct @a r- put r'+setDistinctS =+ let ref = unsafePerformIO $ newIORef []+ in modify (setDistinctWithRef @a ref) -- | Generate distinct values for a specific data type, when used inside another data type-setDistinctFor :: forall a b ins out . (Typeable a, Contains (GenIO a) out, Eq b, Typeable b, Contains (GenIO b) out) => Registry ins out -> IO (Registry ins out)-setDistinctFor r = do- ref <- newIORef []- let g = makeFast @(GenIO b) r- pure $ specializeGenIO @a (distinctWith ref g) r+{-# NOINLINE setDistinctFor #-}+setDistinctFor :: forall a b ins out . (Typeable a, Contains (GenIO a) out, Eq b, Typeable b, Contains (GenIO b) out) => Registry ins out -> Registry ins out+setDistinctFor = setDistinctForWithRef @a @b (unsafePerformIO $ newIORef []) +setDistinctForWithRef :: forall a b ins out . (Typeable a, Contains (GenIO a) out, Eq b, Typeable b, Contains (GenIO b) out) => IORef [b] -> Registry ins out -> Registry ins out+setDistinctForWithRef ref r = specializeGenIO @a (distinctWith ref (makeFast @(GenIO b) r)) r+ -- | Generate distinct values for a specific data type, when used inside another data type+{-# NOINLINE setDistinctForS #-} setDistinctForS :: forall a b m ins out . (Typeable a, Contains (GenIO a) out, Eq b, Typeable b, Contains (GenIO b) out, MonadState (Registry ins out) m, MonadIO m) => m ()-setDistinctForS = do- r <- get- r' <- liftIO $ setDistinctFor @a @b r- put r'+setDistinctForS =+ let ref = unsafePerformIO $ newIORef []+ in modify (setDistinctForWithRef @a @b ref)
test/Test/Data/Registry/HedgehogSpec.hs view
@@ -7,7 +7,6 @@ module Test.Data.Registry.HedgehogSpec where -import Data.List (nub) import Data.Registry import Data.Registry.Hedgehog import qualified Data.Text as T (length, take, toUpper)@@ -91,19 +90,17 @@ -- * It would be also very nice to have stateful generation where we can cycle -- across different constructors for a given data type -test_cycle_constructors = noShrink $+test_cycle_constructors = prop "we can cycle deterministically across all the constructors of a data type" $ runS generators $ do- setGenS @Int (pure 1) setCycleChooserS @EmployeeStatus-- names <- replicateM 10 (forallS @EmployeeStatus)- names === take 10 (join $ repeat [Permanent, Temporary 1])+ -- uncomment to check+ -- collect =<< forallS @EmployeeStatus+ success -- We can also make sure we generate distinct values for a given type test_distinct_values = prop "we can generate distinct values for a given data type when used in a specific context" $ runS generators $ do setDistinctForS @Department @Text-- departments <- replicateM 10 (forallS @Department)- let names = departmentName <$> departments- names === nub names+ -- uncomment to check+ -- collect =<< departmentName <$> forallS @Department+ success