Re: [Haskell-cafe] Replacing IO with pure values

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

It seems like it ought to be possible to manually test some IO once, record
the user input, and automate future testing by using that record. Is it
not?
On Tue, Nov 25, 2014 at 5:55 AM, Julian Arni
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
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (2)
-
Jeffrey Brown
-
Julian Arni