File: Exception.t

package info (click to toggle)
libtest-exception-perl 0.43-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 176 kB
  • ctags: 11
  • sloc: perl: 400; makefile: 2
file content (162 lines) | stat: -rw-r--r-- 4,542 bytes parent folder | download | duplicates (4)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#! /usr/bin/perl -Tw

use strict;
use warnings;

use Test::Builder::Tester tests => 20;
use Test::More;

BEGIN { use_ok( 'Test::Exception' ) };

{
	package Local::Error::Simple;

	my %Exception_singleton;
	
	sub instance { 
		my $class = shift;
		return $Exception_singleton{$class} ||= bless {}, $class;
	};

	sub throw {
		my $class = shift;
		die $class->instance;
	};

	package Local::Error::Test;
	use base qw(Local::Error::Simple);

	package Local::Error::Overload;
	use base qw(Local::Error::Simple);
	use overload q{""} => sub { "overloaded" }, fallback => 1;

	package Local::Error::NoFallback;
	use base qw(Local::Error::Simple);
	use overload q{""} => sub { "no fallback" };
};


my %Exception = map {m/([^:]+)$/; lc $1 => $_->instance} qw(
	Local::Error::Simple 
	Local::Error::Test 
	Local::Error::Overload 
	Local::Error::NoFallback
);


sub error {
	my $type = shift;
	die $Exception{$type} if exists $Exception{$type};
	warn "exiting: unrecognised error type $type\n";
	exit(1);
};

sub no_exception { "this subroutine does not die" };

sub normal_die { die "a normal die\n" };


test_out("ok 1");
dies_ok { normal_die() };
test_test("dies_ok: die");

test_out("not ok 1 - lived. oops");
test_fail(+1);
dies_ok { no_exception() } "lived. oops";
test_test("dies_ok: normal exit detected");

test_out("ok 1 - lived");
lives_ok { no_exception() } "lived";
test_test("lives_ok: normal exit");

test_out("not ok 1");
test_fail(+2);
test_diag("died: a normal die");
lives_ok { normal_die() };
test_test("lives_ok: die detected");

test_out("not ok 1");
test_fail(+2);
test_diag("died: Local::Error::Overload (overloaded)");
lives_ok { Local::Error::Overload->throw };
test_test("lives_ok: die detected");

test_out("ok 1 - expecting normal die");
throws_ok { normal_die() } '/normal/', 'expecting normal die';
test_test("throws_ok: regex match");

test_out("not ok 1 - should die");
test_fail(+3);
test_diag("expecting: /abnormal/");
test_diag("found: a normal die");
throws_ok { normal_die() } '/abnormal/', 'should die';
test_test("throws_ok: regex bad match detected");

test_out("ok 1 - threw Local::Error::Simple");
throws_ok { Local::Error::Simple->throw } "Local::Error::Simple";
test_test("throws_ok: identical exception class");

test_out("not ok 1 - threw Local::Error::Simple");
test_fail(+3);
test_diag("expecting: Local::Error::Simple");
test_diag("found: normal exit");
throws_ok { no_exception() } "Local::Error::Simple";
test_test("throws_ok: exception on normal exit");

test_out("ok 1 - threw Local::Error::Simple");
throws_ok { Local::Error::Test->throw } "Local::Error::Simple";
test_test("throws_ok: exception sub-class");

test_out("not ok 1 - threw Local::Error::Test");
test_fail(+3);
test_diag("expecting: Local::Error::Test");
test_diag("found: " . Local::Error::Simple->instance);
throws_ok { error("simple") } "Local::Error::Test";
test_test("throws_ok: bad sub-class match detected");

test_out("not ok 1 - threw Local::Error::Test");
test_fail(+3);
test_diag("expecting: Local::Error::Test");
test_diag("found: Local::Error::Overload (overloaded)");
throws_ok { error("overload") } "Local::Error::Test";
test_test("throws_ok: throws_ok found overloaded");

test_out("not ok 1 - threw Local::Error::Overload (overloaded)");
test_fail(+3);
test_diag("expecting: Local::Error::Overload (overloaded)");
test_diag("found: $Exception{test}");
throws_ok { error("test") } $Exception{overload};
test_test("throws_ok: throws_ok found overloaded");

my $e = Local::Error::Test->instance("hello");
test_out("ok 1 - threw $e");
throws_ok { error("test") } $e;
test_test("throws_ok: class from object match");

test_out("ok 1 - normal exit");
throws_ok { no_exception() } qr/^$/, "normal exit";
test_test("throws_ok: normal exit matched");

test_out("ok 1");
dies_ok { error("nofallback") };
test_test("dies_ok: overload without fallback");

test_out("not ok 1");
test_fail(+2);
test_diag("died: Local::Error::NoFallback (no fallback)");
lives_ok { error("nofallback") };
test_test("lives_ok: overload without fallback");

test_out("not ok 1 - threw Local::Error::Test");
test_fail(+3);
test_diag("expecting: Local::Error::Test");
test_diag("found: Local::Error::NoFallback (no fallback)");
throws_ok { error("nofallback") } "Local::Error::Test";
test_test("throws_ok: throws_ok overload without fallback");

test_out("ok 1 - ");
throws_ok { normal_die() } '/normal/', '';
{
    local $TODO = "See http://github.com/schwern/test-more/issues/issue/84";
    test_test("throws_ok: can pass empty test description");
}