http://www.perlmonks.org?node_id=737829

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?

Replies are listed 'Best First'.
Re: Interesting testing idea ...
by Corion (Patriarch) 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.