6 use Test::Builder::Tester tests => 20;
9 BEGIN { use_ok( 'Test::Exception' ) };
12 package Local::Error::Simple;
14 my %Exception_singleton;
18 return $Exception_singleton{$class} ||= bless {}, $class;
26 package Local::Error::Test;
27 use base qw(Local::Error::Simple);
29 package Local::Error::Overload;
30 use base qw(Local::Error::Simple);
31 use overload q{""} => sub { "overloaded" }, fallback => 1;
33 package Local::Error::NoFallback;
34 use base qw(Local::Error::Simple);
35 use overload q{""} => sub { "no fallback" };
39 my %Exception = map {m/([^:]+)$/; lc $1 => $_->instance} qw(
42 Local::Error::Overload
43 Local::Error::NoFallback
49 die $Exception{$type} if exists $Exception{$type};
50 warn "exiting: unrecognised error type $type\n";
54 sub no_exception { "this subroutine does not die" };
56 sub normal_die { die "a normal die\n" };
60 dies_ok { normal_die() };
61 test_test("dies_ok: die");
63 test_out("not ok 1 - lived. oops");
65 dies_ok { no_exception() } "lived. oops";
66 test_test("dies_ok: normal exit detected");
68 test_out("ok 1 - lived");
69 lives_ok { no_exception() } "lived";
70 test_test("lives_ok: normal exit");
74 test_diag("died: a normal die");
75 lives_ok { normal_die() };
76 test_test("lives_ok: die detected");
80 test_diag("died: Local::Error::Overload (overloaded)");
81 lives_ok { Local::Error::Overload->throw };
82 test_test("lives_ok: die detected");
84 test_out("ok 1 - expecting normal die");
85 throws_ok { normal_die() } '/normal/', 'expecting normal die';
86 test_test("throws_ok: regex match");
88 test_out("not ok 1 - should die");
90 test_diag("expecting: /abnormal/");
91 test_diag("found: a normal die");
92 throws_ok { normal_die() } '/abnormal/', 'should die';
93 test_test("throws_ok: regex bad match detected");
95 test_out("ok 1 - threw Local::Error::Simple");
96 throws_ok { Local::Error::Simple->throw } "Local::Error::Simple";
97 test_test("throws_ok: identical exception class");
99 test_out("not ok 1 - threw Local::Error::Simple");
101 test_diag("expecting: Local::Error::Simple");
102 test_diag("found: normal exit");
103 throws_ok { no_exception() } "Local::Error::Simple";
104 test_test("throws_ok: exception on normal exit");
106 test_out("ok 1 - threw Local::Error::Simple");
107 throws_ok { Local::Error::Test->throw } "Local::Error::Simple";
108 test_test("throws_ok: exception sub-class");
110 test_out("not ok 1 - threw Local::Error::Test");
112 test_diag("expecting: Local::Error::Test");
113 test_diag("found: " . Local::Error::Simple->instance);
114 throws_ok { error("simple") } "Local::Error::Test";
115 test_test("throws_ok: bad sub-class match detected");
117 test_out("not ok 1 - threw Local::Error::Test");
119 test_diag("expecting: Local::Error::Test");
120 test_diag("found: Local::Error::Overload (overloaded)");
121 throws_ok { error("overload") } "Local::Error::Test";
122 test_test("throws_ok: throws_ok found overloaded");
124 test_out("not ok 1 - threw Local::Error::Overload (overloaded)");
126 test_diag("expecting: Local::Error::Overload (overloaded)");
127 test_diag("found: $Exception{test}");
128 throws_ok { error("test") } $Exception{overload};
129 test_test("throws_ok: throws_ok found overloaded");
131 my $e = Local::Error::Test->instance("hello");
132 test_out("ok 1 - threw $e");
133 throws_ok { error("test") } $e;
134 test_test("throws_ok: class from object match");
136 test_out("ok 1 - normal exit");
137 throws_ok { no_exception() } qr/^$/, "normal exit";
138 test_test("throws_ok: normal exit matched");
141 dies_ok { error("nofallback") };
142 test_test("dies_ok: overload without fallback");
144 test_out("not ok 1");
146 test_diag("died: Local::Error::NoFallback (no fallback)");
147 lives_ok { error("nofallback") };
148 test_test("lives_ok: overload without fallback");
150 test_out("not ok 1 - threw Local::Error::Test");
152 test_diag("expecting: Local::Error::Test");
153 test_diag("found: Local::Error::NoFallback (no fallback)");
154 throws_ok { error("nofallback") } "Local::Error::Test";
155 test_test("throws_ok: throws_ok overload without fallback");
158 throws_ok { normal_die() } '/normal/', '';
159 test_test("throws_ok: can pass empty test description");