line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Chai::Core::Assertions::Exception; |
2
|
2
|
|
|
2
|
|
9
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
55
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
51
|
|
4
|
2
|
|
|
2
|
|
10
|
use utf8; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
17
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
62
|
use Exporter qw/import/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
113
|
|
7
|
|
|
|
|
|
|
our @EXPORT_OK = qw/assert_throw/; |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
11
|
use DDP; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
17
|
|
10
|
2
|
|
|
2
|
|
1621
|
use Try::Lite; |
|
2
|
|
|
|
|
1488
|
|
|
2
|
|
|
|
|
124
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
13
|
use Test::Chai::Util::Flag qw/flag/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
94
|
|
13
|
2
|
|
|
2
|
|
11
|
use Test::Chai::Util::Type qw/is_type/; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
99
|
|
14
|
2
|
|
|
2
|
|
11
|
use Test::Chai::Util::ObjDisplay qw/obj_display/; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
1280
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub assert_throw { |
17
|
31
|
|
|
31
|
0
|
76
|
my ($self, $pkg, $err_msg, $msg) = @_; |
18
|
|
|
|
|
|
|
|
19
|
31
|
100
|
|
|
|
92
|
flag($self, 'message', $msg) if defined $msg; |
20
|
31
|
|
|
|
|
82
|
my $obj = flag($self, 'object'); |
21
|
|
|
|
|
|
|
|
22
|
31
|
|
|
|
|
129
|
ref($self)->new($obj, $msg)->is->a('CodeRef'); |
23
|
|
|
|
|
|
|
|
24
|
31
|
|
|
|
|
141
|
my $thrown = 0; |
25
|
31
|
|
|
|
|
47
|
my $desired_error = undef; |
26
|
31
|
|
|
|
|
57
|
my $thrown_error = undef; |
27
|
|
|
|
|
|
|
|
28
|
31
|
100
|
66
|
|
|
248
|
if (@_ - 1 == 0) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
29
|
5
|
|
|
|
|
11
|
$err_msg = undef; |
30
|
5
|
|
|
|
|
8
|
$pkg = undef; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
elsif ($pkg && ref $pkg eq 'Regexp') { |
34
|
5
|
|
|
|
|
7
|
$err_msg = $pkg; |
35
|
5
|
|
|
|
|
11
|
$pkg = undef; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
elsif (ref $pkg) { |
39
|
6
|
|
|
|
|
13
|
$desired_error = $pkg; |
40
|
6
|
|
|
|
|
17
|
$pkg = undef; |
41
|
6
|
|
|
|
|
13
|
$err_msg = undef; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
31
|
100
|
100
|
|
|
143
|
if (defined $pkg && !defined $err_msg) { |
45
|
11
|
|
|
|
|
19
|
$err_msg = $pkg; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
31
|
|
|
|
|
47
|
my $err; |
49
|
|
|
|
|
|
|
try { |
50
|
31
|
|
|
31
|
|
588
|
$obj->(); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
'*' => sub { |
53
|
25
|
|
|
25
|
|
1400
|
$err = $@; |
54
|
31
|
|
|
|
|
241
|
}; |
55
|
|
|
|
|
|
|
|
56
|
31
|
100
|
|
|
|
357
|
if (defined $err) { |
57
|
|
|
|
|
|
|
# first, check desired error |
58
|
25
|
100
|
|
|
|
115
|
if ($desired_error) { |
59
|
4
|
|
|
|
|
22
|
$self->assert( |
60
|
|
|
|
|
|
|
$err eq $desired_error, |
61
|
|
|
|
|
|
|
'expected #{this} to throw #{exp} but #{act} was thrown'. |
62
|
|
|
|
|
|
|
'expected #{this} to not throw #{exp}', |
63
|
|
|
|
|
|
|
$desired_error, |
64
|
|
|
|
|
|
|
$err |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
|
67
|
4
|
|
|
|
|
4159
|
flag($self, 'object', $err); |
68
|
4
|
|
|
|
|
13
|
return $self; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# next, check constructor |
72
|
21
|
100
|
|
|
|
61
|
if ($pkg) { |
73
|
13
|
|
|
|
|
47
|
$self->assert( |
74
|
|
|
|
|
|
|
is_type($err, $pkg), |
75
|
|
|
|
|
|
|
'expected #{this} to throw #{exp} but #{act} was thrown', |
76
|
|
|
|
|
|
|
'expected #{this} to not throw #{exp} but #{act} was thrown', |
77
|
|
|
|
|
|
|
$pkg, |
78
|
|
|
|
|
|
|
$err |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
13
|
50
|
|
|
|
16794
|
if (!$err_msg) { |
82
|
0
|
|
|
|
|
0
|
flag($self, 'object', $err); |
83
|
0
|
|
|
|
|
0
|
return $self; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# next, check message |
88
|
21
|
100
|
|
|
|
120
|
my $message = !ref $err ? $err : np($err); |
89
|
|
|
|
|
|
|
|
90
|
21
|
100
|
66
|
|
|
58248
|
if (defined $message && ref $err_msg eq 'Regexp') { |
|
|
100
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
91
|
|
|
|
|
|
|
$self->assert( |
92
|
7
|
|
|
|
|
15
|
@{[ $message =~ /$err_msg/ ]} > 0, |
|
7
|
|
|
|
|
60
|
|
93
|
|
|
|
|
|
|
'expected #{this} to throw error matching #{exp} but got #{act}', |
94
|
|
|
|
|
|
|
'expected #{this} to throw error not matching #{exp}', |
95
|
|
|
|
|
|
|
$err_msg, |
96
|
|
|
|
|
|
|
$message |
97
|
|
|
|
|
|
|
); |
98
|
|
|
|
|
|
|
|
99
|
7
|
|
|
|
|
9080
|
flag($self, 'object', $err); |
100
|
7
|
|
|
|
|
43
|
return $self; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
elsif (defined $message && defined $err_msg && !ref $err_msg) { |
104
|
11
|
|
|
|
|
64
|
$self->assert( |
105
|
|
|
|
|
|
|
index($message, $err_msg) > -1, |
106
|
|
|
|
|
|
|
'expected #{this} to throw error including #{exp} but got #{act}', |
107
|
|
|
|
|
|
|
'expected #{this} to throw rrror not including #{act}', |
108
|
|
|
|
|
|
|
$err_msg, |
109
|
|
|
|
|
|
|
$message |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
|
112
|
11
|
|
|
|
|
12809
|
flag($self, 'object', $err); |
113
|
11
|
|
|
|
|
73
|
return $self; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
else { |
117
|
3
|
|
|
|
|
30
|
$thrown = 1; |
118
|
3
|
|
|
|
|
10
|
$thrown_error = $err; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
9
|
|
|
|
|
14
|
my $actually_got = ''; |
123
|
9
|
100
|
|
|
|
28
|
$actually_got = ' but #{act} was thrown' if $thrown; |
124
|
|
|
|
|
|
|
|
125
|
9
|
100
|
|
|
|
32
|
my $expected_thrown = |
|
|
100
|
|
|
|
|
|
126
|
|
|
|
|
|
|
defined $pkg ? $pkg : |
127
|
|
|
|
|
|
|
defined $desired_error ? '#{exp}' : 'an error'; |
128
|
|
|
|
|
|
|
|
129
|
9
|
|
|
|
|
57
|
$self->assert( |
130
|
|
|
|
|
|
|
$thrown, |
131
|
|
|
|
|
|
|
'expected #{this} to throw ' . $expected_thrown . $actually_got, |
132
|
|
|
|
|
|
|
'expected #{this} to not throw ' . $expected_thrown . $actually_got, |
133
|
|
|
|
|
|
|
$desired_error, |
134
|
|
|
|
|
|
|
$thrown_error |
135
|
|
|
|
|
|
|
); |
136
|
|
|
|
|
|
|
|
137
|
9
|
|
|
|
|
11339
|
flag($self, 'object', $thrown_error); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
1; |