File Coverage

blib/lib/Test/Tester.pm
Criterion Covered Total %
statement 93 118 78.8
branch 15 32 46.8
condition 5 12 41.6
subroutine 19 22 86.3
pod 6 13 46.1
total 138 197 70.0


line stmt bran cond sub pod time code
1 5     5   5405 use strict;
  5         8  
  5         284  
2              
3             package Test::Tester;
4              
5             BEGIN
6             {
7 5 50   5   90 if (*Test::Builder::new{CODE})
8             {
9 0         0 warn "You should load Test::Tester before Test::Builder (or anything that loads Test::Builder)"
10             }
11             }
12              
13 5     5   4836 use Test::Builder;
  5         55229  
  5         151  
14 5     5   2297 use Test::Tester::CaptureRunner;
  5         18  
  5         113  
15 5     5   2520 use Test::Tester::Delegate;
  5         12  
  5         158  
16              
17             require Exporter;
18              
19 5     5   27 use vars qw( @ISA @EXPORT $VERSION );
  5         6  
  5         7568  
20              
21             $VERSION = "0.109";
22             @EXPORT = qw( run_tests check_tests check_test cmp_results show_space );
23             @ISA = qw( Exporter );
24              
25             my $Test = Test::Builder->new;
26             my $Capture = Test::Tester::Capture->new;
27             my $Delegator = Test::Tester::Delegate->new;
28             $Delegator->{Object} = $Test;
29              
30             my $runner = Test::Tester::CaptureRunner->new;
31              
32             my $want_space = $ENV{TESTTESTERSPACE};
33              
34             sub show_space
35             {
36 0     0 1 0 $want_space = 1;
37             }
38              
39             my $colour = '';
40             my $reset = '';
41              
42             if (my $want_colour = $ENV{TESTTESTERCOLOUR} || $ENV{TESTTESTERCOLOUR})
43             {
44             if (eval "require Term::ANSIColor")
45             {
46             my ($f, $b) = split(",", $want_colour);
47             $colour = Term::ANSIColor::color($f).Term::ANSIColor::color("on_$b");
48             $reset = Term::ANSIColor::color("reset");
49             }
50              
51             }
52              
53             sub new_new
54             {
55 7     7 0 1454 return $Delegator;
56             }
57              
58             sub capture
59             {
60 3     3 0 796 return Test::Tester::Capture->new;
61             }
62              
63             sub fh
64             {
65             # experiment with capturing output, I don't like it
66 0     0 0 0 $runner = Test::Tester::FHRunner->new;
67              
68 0         0 return $Test;
69             }
70              
71             sub find_run_tests
72             {
73 22     22 0 29 my $d = 1;
74 22         20 my $found = 0;
75 22   66     186 while ((not $found) and (my ($sub) = (caller($d))[3]) )
76             {
77             # print "$d: $sub\n";
78 93         106 $found = ($sub eq "Test::Tester::run_tests");
79 93         516 $d++;
80             }
81              
82             # die "Didn't find 'run_tests' in caller stack" unless $found;
83 22         103 return $d;
84             }
85              
86             sub run_tests
87             {
88 16     16 1 1921 local($Delegator->{Object}) = $Capture;
89              
90 16         66 $runner->run_tests(@_);
91              
92 16         61 return ($runner->get_premature, $runner->get_results);
93             }
94              
95             sub check_test
96             {
97 6     6 1 579 my $test = shift;
98 6         8 my $expect = shift;
99 6         8 my $name = shift;
100 6 50       13 $name = "" unless defined($name);
101              
102 6         22 @_ = ($test, [$expect], $name);
103 6         23 goto &check_tests;
104             }
105              
106             sub check_tests
107             {
108 7     7 1 171 my $test = shift;
109 7         12 my $expects = shift;
110 7         8 my $name = shift;
111 7 50       17 $name = "" unless defined($name);
112              
113 7         8 my ($prem, @results) = eval { run_tests($test, $name) };
  7         19  
114              
115 7 50       18 $Test->ok(! $@, "Test '$name' completed") || $Test->diag($@);
116 7 50       2240 $Test->ok(! length($prem), "Test '$name' no premature diagnostication") ||
117             $Test->diag("Before any testing anything, your tests said\n$prem");
118              
119 7         2007 local $Test::Builder::Level = $Test::Builder::Level + 1;
120 7         20 cmp_results(\@results, $expects, $name);
121 7         1983 return ($prem, @results);
122             }
123              
124             sub cmp_field
125             {
126 60     60 0 110 my ($result, $expect, $field, $desc) = @_;
127              
128 60 50       164 if (defined $expect->{$field})
129             {
130 60         261 $Test->is_eq($result->{$field}, $expect->{$field},
131             "$desc compare $field");
132             }
133             }
134              
135             sub cmp_result
136             {
137 12     12 1 19 my ($result, $expect, $name) = @_;
138              
139 12         21 my $sub_name = $result->{name};
140 12 50       27 $sub_name = "" unless defined($name);
141              
142 12         59 my $desc = "subtest '$sub_name' of '$name'";
143              
144             {
145 12         14 local $Test::Builder::Level = $Test::Builder::Level + 1;
  12         19  
146              
147 12         19 cmp_field($result, $expect, "ok", $desc);
148              
149 12         4485 cmp_field($result, $expect, "actual_ok", $desc);
150              
151 12         4550 cmp_field($result, $expect, "type", $desc);
152              
153 12         4277 cmp_field($result, $expect, "reason", $desc);
154              
155 12         4197 cmp_field($result, $expect, "name", $desc);
156             }
157              
158             # if we got no depth then default to 1
159 12         4255 my $depth = 1;
160 12 50       43 if (exists $expect->{depth})
161             {
162 12         22 $depth = $expect->{depth};
163             }
164              
165             # if depth was explicitly undef then don't test it
166 12 50       23 if (defined $depth)
167             {
168 12 50       35 $Test->is_eq($result->{depth}, $depth, "checking depth") ||
169             $Test->diag('You need to change $Test::Builder::Level');
170             }
171              
172 12 50       4109 if (defined(my $exp = $expect->{diag}))
173             {
174             # if there actually is some diag then put a \n on the end if it's not
175             # there already
176              
177 12 100 100     72 $exp .= "\n" if (length($exp) and $exp !~ /\n$/);
178 12 50       61 if (not $Test->ok($result->{diag} eq $exp,
179             "subtest '$sub_name' of '$name' compare diag")
180             )
181             {
182 0         0 my $got = $result->{diag};
183 0         0 my $glen = length($got);
184 0         0 my $elen = length($exp);
185 0         0 for ($got, $exp)
186             {
187 0         0 my @lines = split("\n", $_);
188             $_ = join("\n", map {
189 0 0       0 if ($want_space)
  0         0  
190             {
191 0         0 $_ = $colour.escape($_).$reset;
192             }
193             else
194             {
195 0         0 "'$colour$_$reset'"
196             }
197             } @lines);
198             }
199              
200 0         0 $Test->diag(<
201             Got diag ($glen bytes):
202             $got
203             Expected diag ($elen bytes):
204             $exp
205             EOM
206              
207             }
208             }
209             }
210              
211             sub escape
212             {
213 0     0 0 0 my $str = shift;
214 0         0 my $res = '';
215 0         0 for my $char (split("", $str))
216             {
217 0         0 my $c = ord($char);
218 0 0 0     0 if(($c>32 and $c<125) or $c == 10)
      0        
219             {
220 0         0 $res .= $char;
221             }
222             else
223             {
224 0         0 $res .= sprintf('\x{%x}', $c)
225             }
226             }
227 0         0 return $res;
228             }
229              
230             sub cmp_results
231             {
232 7     7 1 12 my ($results, $expects, $name) = @_;
233              
234 7         37 $Test->is_num(scalar @$results, scalar @$expects, "Test '$name' result count");
235              
236 7         2732 for (my $i = 0; $i < @$expects; $i++)
237             {
238 12         1436 my $expect = $expects->[$i];
239 12         18 my $result = $results->[$i];
240              
241 12         18 local $Test::Builder::Level = $Test::Builder::Level + 1;
242 12         25 cmp_result($result, $expect, $name);
243             }
244             }
245              
246             ######## nicked from Test::More
247             sub plan {
248 5     5 0 20 my(@plan) = @_;
249              
250 5         12 my $caller = caller;
251              
252 5         24 $Test->exported_to($caller);
253              
254 5         47 my @imports = ();
255 5         70 foreach my $idx (0..$#plan) {
256 2 50       7 if( $plan[$idx] eq 'import' ) {
257 0         0 my($tag, $imports) = splice @plan, $idx, 2;
258 0         0 @imports = @$imports;
259 0         0 last;
260             }
261             }
262              
263 5         42 $Test->plan(@plan);
264              
265 5         169 __PACKAGE__->_export_to_level(1, __PACKAGE__, @imports);
266             }
267              
268             sub import {
269 5     5   31 my($class) = shift;
270             {
271 5     5   29 no warnings 'redefine';
  5         8  
  5         670  
  5         8  
272 5         54 *Test::Builder::new = \&new_new;
273             }
274 5         27 goto &plan;
275             }
276              
277             sub _export_to_level
278             {
279 5     5   13 my $pkg = shift;
280 5         9 my $level = shift;
281 5         8 (undef) = shift; # redundant arg
282 5         11 my $callpkg = caller($level);
283 5         613 $pkg->export($callpkg, @_);
284             }
285              
286              
287             ############
288              
289             1;
290              
291             __END__