Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tripping with a monadic action #374

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hedgehog/src/Hedgehog.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ module Hedgehog (
, (===)
, (/==)
, tripping
, trippingM

, eval
, evalNF
Expand Down Expand Up @@ -196,7 +197,7 @@ import Hedgehog.Internal.State (Action, Sequential(..), Parallel(..))
import Hedgehog.Internal.State (executeSequential, executeParallel)
import Hedgehog.Internal.State (Var(..), Symbolic, Concrete(..), concrete, opaque)
import Hedgehog.Internal.TH (discover, discoverPrefix)
import Hedgehog.Internal.Tripping (tripping)
import Hedgehog.Internal.Tripping (tripping, trippingM)


-- $functors
Expand Down
8 changes: 6 additions & 2 deletions hedgehog/src/Hedgehog/Internal/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,12 @@ action commands =
Command mgenInput exec callbacks <-
Gen.element_ $ filter (\c -> commandGenOK c state0) commands

-- If we shrink the input, we still want to use the same output. Otherwise
-- any actions using this output as part of their input will be dropped. But
-- the existing output is still in the context, so `contextNewVar` will
-- create a new one. To avoid that, we generate the output before the input.
output <- contextNewVar

input <-
case mgenInput state0 of
Nothing ->
Expand All @@ -559,8 +565,6 @@ action commands =
pure Nothing

else do
output <- contextNewVar

contextUpdate $
callbackUpdate callbacks state0 input (Var output)

Expand Down
24 changes: 16 additions & 8 deletions hedgehog/src/Hedgehog/Internal/Tripping.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# OPTIONS_HADDOCK not-home #-}
module Hedgehog.Internal.Tripping (
tripping
, trippingM
) where

import Hedgehog.Internal.Property (MonadTest, Diff(..), success, failWith)
Expand Down Expand Up @@ -28,18 +29,25 @@ tripping ::
-> (b -> f a)
-> m ()
tripping x encode decode =
trippingM x (pure . encode) (pure . decode)


-- | Similar to tripping, but with a monadic action.
trippingM ::
(MonadTest m, Applicative f, Show b, Show (f a), Eq (f a), HasCallStack)
=> a
-> (a -> m b)
-> (b -> m (f a))
-> m ()
trippingM x encode decode = do
let
mx =
pure x

i =
encode x

my =
decode i
in
if mx == my then
success
i <- encode x
my <- decode i
if mx == my
then success
else
case valueDiff <$> mkValue mx <*> mkValue my of
Nothing ->
Expand Down