/
ignore.t
98 lines (70 loc) · 1.7 KB
/
ignore.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use strict;
use warnings;
use Test::More;
## no critic (Modules::ProhibitMultiplePackages)
{
package Foo;
sub foo {
my $class = shift;
TestException->throw(@_) unless $class eq 'Foo';
Bar->bar(@_);
}
}
{
package Bar;
sub bar {
shift;
Baz->baz(@_);
}
}
{
package Baz;
use base 'Foo';
sub baz {
shift->foo(@_);
}
}
use strict;
use Exception::Class qw(TestException);
sub check_trace {
my ( $trace, $unwanted_pkg, $unwanted_class ) = @_;
my ($bad_frame);
while ( my $frame = $trace->next_frame ) {
if ( ( grep { $frame->package eq $_ } @$unwanted_pkg )
|| ( grep { $frame->package->isa($_) } @$unwanted_class ) ) {
$bad_frame = $frame;
last;
}
}
ok( !$bad_frame, 'Check for unwanted frames' );
diag( 'Unwanted frame found: ' . $bad_frame->as_string )
if $bad_frame;
}
## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
eval { Foo->foo() };
my $e = $@;
check_trace( $e->trace, [], [] );
eval { Foo->foo( ignore_package => ['Baz'] ) };
$e = $@;
check_trace( $e->trace, ['Baz'], [] );
eval { Foo->foo( ignore_class => ['Foo'] ) };
$e = $@;
check_trace( $e->trace, [], ['Foo'] );
eval { Foo->foo( ignore_package => [ 'Foo', 'Baz' ] ) };
$e = $@;
check_trace( $e->trace, [ 'Foo', 'Baz' ], [] );
eval { Foo->foo( skip_frames => 5 ) };
$e = $@;
check_trace( $e->trace, ['Baz'], [] );
eval {
Foo->foo(
frame_filter => sub {
my $p = shift;
return 0 if defined $p->{args}[0] && $p->{args}[0] eq 'Baz';
return 1;
}
);
};
$e = $@;
check_trace( $e->trace, ['Baz'], [] );
done_testing();