On Tue, Jun 30, 2009 at 6:17 PM, Ovid<publiustemp-perl...@yahoo.com> wrote:
>
> Part of the problem with have with 'Test::More' and friends is that there's 
> not an easy way to attach diagnostics to any test.  This is largely because 
> the interface is set in stone.  So I was thinking about a rewritten test 
> interface which allows something like this (this code actually works, by the 
> way):
>
>    use Test::Fluent 'no_plan';
>
>    my ( $have, $want ) = ( 1, 1 );
>    have $have, want $want, reason 'Some test name';
>    have [ 3, 4 ], want [ 4, 5 ], reason 'cuz I said so';   # fails
>    true 3,  reason '3 had better be true';
>    false 3, reason '3 had better still better be true';    # fails
>
>
> At the end of each of those, you could append a 'diagnostics' tag which is 
> required to take a hashref:
>
>  my $armageddon = $world->destruction;
>  false $armaggedon,
>      reason "World->destruction must never return true",
>      diagnostic { world => $world };
>
> I don't know that this is a brilliant interface, but just playing around with 
> it works (the have/want pair automatically falls back to Test::Differences if 
> $have or $want is a reference).
>
> It's a lot more explicit that Test::More and friends.  That means it's a lot 
> more verbose.  However, it's something which could be played with.
>
> Is this a bad idea?  Are there other suggestions people want?  If we had a 
> dot operator, it would be clean to write this:
>
>  have($have).want($want).reason($some_reason);
>
> But we don't, so we'd have to fall back on the ugly:
>
>  have($have)->want($want)->reason($some_reason);
>
> Though I could easily overload the dot operator to get the first syntax.
>
> Or if we needed delayed evaluation for some reason:
>
>  have { $have }, want { $want }, reason $some_reason);
>
> That's also ugly and requires the (&) prototype (and friends).
>
> Thoughts?  Am I totally smoking crack here?  If there's a clean way to 
> shoehorn diagnostics on the Test::More-style interface, I guess that would be 
> ok.
>
> Cheers,
> Ovid
> --
> Buy the book         - http://www.oreilly.com/catalog/perlhks/
> Tech blog            - http://use.perl.org/~Ovid/journal/
> Twitter              - http://twitter.com/OvidPerl
> Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
>
>

At the hackathon Schwern and I had a go at implementing the ugly ->
type interface for Test::Builder2 based on an idea from a PDX.pm
meeting.

The idea being that it would work something like this,

ok($func('a'), 'some test')->diag('some cheap diagnostics');

or for delayed diagnostics,

{
        my $result = ok $func('a'), 'A test';
        if(!$result) {
                $result->diag("expensive diagnostics we'd rather not run");
        }
}

The idea being that these result objects would then be usable by
modules like Test::More and you would be able to easily attach
diagnostics in the right place.

Note that the block in the second example is a necessary evil for the
delayed diagnostics because of the way we make sure we don't produce
the output before we know what diagnostics come with the test.

I did have a go at writing this up at
http://colinnewell.wordpress.com/2009/03/31/perl-qa-hackathon-testbuilder2/.


Colin.

Reply via email to