blib/lib/Test2/Tools/Exception.pm

filter: Statementstmt 94.2% 49 / 52 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 94.6% 71 / 75 CC 7 SCAR 9.0
CC
7
Cov
94%
dies
4.1
lives
3.0
try_ok
2.0
CRAP 2.5
« blib/lib/Test2/Tools/Warnings.pm ↑ Summary blib/lib/Test2/Compare/String.pm »
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
BranchTrueFalseTotal
defined wantarray or carp("Useless use of dies() in void context") 164 1 165
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
BranchTrueFalseTotal
if $ok 3 162 165
23  
24 162 ▶ unless ($err) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 0 162 162
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
BranchTrueFalseTotal
defined wantarray or carp("Useless use of lives() in void context") 22 1 23
37  
38 23 my $err;
39 {
40 23 local ($@, $!, $?);
41 23 ▶ eval { $code->(); 1 } and return 1;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$code(); 1 } } and return 1 21 2 23
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
BranchTrueFalseTotal
unless $ok 1 1 2
68 2 return $ok;
69 }
70  
71 162 1;
72  
73 __END__
« blib/lib/Test2/Tools/Warnings.pm ↑ Summary blib/lib/Test2/Compare/String.pm »