@@ -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
139140type instance Realized (WriterT w m ) a = Realized m a
140141type 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`.
145149monitorPost :: 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
148156class 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