If the function references read-line as a top-level binding, you might get away with a macro (I guess I've got macros on the brain) like:
(with-stub-bindings ((read-line (lambda () 'do-nothing)) (display (lambda (x) 'do-nothing))) 'run-test)
which expands into:
(let ((store-rl (gensym)) (store-display (gensym))) `(dynamic-wind (lambda () (set! ,store-rl read-line) (set! ,store-display display)) (lambda () 'run-test) (lambda () (set! read-line ,store-rl) (set! display ,store-display))))
This is like the parameterize macro of PLT (and maybe Gambit?). It seems a lot cleaner than requiring that functions you want to stub to be arguments. (Of course, it's dirty as hell in the sense that it's unhygienic :).
Will
On 18 Aug 2005 21:31:33 +0200, Thomas Hafner thomas@hafner.nl.eu.org wrote:
Are there any tools helping me doing TDD using Gambit-C?
I've found Neil Van Dyke's Testeez. But it doesn't seem to support stubs. I think there is the need, that a function under test may call a stub (belonging to the test environment) rather than the original function. E.g. it may be a good idea to stub read-line, otherwise the test wouldn't be automatical. I can even think about situations where it may usefull to stub functions which are part of the software to be tested, at least for a part of the tests: e.g. if the original function isn't stateless and it's difficult to get to a rare state. Then a stub representing that state is easier to deal with.
Maybe I shouln't look for tools, but on ways how to write code, that can be tested easier. Programming in a functional way seems to be better there, but what to do then with non-functional functions like read-line? (I call it ,,non-functional`` because it can return different values although the argument list keeps being the same, i.e. the emptry one.)
Just an idea: problematic functions (like read-line above) are passed explicitely as function arguments rather than being referenced directly. When testing, I'm free to pass a stub instead of the original function read-line. Is that good?
Regards Thomas _______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca http://mailman.iro.umontreal.ca/mailman/listinfo/gambit-list