-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose generate via Graphula.Arbitrary
Anything in tests that utilizes random generation can use the graphula seed and remain deterministic, instead of falling back to `IO`.
- Loading branch information
Showing
3 changed files
with
48 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{-| | ||
Graphula tracks its own 'QCGen' for deterministic generation with 'Arbitrary' | ||
and 'Gen'. 'generate' can be used to produce arbitrary values utilizing | ||
graphula's generation. | ||
-} | ||
module Graphula.Arbitrary | ||
( generate | ||
) | ||
where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.IO.Unlift (MonadIO, liftIO) | ||
import Data.IORef (readIORef, writeIORef) | ||
import Graphula.Internal (MonadGraphulaBackend, askGen) | ||
import System.Random (split) | ||
import Test.QuickCheck (Gen) | ||
import Test.QuickCheck.Gen (unGen) | ||
|
||
-- | Run a generator | ||
-- | ||
-- This is akin to 'Test.QuickCheck.generate', but utilizing graphula's | ||
-- generation. The size passed to the generator is always 30; if you want | ||
-- another size then you should explicitly use 'Test.QuickCheck.resize'. | ||
-- | ||
generate :: (MonadIO m, MonadGraphulaBackend m) => Gen a -> m a | ||
generate gen = do | ||
genRef <- askGen | ||
g <- liftIO $ readIORef genRef | ||
let | ||
(g1, g2) = split g | ||
x = unGen gen g1 30 | ||
liftIO $ writeIORef genRef g2 | ||
pure x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters