Skip to content

Commit 96bbfcf

Browse files
Better convenience functions
1 parent e95e89a commit 96bbfcf

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

quickcheck-dynamic/src/Test/QuickCheck/StateModel.hs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Test.QuickCheck.StateModel (
2424
Realized,
2525
Generic,
2626
monitorPost,
27+
counterexamplePost,
2728
stateAfter,
2829
runActions,
2930
lookUpVar,
@@ -139,11 +140,18 @@ type instance Realized (ReaderT r m) a = Realized m a
139140
type instance Realized (WriterT w m) a = Realized m a
140141
type instance Realized Identity a = a
141142

142-
newtype PostconditionM m a = PostconditionM {runPost :: WriterT (Endo Property) m a}
143+
newtype PostconditionM m a = PostconditionM {runPost :: WriterT (Endo Property, Endo Property) m a}
143144
deriving (Functor, Applicative, Monad, MonadTrans)
144145

146+
-- | Apply the property transformation to the property after evaluating
147+
-- the postcondition. Useful for collecting statistics while avoiding
148+
-- duplication between `monitoring` and `postcondition`.
145149
monitorPost :: Monad m => (Property -> Property) -> PostconditionM m ()
146-
monitorPost m = PostconditionM $ tell (Endo m)
150+
monitorPost m = PostconditionM $ tell (Endo m, mempty)
151+
152+
-- | Acts as `Test.QuickCheck.counterexample` if the postcondition fails.
153+
counterexamplePost :: Monad m => String -> PostconditionM m ()
154+
counterexamplePost c = PostconditionM $ tell (mempty, Endo $ counterexample c)
147155

148156
class Monad m => RunModel state m where
149157
-- | Perform an `Action` in some `state` in the `Monad` `m`. This
@@ -389,7 +397,8 @@ runActions (Actions_ rejected (Smart _ actions)) = loop initialAnnotatedState []
389397
s' = computeNextState s act var
390398
env' = (var :== ret) : env
391399
monitor (monitoring @state @m (underlyingState s, underlyingState s') act (lookUpVar env') ret)
392-
(b, Endo mon) <- run . runWriterT . runPost $ postcondition @state @m (underlyingState s, underlyingState s') act (lookUpVar env) ret
400+
(b, (Endo mon, Endo onFail)) <- run . runWriterT . runPost $ postcondition @state @m (underlyingState s, underlyingState s') act (lookUpVar env) ret
393401
monitor mon
402+
unless b $ monitor onFail
394403
assert b
395404
loop s' env' as

0 commit comments

Comments
 (0)