| 1 | package Test2::Tools::Exception; | ||||||||||
| 2 | 162 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 162 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 162 | our $VERSION = '1.302226'; | |||||||||
| 6 | |||||||||||
| 7 | 162 | ▶ | use Carp qw/carp/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 162 | ▶ | use Test2::API qw/context test2_add_pending_diag test2_clear_pending_diags/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 162 | our @EXPORT = qw/dies lives try_ok/; | |||||||||
| 11 | 162 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 165 | ▶ | sub dies(&) { | ||||||||
|
Subroutine
dies: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||
| 14 | 165 | my $code = shift; | |||||||||
| 15 | |||||||||||
| 16 | 165 | ▶ | defined wantarray or carp "Useless use of dies() in void context"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | |||||||||||
| 18 | 165 | local ($@, $!, $?); | |||||||||
| 19 | 165 | my $ok = eval { $code->(); 1 }; | |||||||||
| 20 | 165 | my $err = $@; | |||||||||
| 21 | |||||||||||
| 22 | 165 | ▶ | return undef if $ok; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | |||||||||||
| 24 | 162 | ▶ | unless ($err) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 0 | my $ctx = context(); | |||||||||
| 26 | 0 | $ctx->alert("Got exception as expected, but exception is falsy (undef, '', or 0)..."); | |||||||||
| 27 | 0 | $ctx->release; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 162 | return $err; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 23 | ▶ | sub lives(&) { | ||||||||
|
Subroutine
lives: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 34 | 23 | my $code = shift; | |||||||||
| 35 | |||||||||||
| 36 | 23 | ▶ | defined wantarray or carp "Useless use of lives() in void context"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | 23 | my $err; | |||||||||
| 39 | { | ||||||||||
| 40 | 23 | local ($@, $!, $?); | |||||||||
| 41 | 23 | ▶ | eval { $code->(); 1 } and return 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 2 | $err = $@; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 2 | test2_add_pending_diag("Exception: $err"); | |||||||||
| 46 | |||||||||||
| 47 | # If the eval failed we want to set $@ to the error. | ||||||||||
| 48 | 2 | $@ = $err; | |||||||||
| 49 | 2 | return 0; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 2 | ▶ | sub try_ok(&;$) { | ||||||||
|
Subroutine
try_ok: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 53 | 2 | my ($code, $name) = @_; | |||||||||
| 54 | |||||||||||
| 55 | 2 | my $ok = &lives($code); | |||||||||
| 56 | 2 | my $err = $@; | |||||||||
| 57 | |||||||||||
| 58 | 2 | my @diag = test2_clear_pending_diags(); | |||||||||
| 59 | |||||||||||
| 60 | # Context should be obtained AFTER code is run so that events inside the | ||||||||||
| 61 | # codeblock report inside the codeblock itself. This will also preserve $@ | ||||||||||
| 62 | # as thrown inside the codeblock. | ||||||||||
| 63 | 2 | my $ctx = context(); | |||||||||
| 64 | 2 | $ctx->ok($ok, $name, \@diag); | |||||||||
| 65 | 2 | $ctx->release; | |||||||||
| 66 | |||||||||||
| 67 | 2 | ▶ | $@ = $err unless $ok; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 2 | return $ok; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 162 | 1; | |||||||||
| 72 | |||||||||||
| 73 | __END__ | ||||||||||