Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Interesting testing idea ...

by dragonchild (Archbishop)
on Jan 21, 2009 at 14:20 UTC ( #737829=perlmeditation: print w/ replies, xml ) Need Help??

The idea comes from http://stuartsierra.com/2009/01/18/tests-are-code. It's a neat idea, probably best suited for functional programming where one-liners are the norm and not the exception. BUT ... I think we could do it in Perl. Something like:
use Test::With; sub foo : WithTests( 2 ) { my ($x, $y) = @_; return $x + y; } with_test { cmp_ok( foo( 4, 3 ), '==', 7 ); cmp_ok( foo( -6, 2 ), '==', -4 ); };
I'm not even sure if that syntax will work, but it looks appealing. :-)

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Comment on Interesting testing idea ...
Download Code
Re: Interesting testing idea ...
by Corion (Pope) on Jan 21, 2009 at 14:23 UTC

    This is close-yet-different to the idea of Test::Inline, which lets you write your tests in POD directly after the code. I think I prefer Test::Inline's way of allowing a more elaborate test setup, for example setting up a database etc.. The advantage of your idea is that no other tools are required to extract the tests from the POD, but this means that everybody wanting to use your module needs Test::With installed, while the inline tests can be extracted before creating the distribution file.

Re: Interesting testing idea ...
by autarch (Hermit) on Jan 21, 2009 at 21:52 UTC

    I think to do this you'd either need a source filter (gah!) or Devel::Declare.

    As for whether it's a good idea. I don't know. I'm not a big fan of subroutine attributes, and your attribute seems redundant anyway. I think I'd prefer something more like this:

    sub foo { ... } is tested(2) { ... }

    That looks fairly 6-ish.

Re: Interesting testing idea ...
by artist (Parson) on Jan 31, 2009 at 19:13 UTC
    I like the idea. It's like test with the code itself. It eliminates the need of writing 'separate' tests. It also serve purpose of the intended code.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://737829]
Approved by Corion
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-08-29 00:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls