|Problems? Is your data what you think it is?|
Setting up tests for cat-type filter programsby dmorgo (Pilgrim)
|on Oct 03, 2004 at 06:18 UTC||Need Help??|
dmorgo has asked for the wisdom of the Perl Monks concerning the following question:
I have some Perl programs that are called like this (a simplification, but it captures the gist):
I'd like to set up simple test scripts, one per program, to check whether certain input data yields the expected output:
So, just to reemphasize, there would be a separate test for each program, prog1.pl, prog2.pl, and prog3.pl, as opposed to having just one test for the entire pipeline.
There are many ways to do this. I'm wondering what nice idioms people have come up with for this. Nice properties of a test harness would be that it keeps the input and expected
output data (which can be fake, usually) in a nicely plain text, easy to read and easy to edit format. That rules out this, for example:
I seems possible all the programs could be tested by the same test script, using different data for each test script. On the other hand, I don't know if that's the best solution, because it seems nice for each test to be self contained.
Another choice is to use a __DATA__ section. That's OK, but it comes at the end of the file, decreasing ease of reading and editing a tad. And it would require another label be embedded and parsed... not that that's hard, but the simpler, the better.
Still another choice is HERE docs. I'm leaning toward that, but wondering if there are better suggestions:
Separate from the setup question is how to compare the actual and expected results. Or at least I thought at first that these were separate. But there may be some interaction between the two questions. When the data is stored in a hash, each element is being treated individually, with a placeholder for an empty result in some cases. With the HERE doc, when the result is empty, it just means a shorter list. The latter feels like a better approach, and for my application there doesn't need to be a one-to-one correspondence between each input and output item. My main concern is finding a nice way to set up the data. Thoughts or suggested idioms appreciated!