Perl Diver 2.31
Main Environment Variables Perl Default Values Perl Config - Summary Perl Config - Full Installed Modules List Directory uptime Docs

Module Documentation
Details and documentation about a specific module, including version and documentation (if available). Note that while links to perldoc.com and search.cpan.org are provided, the module may be part of a larger distribution. If you reach a File Not Found page on either site, please try the parent module.

Test2::Tools::Exception

Name Test2::Tools::Exception
Version 0.000144
Located at /usr/local/share/perl5
File /usr/local/share/perl5/Test2/Tools/Exception.pm
Is Core No
Search CPAN for this module Test2::Tools::Exception
Documentation Test2::Tools::Exception
Module Details Test2::Tools::Exception


NAME

Test2::Tools::Exception - Test2 based tools for checking exceptions


DESCRIPTION

This is the Test2 implementation of code used to test exceptions. This is similar to the Test::Fatal manpage, but it intentionally does much less.


SYNOPSIS

    use Test2::Tools::Exception qw/dies lives/;
    like(
        dies { die 'xxx' },
        qr/xxx/,
        "Got exception"
    );
    ok(lives { ... }, "did not die") or note($@);


EXPORTS

All subs are exported by default.

$e = dies { ... }
This will trap any exception the codeblock throws. If no exception is thrown the sub will return undef. If an exception is thrown it will be returned. This function preserves $@, it will not be altered from its value before the sub is called.

$bool = lives { ... }
This will trap any exception thrown in the codeblock. It will return true when there is no exception, and false when there is. $@ is preserved from before the sub is called when there is no exception. When an exception is trapped $@ will have the exception so that you can look at it.

$bool = try_ok { ... }
$bool = try_ok { ... } "Test Description"
This will run the code block trapping any exception. If there is no exception a passing event will be issued. If the test fails a failing event will be issued, and the exception will be reported as diagnostics.

Note: This function does not preserve $@ on failure, it will be set to the exception the codeblock throws, this is by design so that you can obtain the exception if desired.


DIFFERENCES FROM TEST::FATAL

the Test::Fatal manpage sets $Test::Builder::Level such that failing tests inside the exception block will report to the line where exception() is called. I disagree with this, and think the actual line of the failing test is more important. Ultimately, though the Test::Fatal manpage cannot be changed, people probably already depend on that behavior.


SOURCE

The source code repository for Test2-Suite can be found at https://github.com/Test-More/Test2-Suite/.


MAINTAINERS

Chad Granum


AUTHORS

Chad Granum <exodist@cpan.org>Chad Granum


COPYRIGHT

Copyright 2018 Chad Granum <exodist@cpan.org>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://dev.perl.org/licenses/

Perl Diver brought to you by ScriptSolutions.com © 1997- 2024