
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? I'd like to inject data on the handle so that all the input chain is tested. How are command line programs' IO automatically tested usually? Another little question: How can I access the name of a function inside a function (for Trace purpose)? I have to use the CPP preprocessor? Thanks for hints, Corentin

Hello Dupont, If your code follows good style and has kept as much code out of IO as possible, you should be able to easily unit test the pure portions of your code. Otherwise, classic integration tests, by setting up the network jigs yourself, is standard.
Another little question: How can I access the name of a function inside a function (for Trace purpose)? I have to use the CPP preprocessor?
I think most people usually explicitly write in their function name. CPP can also work for this purpose. Edward

On Tue, Oct 26, 2010 at 5:11 PM, Dupont Corentin
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.

On Tue, Oct 26, 2010 at 11:11 AM, Dupont Corentin
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? I'd like to inject data on the handle so that all the input chain is tested.
I haven't tested it yet, but this "mock handle" will eventually work in GHC 7: http://hackage.haskell.org/trac/ghc/attachment/ticket/4144/ByteStringHandle.... At least, the bug report about how it didn't work has now been closed :-) I don't remember which operations worked and which didn't - getChar would work, but getContents would fail. The others have great advice, that you should try to isolate the logic of your code from IO for precisely this reason - so it is easily testable. Antoine

Thanks for your responses.
Of course I already test every pure parts of my program easily.
For now, user input is treated in 2 different ways:
- direct IO with hgetLine and like functions (blocking)
- the same but I directly push the strings on a TChan for another thread to
treat them (not blocking).
The second is easy to test, since all I have to do is to insert test strings
on the TChan to simulate user's input.
Maybe I can do everything this way.
But for example I had a bug of two threads inter-blocked on the handle.
These tests were not able to show it, since the test takes place not on the
handle itself but a little after.
Maybe "mock handle" can help.
Corentin
On Tue, Oct 26, 2010 at 11:32 PM, Antoine Latter
On Tue, Oct 26, 2010 at 11:11 AM, Dupont Corentin
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? I'd like to inject data on the handle so that all the input chain is tested.
I haven't tested it yet, but this "mock handle" will eventually work in GHC 7:
http://hackage.haskell.org/trac/ghc/attachment/ticket/4144/ByteStringHandle....
At least, the bug report about how it didn't work has now been closed :-)
I don't remember which operations worked and which didn't - getChar would work, but getContents would fail.
The others have great advice, that you should try to isolate the logic of your code from IO for precisely this reason - so it is easily testable.
Antoine
participants (4)
-
Antoine Latter
-
Ben Millwood
-
Dupont Corentin
-
Edward Z. Yang