Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libtest-exception-perl / libtest-exception-perl-0.27 / t / Exception.t
1 #! /usr/bin/perl -Tw
2
3 use strict;
4 use warnings;
5
6 use Test::Builder::Tester tests => 20;
7 use Test::More;
8
9 BEGIN { use_ok( 'Test::Exception' ) };
10
11 {
12         package Local::Error::Simple;
13
14         my %Exception_singleton;
15         
16         sub instance { 
17                 my $class = shift;
18                 return $Exception_singleton{$class} ||= bless {}, $class;
19         };
20
21         sub throw {
22                 my $class = shift;
23                 die $class->instance;
24         };
25
26         package Local::Error::Test;
27         use base qw(Local::Error::Simple);
28
29         package Local::Error::Overload;
30         use base qw(Local::Error::Simple);
31         use overload q{""} => sub { "overloaded" }, fallback => 1;
32
33         package Local::Error::NoFallback;
34         use base qw(Local::Error::Simple);
35         use overload q{""} => sub { "no fallback" };
36 };
37
38
39 my %Exception = map {m/([^:]+)$/; lc $1 => $_->instance} qw(
40         Local::Error::Simple 
41         Local::Error::Test 
42         Local::Error::Overload 
43         Local::Error::NoFallback
44 );
45
46
47 sub error {
48         my $type = shift;
49         die $Exception{$type} if exists $Exception{$type};
50         warn "exiting: unrecognised error type $type\n";
51         exit(1);
52 };
53
54 sub no_exception { "this subroutine does not die" };
55
56 sub normal_die { die "a normal die\n" };
57
58
59 test_out("ok 1");
60 dies_ok { normal_die() };
61 test_test("dies_ok: die");
62
63 test_out("not ok 1 - lived. oops");
64 test_fail(+1);
65 dies_ok { no_exception() } "lived. oops";
66 test_test("dies_ok: normal exit detected");
67
68 test_out("ok 1 - lived");
69 lives_ok { no_exception() } "lived";
70 test_test("lives_ok: normal exit");
71
72 test_out("not ok 1");
73 test_fail(+2);
74 test_diag("died: a normal die");
75 lives_ok { normal_die() };
76 test_test("lives_ok: die detected");
77
78 test_out("not ok 1");
79 test_fail(+2);
80 test_diag("died: Local::Error::Overload (overloaded)");
81 lives_ok { Local::Error::Overload->throw };
82 test_test("lives_ok: die detected");
83
84 test_out("ok 1 - expecting normal die");
85 throws_ok { normal_die() } '/normal/', 'expecting normal die';
86 test_test("throws_ok: regex match");
87
88 test_out("not ok 1 - should die");
89 test_fail(+3);
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");
94
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");
98
99 test_out("not ok 1 - threw Local::Error::Simple");
100 test_fail(+3);
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");
105
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");
109
110 test_out("not ok 1 - threw Local::Error::Test");
111 test_fail(+3);
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");
116
117 test_out("not ok 1 - threw Local::Error::Test");
118 test_fail(+3);
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");
123
124 test_out("not ok 1 - threw Local::Error::Overload (overloaded)");
125 test_fail(+3);
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");
130
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");
135
136 test_out("ok 1 - normal exit");
137 throws_ok { no_exception() } qr/^$/, "normal exit";
138 test_test("throws_ok: normal exit matched");
139
140 test_out("ok 1");
141 dies_ok { error("nofallback") };
142 test_test("dies_ok: overload without fallback");
143
144 test_out("not ok 1");
145 test_fail(+2);
146 test_diag("died: Local::Error::NoFallback (no fallback)");
147 lives_ok { error("nofallback") };
148 test_test("lives_ok: overload without fallback");
149
150 test_out("not ok 1 - threw Local::Error::Test");
151 test_fail(+3);
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");
156
157 test_out("ok 1 - ");
158 throws_ok { normal_die() } '/normal/', '';
159 test_test("throws_ok: can pass empty test description");