I frequently write code that generates anonymous functions on the fly. However, I often want to verify that these functions are correct without executing them. To this end, I've started writing Test::Code. Here's the start of my test suite (more or less):
BEGIN { use_ok 'Test::Code' or die } ok defined *::is_code{CODE}, '&is_code should be exported to our namespace'; is_code sub { 1 }, sub { 1 }, 'Basic subs should match'; is_code sub { 2 }, sub { 1+1 }, '... even if the exact text is not the same'; is_code sub { print((3 * 4) + shift) }, sub { print 3 * 4 + shift }, '... and parens that do not affect the meaning should work'; ok defined *::isnt_code{CODE}, '&isnt_code should be exported to our namespace'; # How many people would spot the following bug, eh? It's something # I know I've fallen victim to ... isnt_code sub { print (3 * 4) + shift }, sub { print 3 * 4 + shift }, '... and parens that do affect the meaning should fail'; isnt_code sub { print for 1 .. 4 }, sub { for (1 .. 4) { print } }, 'Subtle lexical issues should cause the match to fail (darn it)';
The last example really bugs me. I'd like for that to work, but it doesn't. Also, variables with different names will fail, even if the code is functionally identical. I'm currently using B::Deparse to handle this, but in the long run, I'd really prefer to be able to use PPI::Normal and fail back to B::Deparse.
Right now, this test module is not as useful as I would like due to caveats listed above. Suggestions welcome.
Cheers,
Ovid
New address of my CGI Course.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Test::Code
by diotalevi (Canon) on Aug 11, 2005 at 22:19 UTC | |
by Ovid (Cardinal) on Aug 11, 2005 at 22:28 UTC | |
by thor (Priest) on Aug 12, 2005 at 11:28 UTC | |
by xdg (Monsignor) on Aug 12, 2005 at 12:27 UTC | |
by diotalevi (Canon) on Aug 12, 2005 at 14:05 UTC | |
by diotalevi (Canon) on Aug 12, 2005 at 13:07 UTC | |
|
Re: Test::Code
by adrianh (Chancellor) on Aug 12, 2005 at 10:10 UTC | |
by xdg (Monsignor) on Aug 12, 2005 at 11:45 UTC | |
by Ovid (Cardinal) on Aug 12, 2005 at 17:33 UTC | |
by adrianh (Chancellor) on Aug 13, 2005 at 08:34 UTC |