[Pdx-pm] Test::Builder calling convention

Michael G Schwern schwern at pobox.com
Fri Mar 13 21:45:25 PDT 2009

benh wrote:
> On Fri, Mar 13, 2009 at 12:08 PM, Hans Dieter Pearcey
> <hdp.perl.pm.pdx at weftsoar.net> wrote:
>> On Fri, Mar 13, 2009 at 11:51:04AM -0700, Michael G Schwern wrote:
>>> As always, +1 and -1s appreciated.
>> +1
>>> PS  At PDX.pm we talked about an or() method which would do this:
>>>     $builder->ok( open my $fh, $file )
>>>             ->diag( "Errno: $!" )
>>>             ->or
>>>             ->is( $!, ENOBACON )
>>>             ->name( "Insufficient bacon" );
>>> Its a short circuit method.  If the result is true, it returns a null object
>>> that does nothing.  If it's false, it chains the result through and the
>>> following is() starts a new result object.  Trouble is, I can't remember what
>>> it bought us over a regular or operator.
>>>     $builder->ok( open my $fh, $file )
>>>             ->diag( "Errno: $!" )
>>>       or
>>>     $builder->is( $!, ENOBACON )
>>>             ->name( "Insufficient bacon" );
>>> That would appear to be perfectly sufficient, understandable to all and you
>>> get real short circuiting, not calling empty methods on an empty object.
>> It seems weird to me that diag() in your second example is returning whether or
>> not the test passed.

It's returning the result object.  Let me write it out long hand.

  my $ok = $builder->ok( open my $fh, $file );
  $ok->diag( "Errno: $!" );

  return $ok;

is equivalent to:

  return $builder->ok( open my $fh, $file )
                 ->diag("Errno: $!");

Each call to the $ok object simply returns the $ok object to allow chaining.

>> The whole example is a little contrived, though.  Do you really want to run a
>> second test based on whether the first passed?  It's hard to consider this
>> syntax without a real use case.

It is, I'll admit, very uncommon.  So I might be worrying about something that
doesn't really matter.

> I would think that it should at least be an option. SKIP, as an idea,
> is already a dependant test, so would it be that bad to allow a multi
> level setup?

How do you mean by multi-level?

Just call me 'Moron Sugar'.

More information about the Pdx-pm-list mailing list