File Coverage

inc/Test/Exception.pm
Criterion Covered Total %
statement 50 79 63.2
branch 5 20 25.0
condition 3 12 25.0
subroutine 14 17 82.3
pod 4 4 100.0
total 76 132 57.5


line stmt bran cond sub pod time code
1             #line 1
2             #! /usr/bin/perl -w
3              
4 3     3   80401 package Test::Exception;
  3         11  
  3         109  
5 3     3   15 use 5.005;
  3         5  
  3         92  
6 3     3   14 use strict;
  3         6  
  3         60  
7 3     3   1622 use Test::Builder;
  3         6  
  3         161  
8 3     3   16 use Sub::Uplevel;
  3         3  
  3         298  
9             use base qw(Exporter);
10 3     3   13  
  3         8  
  3         2347  
11             use vars qw($VERSION @EXPORT @EXPORT_OK);
12              
13             $VERSION = '0.21';
14             @EXPORT = qw(dies_ok lives_ok throws_ok lives_and);
15              
16             my $Tester = Test::Builder->new;
17              
18 3     3   18 sub import {
19 3 50       16 my $self = shift;
20 0         0 if (@_) {
21 0         0 my $package = caller;
22 0         0 $Tester->exported_to($package);
23             $Tester->plan(@_);
24 3         764 };
25             $self->export_to_level(1, $self, $_) foreach @EXPORT;
26             }
27              
28             #line 76
29              
30              
31             sub _try_as_caller {
32             my $coderef = shift;
33             eval { uplevel 3, $coderef };
34             return $@;
35             };
36              
37              
38             sub _is_exception {
39             my $exception = shift;
40             ref($exception) || $exception ne '';
41             };
42              
43              
44             sub _exception_as_string {
45             my ($prefix, $exception) = @_;
46             return "$prefix undef" unless defined($exception);
47             return "$prefix normal exit" unless _is_exception($exception);
48             my $class = ref($exception);
49             $exception = "$class ($exception)"
50             if $class && "$exception" !~ m/^\Q$class/;
51             chomp($exception);
52             return("$prefix $exception");
53             };
54              
55              
56             #line 123
57              
58              
59             sub dies_ok (&;$) {
60             my ($coderef, $description) = @_;
61             my $exception = _try_as_caller($coderef);
62             my $ok = $Tester->ok( _is_exception($exception), $description );
63             $@ = $exception;
64             return($ok);
65             }
66              
67              
68             #line 163
69              
70             sub lives_ok (&;$) {
71             my ($coderef, $description) = @_;
72             my $exception = _try_as_caller($coderef);
73             my $ok = $Tester->ok(! _is_exception($exception), $description)
74             || $Tester->diag(_exception_as_string("died:", $exception));
75             $@ = $exception;
76             return($ok);
77             }
78              
79 22     22   42  
80 22         42 #line 220
  22         145  
81 22         166  
82              
83             sub throws_ok (&$;$) {
84             my ($coderef, $expecting, $description) = @_;
85             $description ||= _exception_as_string("threw", $expecting);
86 27     27   69 my $exception = _try_as_caller($coderef);
87 27 50       418 my $regex = $Tester->maybe_regex($expecting);
88             my $ok = $regex ? ($exception =~ m/$regex/)
89             : UNIVERSAL::isa($exception, ref($expecting) || $expecting);
90             $Tester->ok($ok, $description);
91             unless ($ok) {
92 5     5   7824 $Tester->diag( _exception_as_string("expecting:", $expecting) );
93 5 50       23 $Tester->diag( _exception_as_string("found:", $exception) );
94 5 50       15 };
95 5         11 $@ = $exception;
96 5 50 33     60 return($ok);
97             };
98 5         15  
99 5         27  
100             #line 268
101              
102             sub lives_and (&$) {
103             my ($test, $description) = @_;
104             {
105             local $Test::Builder::Level = $Test::Builder::Level+1;
106             my $ok = \&Test::Builder::ok;
107             no warnings;
108             local *Test::Builder::ok = sub {
109             $_[2] = $description unless defined $_[2];
110             $ok->(@_);
111             };
112             use warnings;
113             eval { $test->() } and return 1;
114             };
115             my $exception = $@;
116             if (_is_exception($exception)) {
117             $Tester->ok(0, $description);
118             $Tester->diag( _exception_as_string("died:", $exception) );
119             };
120             $@ = $exception;
121             return;
122             }
123              
124             #line 367
125              
126 6     6 1 5485 1;