On Mon, Mar 7, 2022 at 2:16 PM David Cantrell <da...@cantrell.org.uk> wrote:

>
> The best tool for this is Capture::Tiny.
>>
>
> I won't disagree with David.
>

Capture and then test the caught value with an is() or like() is quite
straight forward, so I still won't disagree with David.
And this has the added advantage of being able to make several assertions
about the results of calling a single method or subr under test, compared
to below syntactic sugar.
For a new-to-testing codebase, it's transparent and simple, with minimal
novelty.

However OTOH, the testing community would likely recommend considering as
the idiomatic and declarative testing way either

*Test::Most* & *Test::Exception* 's syntactic sugar *throws_ok() *

   throws_ok {block under test} qr/regex/, q(explanation);

*Note that there is no comma after the block !*

perl -MTest::Exception=tests,1 -E 'throws_ok { 3/0 } qr/division by zero/,
q(zero caught ok);'
1..1
ok 1 - zero caught ok

or the post-modern *Test2::Tools::Exception* 's *dies*()

like(
    dies { die 'xxx' },
    qr/xxx/,
    "Got exception"
);

This also decouples the capture from the compare, similar to Capture::Tiny,
and thus allows multiple assertions after, especially if result is saved,
even tho the dies() owns the block under test as the throws_ok() above does.

 perl -MTest2::V0 -MTest2::Tools::Exception -E 'like ( dies { 3/0 },
qr/division by zero/, q(zero caught ok)); done_testing;'
# Seeded srand with seed '20220308' from local date.
ok 1 - zero caught ok
1..1


Since you're already using Test::More, Test::Most is just more of the same
and includes Test::Exception and some other useful bits, it's just even
more than More.
AFAIK Test::Most is what the cool kids use, at least for codebases that
haven't progressed to Test::Class or Test2.
Test::Most is a drop in substitute for the Test::More you're already using.

-- 
Bill Ricker
bill.n1...@gmail.com
https://www.linkedin.com/in/n1vux

_______________________________________________
Boston-pm mailing list
Boston-pm@pm.org
https://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to