line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test2::Tools::Exception; |
2
|
154
|
|
|
154
|
|
1080
|
use strict; |
|
154
|
|
|
|
|
333
|
|
|
154
|
|
|
|
|
4496
|
|
3
|
154
|
|
|
154
|
|
789
|
use warnings; |
|
154
|
|
|
|
|
364
|
|
|
154
|
|
|
|
|
6140
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.000153'; |
6
|
|
|
|
|
|
|
|
7
|
154
|
|
|
154
|
|
885
|
use Carp qw/carp/; |
|
154
|
|
|
|
|
340
|
|
|
154
|
|
|
|
|
6903
|
|
8
|
154
|
|
|
154
|
|
955
|
use Test2::API qw/context/; |
|
154
|
|
|
|
|
386
|
|
|
154
|
|
|
|
|
9976
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @EXPORT = qw/dies lives try_ok/; |
11
|
154
|
|
|
154
|
|
1193
|
use base 'Exporter'; |
|
154
|
|
|
|
|
358
|
|
|
154
|
|
|
|
|
62898
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub dies(&) { |
14
|
145
|
|
|
145
|
1
|
3120
|
my $code = shift; |
15
|
|
|
|
|
|
|
|
16
|
145
|
100
|
|
|
|
640
|
defined wantarray or carp "Useless use of dies() in void context"; |
17
|
|
|
|
|
|
|
|
18
|
145
|
|
|
|
|
816
|
local ($@, $!, $?); |
19
|
145
|
|
|
|
|
296
|
my $ok = eval { $code->(); 1 }; |
|
145
|
|
|
|
|
462
|
|
|
3
|
|
|
|
|
11
|
|
20
|
145
|
|
|
|
|
4429
|
my $err = $@; |
21
|
|
|
|
|
|
|
|
22
|
145
|
100
|
|
|
|
459
|
return undef if $ok; |
23
|
|
|
|
|
|
|
|
24
|
142
|
50
|
|
|
|
368
|
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
|
|
|
|
|
1528
|
return $err; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub lives(&) { |
34
|
10
|
|
|
10
|
1
|
138
|
my $code = shift; |
35
|
|
|
|
|
|
|
|
36
|
10
|
100
|
|
|
|
110
|
defined wantarray or carp "Useless use of lives() in void context"; |
37
|
|
|
|
|
|
|
|
38
|
10
|
|
|
|
|
19
|
my $err; |
39
|
|
|
|
|
|
|
{ |
40
|
10
|
|
|
|
|
14
|
local ($@, $!, $?); |
|
10
|
|
|
|
|
53
|
|
41
|
10
|
100
|
|
|
|
21
|
eval { $code->(); 1 } and return 1; |
|
10
|
|
|
|
|
27
|
|
|
8
|
|
|
|
|
109
|
|
42
|
2
|
|
|
|
|
18
|
$err = $@; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# If the eval failed we want to set $@ to the error. |
46
|
2
|
|
|
|
|
6
|
$@ = $err; |
47
|
2
|
|
|
|
|
6
|
return 0; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub try_ok(&;$) { |
51
|
2
|
|
|
2
|
1
|
331
|
my ($code, $name) = @_; |
52
|
|
|
|
|
|
|
|
53
|
2
|
|
|
|
|
5
|
my $ok = &lives($code); |
54
|
2
|
|
|
|
|
3
|
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
|
|
|
|
|
6
|
my $ctx = context(); |
60
|
2
|
|
|
|
|
134
|
chomp(my $diag = "Exception: $err"); |
61
|
2
|
|
|
|
|
9
|
$ctx->ok($ok, $name, [$diag]); |
62
|
2
|
|
|
|
|
776
|
$ctx->release; |
63
|
|
|
|
|
|
|
|
64
|
2
|
100
|
|
|
|
48
|
$@ = $err unless $ok; |
65
|
2
|
|
|
|
|
6
|
return $ok; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
__END__ |