[Haskell-cafe] Replacing IO with pure values

Julian Arni jkarni at gmail.com
Tue Nov 25 13:55:53 UTC 2014


Besides, the aforementioned IOSpec, I would also suggest taking a look at
"Purify Code Using Free Monads" [0], which gives a nice introduction to a
general approach. If you're faced with library functions (i.e., ones you
haven't written yourself) that do IO, it can become a little onerous to
mock them all, though. In case the IO you're interested in testing is
mostly stdin/stderr/stdout related, you could also look at the 'silently'
package [1]. Finally, the 'knob' package [2] allows you to use in-memory
file handles, so it helps if your tack ends up being passing Handles to
pure functions (so that in the executable, those handles are
stdin/stderr/stdout + files etc, but in the test they're just in-memory
handles provided by 'knob'). 'System.Environment' has 'withArgs', which
provides for the case of testing command-line args.

[0] http://www.haskellforall.com/2012/07/purify-code-using-free-monads.html
[1] https://hackage.haskell.org/package/silently-1.2.4.1
[2] http://hackage.haskell.org/package/knob-0.1.1/docs/Data-Knob.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141125/398266ec/attachment.html>


More information about the Haskell-Cafe mailing list