line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test2::Tools::Exception; |
2
|
155
|
|
|
155
|
|
1118
|
use strict; |
|
155
|
|
|
|
|
343
|
|
|
155
|
|
|
|
|
4767
|
|
3
|
155
|
|
|
155
|
|
813
|
use warnings; |
|
155
|
|
|
|
|
352
|
|
|
155
|
|
|
|
|
6609
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.000155'; |
6
|
|
|
|
|
|
|
|
7
|
155
|
|
|
155
|
|
916
|
use Carp qw/carp/; |
|
155
|
|
|
|
|
362
|
|
|
155
|
|
|
|
|
7662
|
|
8
|
155
|
|
|
155
|
|
972
|
use Test2::API qw/context/; |
|
155
|
|
|
|
|
447
|
|
|
155
|
|
|
|
|
9947
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @EXPORT = qw/dies lives try_ok/; |
11
|
155
|
|
|
155
|
|
1177
|
use base 'Exporter'; |
|
155
|
|
|
|
|
365
|
|
|
155
|
|
|
|
|
67592
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub dies(&) { |
14
|
145
|
|
|
145
|
1
|
3931
|
my $code = shift; |
15
|
|
|
|
|
|
|
|
16
|
145
|
100
|
|
|
|
716
|
defined wantarray or carp "Useless use of dies() in void context"; |
17
|
|
|
|
|
|
|
|
18
|
145
|
|
|
|
|
618
|
local ($@, $!, $?); |
19
|
145
|
|
|
|
|
481
|
my $ok = eval { $code->(); 1 }; |
|
145
|
|
|
|
|
466
|
|
|
3
|
|
|
|
|
11
|
|
20
|
145
|
|
|
|
|
4488
|
my $err = $@; |
21
|
|
|
|
|
|
|
|
22
|
145
|
100
|
|
|
|
470
|
return undef if $ok; |
23
|
|
|
|
|
|
|
|
24
|
142
|
50
|
|
|
|
422
|
unless ($err) { |
25
|
0
|
|
|
|
|
0
|
my $ctx = context(); |
26
|
0
|
|
|
|
|
0
|
$ctx->alert("Got exception as expected, but exception is falsy (undef, '', or 0)..."); |
27
|
0
|
|
|
|
|
0
|
$ctx->release; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
142
|
|
|
|
|
1813
|
return $err; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub lives(&) { |
34
|
10
|
|
|
10
|
1
|
197
|
my $code = shift; |
35
|
|
|
|
|
|
|
|
36
|
10
|
100
|
|
|
|
131
|
defined wantarray or carp "Useless use of lives() in void context"; |
37
|
|
|
|
|
|
|
|
38
|
10
|
|
|
|
|
19
|
my $err; |
39
|
|
|
|
|
|
|
{ |
40
|
10
|
|
|
|
|
21
|
local ($@, $!, $?); |
|
10
|
|
|
|
|
54
|
|
41
|
10
|
100
|
|
|
|
27
|
eval { $code->(); 1 } and return 1; |
|
10
|
|
|
|
|
32
|
|
|
8
|
|
|
|
|
136
|
|
42
|
2
|
|
|
|
|
25
|
$err = $@; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# If the eval failed we want to set $@ to the error. |
46
|
2
|
|
|
|
|
5
|
$@ = $err; |
47
|
2
|
|
|
|
|
8
|
return 0; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub try_ok(&;$) { |
51
|
2
|
|
|
2
|
1
|
405
|
my ($code, $name) = @_; |
52
|
|
|
|
|
|
|
|
53
|
2
|
|
|
|
|
7
|
my $ok = &lives($code); |
54
|
2
|
|
|
|
|
5
|
my $err = $@; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Context should be obtained AFTER code is run so that events inside the |
57
|
|
|
|
|
|
|
# codeblock report inside the codeblock itself. This will also preserve $@ |
58
|
|
|
|
|
|
|
# as thrown inside the codeblock. |
59
|
2
|
|
|
|
|
16
|
my $ctx = context(); |
60
|
2
|
|
|
|
|
181
|
chomp(my $diag = "Exception: $err"); |
61
|
2
|
|
|
|
|
9
|
$ctx->ok($ok, $name, [$diag]); |
62
|
2
|
|
|
|
|
1311
|
$ctx->release; |
63
|
|
|
|
|
|
|
|
64
|
2
|
100
|
|
|
|
70
|
$@ = $err unless $ok; |
65
|
2
|
|
|
|
|
7
|
return $ok; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
__END__ |