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.


In reply to Test::Code by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.