On Tuesday 30 January 2007 19:09, A. Pagaltzis wrote:
> * Nadim Khemir <[EMAIL PROTECTED]> [2007-01-30 18:20]:
> >   # Check that a test runs without an exception
> >   lives_and { is $foo->method, 42 } 'method is 42';
> > Isn't this equivalent to is($foo->method, 42 , 'method is 42')?
> > The test framework will catch the error if any. It's just
> > weird to attempt to catch something when the expected result is
> > to pass.
>
> No. The framework won’t catch the exception and the test script
> will die at that point. You would have to catch the exception
> yourself, then check if you got one. To get the same effect as
>
>     lives_and { ok die, "foo" };
>
> you would have to write
>
>     eval { ok die, "foo"; 0 } or do { fail; diag "died: $@" };
>
> Note how layout-sensitive this code is. If you change it just a
> bit, you will run different numbers of tests depending on whether
> the exception gets thrown or not.
>
> You could also always run two separate tests:
>
>     local $@ = "";
>     ok eval { die }, "foo";
>     ok ! $@, "foo didn't die";
>
> But that’s an extra test. `lives_and` removes all the headache
> and makes sure you can write a semantically singular test as a
> single test.

I completely agree with you and I'm going to rephrase my question/example.

yes, the framework won't catch the exception and the script will fail. Now I 
looked around for some test to make my point clearer. I did find a

ok( 1, 'I can have a coke now' );

but I wanted something a bit more complex like:

is_deeply( bencode( $thawed ), $frozen, "encode $frozen" );

what says bencode won't croak? what says it will not be modified to croak in 
the future? should we write it with lives_and? I have thousands of tests 
where I am expecting the test to not croak. stuff like:

$buffer->Reset() ;
$buffer->Insert(' word1 word_2 3 word4') ;
$buffer->GetSelection()->Set(0, 3, 0, 7) ;
is_deeply([$buffer->GetFirstWord()] , ['word1', 1], 'GetFirstWord with 
selection') ;

Any of the subroutines could croak. So my point is "is there any point in 
using lives_and" at certain places when it's not used in thousands of other 
places?

Get me right, I'm not showing any stuborness here, I just would like to see 
real world examples. If someone used it and had a real good reason, I'd like 
to learn from that person.

I found a stunning 32 occurences of live_ok in my own tests.

I believe the example in Test::Exception is not as good as it could be.

   # Check that something did not die
  lives_ok { $foo->method2 } 'expecting to live';

Doesn't explain much about when to use it.

I checked why I used lives_ok and it's when I throw crap at my modules and I 
want them to just accept it.

lives_ok {$buffer->ReplaceOccurence(qr/l/, sub{die;}, 2, 0)} 'invalid 
replacement doesn\'t die' ;

Cheers, Nadim.












Reply via email to