[racket] Dynamically Bind Functions

From: Stephen Bloch (bloch at adelphi.edu)
Date: Sat Dec 22 19:50:53 EST 2012

On Dec 22, 2012, at 4:52 PM, Cristian Esquivias wrote:

> For example, I have a function that takes a filename and spits out a data structure. I wanted to create some unit tests and this mock the open-input-file function (with a open-input-string implementation).
> 
> I originally thought parameterize would allow me write the function as I normally would (i.e., not wrap functions in make-parameter), but it sounds like I can't. I sounds like the best way to test this function would be to pass in a port object and pass in a string port object for testing purposes.

Yes, I think the best way to write that function would be to open the named file, then pass the resulting port to another function that does all the real work.  This separates the OS-specific issues of finding and opening a file from the data-processing issues, and incidentally makes it easy to unit-test the latter function on arbitrary data.  Since the former function is only about the file system, meaningful unit-testing for it it would have to be about how it handles files that exist, files that don't, unreadable files, files in unreadable directories, etc. anyway.

Stephen Bloch
sbloch at adelphi.edu



Posted on the users mailing list.