On 8/11/05, Ovid <[EMAIL PROTECTED]> wrote:
> 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.

One idea might be to try using Data::Dump::Streamer for your tests. It
will serialize the lexical context that the subroutine was compiled
with (including bound anonymous subroutines and their lexical
context).

A small hack (that i would be happy to work with you to put into the
next release) might allow you to rename the vars the in the sub as you
wanted. IE, it could normalize all vars that are lexical bound to the
subroutine so that comparisons would be easier. Of course by default
DDS wouldnt consider the subs the same unless the values of said vars
were the same too...

Yves
/me goes off to read about PPI.








-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

Reply via email to