[Haskell-cafe] Test command line programs

Ben Millwood haskell at benmachine.co.uk
Tue Oct 26 14:40:38 EDT 2010


On Tue, Oct 26, 2010 at 5:11 PM, Dupont Corentin
<corentin.dupont at gmail.com> wrote:
> Hello again café,
>
> I have a command line program that takes input from various handles
> (actually network sockets) like this:
>
>> s <- hGetLine h
>> etc.
>
> I'd like to unit test this. How can I do?

If all you ever do in some part of the code is read from the socket,
consider passing an IO action to do that into your function, instead
of the handle itself.
Then:
a. you can easily replace the IO String with (return "testdata"), or a
read from an MVar you feed data into, or whatever else you like.
b. you can statically guarantee the function doesn't do anything
unexpected to the handle, like closing it or seeking or setting a
buffering option.
c. you will probably not have to write as much, saving on keyboard wear.


More information about the Haskell-Cafe mailing list