Test-Exception - Re: Problems with Test-Exception's Interface

Posted on Thu Apr 21 19:06:25 2005 by adrianh in response to 364 (See the whole thread of 4)
Re: Problems with Test-Exception's Interface

Why does Test::Exception use this syntax rather than the one more familiar to Perl testers?

Because I think it reads more cleanly than having to type "sub" everywhere :-) There's a general rule in Perl that there isn't a comma after passing a block to a subroutine so I don't think that this should be completely unexpected.

Now, to make things even more annoying, Test::Exception reverts to a more familiar syntax for throws_ok -- but only partially so. In this function, a comma is required between the regex matching the thrown error message and the test description

Again, this is just another instance of the general rule in Perl that there isn't a comma following a block. Please complain to Larry :-)

We would recommend that an alternative interface be provided which more closely follows that established in Test::More. Such an interface would probably look like this:

This syntax already exists. Like all subroutines prototyped to take anonymous subroutines as the first argument you can do it explicitly by using () and sub {}. For example:

dies_ok { die "oops" } 'died'; dies_ok( sub { die "oops" }, 'died' ); lives_ok { 1+1 } 'lived'; lives_ok( sub { 1+1 }, 'lived' ); throws_ok { die "oops" } qr/oops/, 'oops thrown'; throws_ok( sub { die "oops" }, qr/oops/, 'oops thrown' ); lives_and { is( 1+1, 2 ) } 'addition works'; lives_and( sub { is( 1+1, 2 ) }, 'addition works' );

Personally I find the block passing prototyped calls much easier to follow - but you can use either syntax if you wish. I'll make this more explicit in the docs.

Direct Responses: 689 | Write a response