X-Posted to Perlmonks (http://perlmonks.org/index.pl?node_id=483100)

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

-- 
If this message is a response to a question on a mailing list, please send
follow up questions to the list.

Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/

Reply via email to