[Haskell-cafe] QuickCheck and State monad

Mihai Maruseac mihai.maruseac at gmail.com
Sat Feb 4 21:04:25 UTC 2017

I'm assuming that each state in your example has something to
differentiate it from the others. That can be expanded to some
properties to test.

Another thing is that one event can move you from one state to only a
set of states. This can also be encoded as a property.

Finally, try to see if there are some properties/invariants that hold
when handling an event in a state. Doesn't have to be for all events
and all states, but for those where you can find some it should be
possible to write some tests.

On Sat, Feb 4, 2017 at 12:20 PM, George Boulougaris
<gboulougaris at gmail.com> wrote:
> Hello,
> I have written a Haskell module that contains functions that operate on some
> state. Let's say that the code looks like this (the actual functions may
> return an actual result instead of `()`, but this is irrelevant to the
> question):
>     data StateContext = StateContext {
>       -- some records
>     }
>     handleEventA :: EventA -> State StateContext ()
>     handleEventB :: EventB -> State StateContext ()
>     handleEventC :: EventC -> State StateContext ()
> As you can imagine the behavior of each function depends on the current
> state. For example `handleEventA >> handleEventB` will not produce the same
> result as `handleEventB >> handleEventA`. So I have several HUnit tests that
> verify the behavior of each function at several states.
> But now I would like to write more tests, that exercise all functions at all
> possible states (the number of states is finite). Writing them with HUnit
> would be quite labor-itensive, so I thought that using QuickCheck might be
> helpful in that case (I have only used it before for trivial functions).
> But I cannot see which properties should I test, or what kind of data should
> the test generate. I suspect that the test should generate random sequence
> of events (e.g. `handleEventB >> handleEventC >> handleEventA` etc), but I
> cannot see what properties should be satisfied.
> Thanks
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

Mihai Maruseac (MM)
"If you can't solve a problem, then there's an easier problem you can
solve: find it." -- George Polya

More information about the Haskell-Cafe mailing list