![]() |
![]() |
![]() |
Test Driven Design in Functional Languages I understand how to use TDD packages when coding in an OO language, but I'm a bit confused on how one does this in a functional language. I'm currently coding in TCL (project requirements) where the TCL is executed purely as a batch script.
Lou
Mike Scwern wrote test::More for Perl as well as test::Tutorial.
Matt H.
Sorry to be picky, but TCL isn't a functional language. Far from it. Functional languages are those like Haskell, Clean and OCaml and the ML family.
David B. Wildgoose
As Matt says, the principles are really no different. You just end up testing things like subroutine outputs (and side effects, where appropriate) instead of methods. Of course you can also test the entire script -- give it known inputs and check the outputs against expected outputs.
John C.
Thanks for the ideas on how to strap the tests onto a language like TCL. I think I have a pretty good handle on how I can proceed from here.
Lou
When your code is written in a functional style, it's actually easier to set up tests. The outputs for a chunk of code depend solely on the inputs, and don't depend on any calls before or after. In an object-oriented style, you have to set up the internal states for the object(s), pass in any parameters, check the output of the call and then check the internal state for the object(s). It takes more steps, and you may have to provide an unsafe "maintenance" interface to the object internals in order to test various failure conditions.
rwh
Maybe the original poster meant "procedural" when he said "functional".
Exception guy
I did.
Lou
Good point, rwh, and that's something that's bugged me for a while about a lot of OO code I work with. For whatever reason I long ago got into a habit of writing my procedural code in a relatively functional, no-side-effects kind of way, which made it very natural to integrate lots of testing into the development process. But all the state that can get carried around in an OO world seems to get nasty sometimes, because it's much more difficult to be certain you've properly handled prerequisites and tested all possible states (though things like modeling processes around well-defined state machines does seem to help).
John C.
Everyone here seems to have missed the fact that Tcl already comes with an extensive test-management package. See documentation at http://www.tcl.tk/man/tcl8.4/TclCmd/tcltest.htm and background info at http://wiki.tcl.tk/1502 and http://wiki.tcl.tk/2604 . You don't need to reinvent this wheel!
Colin Macleod
Matt H.
|