File Coverage

blib/lib/Test/Most.pm
Criterion Covered Total %
statement 201 219 91.7
branch 44 58 75.8
condition 12 18 66.6
subroutine 37 41 90.2
pod 10 10 100.0
total 304 346 87.8


line stmt bran cond sub pod time code
1             package Test::Most;
2              
3 22     22   249417 use warnings;
  18         34  
  18         1157  
4 22     22   128 use strict;
  22         130  
  22         800  
5              
6 22     22   7790 use Test::Most::Exception 'throw_failure';
  22         58  
  22         1338  
7              
8             # XXX don't use 'base' as it can override signal handlers
9 22     22   8806 use Test::Builder::Module;
  22         1589325  
  22         265  
10             our ( @ISA, @EXPORT, $DATA_DUMPER_NAMES_INSTALLED );
11             my $HAVE_TIME_HIRES;
12              
13             BEGIN {
14              
15 22     22   35072 require Test::More;
16 22 50       98143 if (Test::More->can('TB_PROVIDER_META')) {
17 4         42 Test::More->import(import => [ '!explain' ]);
18             }
19             else {
20             # There's some strange fiddling around with import(), so this allows us to
21             # be nicely backwards compatible to earlier versions of Test::More.
22 19         137 local @Test::More::EXPORT = grep { $_ ne 'explain' } @Test::More::EXPORT;
  504         661  
23 18         140 Test::More->import;
24             }
25              
26 19     19   6177 eval "use Time::HiRes";
  19         175  
  19         38  
  19         169  
27 19 50       1157 $HAVE_TIME_HIRES = 1 unless $@;
28             }
29              
30 19     19   87 use Test::Builder;
  19         32  
  19         750  
31             my $OK_FUNC;
32             BEGIN {
33 19     19   2964 $OK_FUNC = \&Test::Builder::ok;
34             }
35              
36             our $VERSION = '0.42';
37             $VERSION = eval $VERSION;
38              
39             BEGIN {
40 19     19   455 @ISA = qw(Test::Builder::Module);
41             @EXPORT = (
42             Test::More->can('TB_PROVIDER_META')
43 19 50       4859 ? grep { $_ ne 'TODO' } keys( %{Test::More->TB_PROVIDER_META->{attrs}})
  1         3  
  1         91  
44             : @Test::More::EXPORT,
45             qw<
46             $TODO
47             all_done
48             bail_on_fail
49             die_on_fail
50             explain
51             always_explain
52             last_test_failed
53             restore_fail
54             set_failure_handler
55             show
56             always_show
57             >
58             );
59             }
60              
61             sub import {
62 25     25   135139 my $class = shift;
63 25         39 my $bail_set = 0;
64 25         334 local @EXPORT = @EXPORT; # localize effect of %exclude_symbol
65              
66 25         56 my %modules_to_load = map { $_ => 1 } qw/
  100         269  
67             Test::Differences
68             Test::Exception
69             Test::Deep
70             Test::Warn
71             /;
72 25         705 warnings->import;
73 25         132 strict->import;
74 19     19   2472 eval "use Data::Dumper::Names 0.03";
  1         35  
  1         4  
  25         1624  
75 25         81 $DATA_DUMPER_NAMES_INSTALLED = !$@;
76              
77 25 100       181 if ( $ENV{BAIL_ON_FAIL} ) {
78 1         1 $bail_set = 1;
79 1         3 bail_on_fail();
80             }
81 25 100 100     150 if ( !$bail_set and $ENV{DIE_ON_FAIL} ) {
82 1         22 die_on_fail();
83             }
84 25         145 for my $i ( 0 .. $#_ ) {
85 44 100       187 if ( 'bail' eq $_[$i] ) {
86 1         2 splice @_, $i, 1;
87 1         2 bail_on_fail();
88 1         1 $bail_set = 1;
89 1         1 last;
90             }
91             }
92 25         95 my $caller = caller;
93 25         76 for my $i ( 0 .. $#_ ) {
94 45 100       104 if ( 'timeit' eq $_[$i] ) {
95 1         3 splice @_, $i, 1;
96 18     19   131 no strict;
  18         26  
  19         10493  
97 1         1 *{"${caller}::timeit"} = \&timeit;
  1         5  
98 1         2 last;
99             }
100             }
101              
102 25         42 my %exclude_symbol;
103 25         52 my $i = 0;
104              
105             # Pull out `import => [...]` so it doesn't fall through to plan(), which
106             # doesn't understand it. The list drives export_to_level below.
107             #
108             # The while-with-index loop is intentional: we mutate @_ via splice as
109             # we go, and this matches the existing parser further down the sub.
110 25         68 my @explicit;
111 25         76 while ( $i < @_ ) {
112 37 100 66     106 if ( $_[$i] eq 'import' && ref $_[ $i + 1 ] eq 'ARRAY' ) {
113 6         5 @explicit = @{ ( splice @_, $i, 2 )[1] };
  6         18  
114 6         9 last;
115             }
116 31         76 $i++;
117             }
118 25         36 $i = 0;
119              
120 25         47 foreach my $do_not_import_by_default (qw/blessed reftype/) {
121 50 100       124 if ( grep { $_ eq $do_not_import_by_default } @_, @explicit ) {
  82         149  
122 2         2 @_ = grep { $_ ne $do_not_import_by_default } @_;
  1         3  
123             # If the user opted in positionally AND used import => [...],
124             # the explicit list is the export source — make sure the symbol
125             # is in it, or it would be silently dropped.
126 2 100 66     4 if ( @explicit
127 6         9 && !grep { $_ eq $do_not_import_by_default } @explicit ) {
128 1         1 push @explicit, $do_not_import_by_default;
129             }
130             }
131             else {
132 48         125 $exclude_symbol{$do_not_import_by_default} = 1;
133             }
134             }
135              
136 25         67 while ($i < @_) {
137 35 100 100     141 if ( !$bail_set and ( 'die' eq $_[$i] ) ) {
138 1         2 splice @_, $i, 1;
139 1         2 die_on_fail();
140 1         1 $i = 0;
141 1         2 next;
142             }
143 34 100       96 if ( $_[$i] =~ /^-(.*)/ ) {
144 2         5 my $module = $1;
145 2         5 splice @_, $i, 1;
146 2 50       5 unless (exists $modules_to_load{$module}) {
147 0         0 require Carp;
148 0         0 Carp::croak("Cannot remove non-existent Test::Module ($module)");
149             }
150 2         5 delete $modules_to_load{$module};
151 2         3 $i = 0;
152 2         5 next;
153             }
154 32 100       78 if ( $_[$i] =~ /^!(.*)/ ) {
155 3         9 splice @_, $i, 1;
156 3         10 $exclude_symbol{$1} = 1;
157 3         7 $i = 0;
158 3         8 next;
159             }
160 29 100       55 if ( 'defer_plan' eq $_[$i] ) {
161 1         3 require Carp;
162 1 50       2 Carp::carp(<<'END') unless $ENV{DO_NOT_WARN_ON_DEFER_PLAN};
163             defer_plan() is deprecated and will be removed in a future release of
164             Test::Most. It's functionality is provided by Test::More's done_testing(),
165             first added in 2009 (0.88).
166             END
167 1         3 splice @_, $i, 1;
168              
169 1         3 my $builder = Test::Builder->new;
170              
171             # XXX I don't like setting this directly, but
172             # Test::Builder::has_plan isn't public
173 1         8 $builder->{Have_Plan} = 1;
174 1         2 $builder->{TEST_MOST_deferred_plan} = 1;
175 1         1 $builder->{TEST_MOST_all_done} = 0;
176 1         1 $i = 0;
177 1         3 next;
178             }
179 28         49 $i++;
180             }
181 25         280 local @EXPORT = @EXPORT; # localize effect of %exclude_symbol
182 25         99 foreach my $module (keys %modules_to_load) {
183 19     19   11121 eval "use $module";
  19     19   135370  
  18     18   2673  
  18     17   12654  
  18         219859  
  18         650  
  18         11039  
  18         181993  
  18         805  
  17         10458  
  17         159074  
  17         438  
  98         7980  
184              
185 98 50       10998 if ( my $error = $@) {
186 0         0 require Carp;
187 0         0 Carp::croak($error);
188             }
189 19     19   156 no strict 'refs';
  19         32  
  19         9118  
190 98         189 push @EXPORT => grep { !$exclude_symbol{$_} } @{"${module}::EXPORT"};
  1548         4641  
  98         1873  
191             }
192              
193 25         316 my $test = $class->builder;
194 25         369 $test->exported_to($caller);
195 25         376 $test->plan(@_);
196              
197             # Empty @explicit covers two cases that should both fall through to the
198             # default exports: no `import =>` was given, or `import => []` was given
199             # (which matches Test::Builder::Module / Exporter, where an empty import
200             # list means "use defaults"). When @explicit has items but `!sym`
201             # exclusions empty it out, @to_export is empty and we export nothing.
202             my @to_export
203 25 100       13060 = @explicit ? grep { !$exclude_symbol{$_} } @explicit : @EXPORT;
  10         17  
204 25 100       22575 $class->export_to_level( 1, $class, @to_export ) if @to_export;
205             }
206              
207             sub explain {
208 6     6 1 325805 _explain(\&Test::More::note, @_);
209             }
210              
211              
212             sub timeit(&;$) {
213 2     2 1 160519 my ( $code, $message ) = @_;
214 2 50       9 unless($HAVE_TIME_HIRES) {
215 0         0 Test::Most::diag("timeit: Time::HiRes not installed");
216 0         0 $code->();
217             }
218 2 100       5 if ( !$message ) {
219 1         4 my ( $package, $filename, $line ) = caller;
220 1         3 $message = "$filename line $line";
221             }
222 2         8 my $start = [Time::HiRes::gettimeofday()];
223 2         5 $code->();
224 2         1181 explain(
225             sprintf "$message: took %s seconds" => Time::HiRes::tv_interval($start) );
226             }
227              
228             sub always_explain {
229 2     2 1 2807 _explain(\&Test::More::diag, @_);
230             }
231              
232             sub _explain {
233 10     10   17 my $diag = shift;
234 19     19   148 no warnings 'once';
  19         86  
  19         4889  
235             $diag->(
236             map {
237 10         20 ref $_
238 14 100       145 ? do {
239 5         28 require Data::Dumper;
240 5         8 local $Data::Dumper::Indent = 1;
241 5         7 local $Data::Dumper::Sortkeys = 1;
242 5         5 local $Data::Dumper::Terse = 1;
243 5         13 Data::Dumper::Dumper($_);
244             }
245             : $_
246             } @_
247             );
248             }
249              
250             sub show {
251 1     1 1 1049 _show(\&Test::More::note, @_);
252             }
253              
254             sub always_show {
255 1     1 1 939 _show(\&Test::More::diag, @_);
256             }
257              
258             sub _show {
259 2 50   2   6 unless ( $DATA_DUMPER_NAMES_INSTALLED ) {
260 2         10 require Carp;
261 2         27 Carp::carp("Data::Dumper::Names 0.03 not found. Use explain() instead of show()");
262 2         462 goto &_explain;
263             }
264 0         0 my $diag = shift;
265 19     19   166 no warnings 'once';
  19         86  
  19         3731  
266 0         0 local $Data::Dumper::Indent = 1;
267 0         0 local $Data::Dumper::Sortkeys = 1;
268 0         0 local $Data::Dumper::Names::UpLevel = $Data::Dumper::Names::UpLevel + 2;
269 0         0 $diag->(Data::Dumper::Names::Dumper(@_));
270             }
271              
272             sub die_on_fail {
273 0     0 1 0 set_failure_handler( sub { throw_failure } );
  3     3   160761  
274             }
275              
276             sub bail_on_fail {
277             set_failure_handler(
278 0     0 1 0 sub { Test::More::BAIL_OUT("Test failed. BAIL OUT!.\n") } );
  3     3   247332  
279             }
280              
281             sub restore_fail {
282 19     19   119 no warnings 'redefine';
  19         29  
  19         2928  
283 0     0 1 0 *Test::Builder::ok = $OK_FUNC;
284             }
285              
286             sub all_done {
287 1     1 1 190684 my $builder = Test::Builder->new;
288 1 50       7 if ($builder->{TEST_MOST_deferred_plan}) {
289 1         2 $builder->{TEST_MOST_all_done} = 1;
290 1 50       5 $builder->expected_tests(@_ ? $_[0] : $builder->current_test);
291             }
292             }
293              
294              
295             sub set_failure_handler {
296 6     6 1 17 my $action = shift;
297 18     19   134 no warnings 'redefine';
  18         31  
  19         4442  
298 6         30 Test::Builder->new->{TEST_MOST_failure_action} = $action; # for DESTROY
299             *Test::Builder::ok = sub {
300 38     38   798728 local $Test::Builder::Level = $Test::Builder::Level + 1;
301 38         53 my $builder = $_[0];
302 38 50       100 if ( $builder->{TEST_MOST_test_failed} ) {
303 0         0 $builder->{TEST_MOST_test_failed} = 0;
304 0         0 $action->($builder);
305             }
306 38         54 $builder->{TEST_MOST_test_failed} = 0;
307 38         91 my $result = $OK_FUNC->(@_);
308 38         18192 $builder->{TEST_MOST_test_failed} = !( $builder->summary )[-1];
309 38         5608 return $result;
310 6         94 };
311             }
312              
313             {
314 19     19   148 no warnings 'redefine';
  19         34  
  19         4536  
315             my $orig_destroy = Test::Builder->can('DESTROY');
316              
317             # we need this because if the failure is on the final test, we won't have
318             # a subsequent test triggering the behavior.
319             *Test::Builder::DESTROY = sub {
320 10     10   13635 my $builder = $_[0];
321 10 50       21 if ( $builder->{TEST_MOST_test_failed} ) {
322 0   0 0   0 ( $builder->{TEST_MOST_failure_action} || sub {} )->();
323             }
324 10 50       32 $orig_destroy->(@_) if $orig_destroy;
325             };
326             }
327              
328             sub _deferred_plan_handler {
329 18     18   110 my $builder = Test::Builder->new;
330 18 50 66     718 if ($builder->{TEST_MOST_deferred_plan} and !$builder->{TEST_MOST_all_done})
331             {
332 0           $builder->expected_tests($builder->current_test + 1);
333             }
334             }
335              
336             # This should work because the END block defined by Test::Builder should be
337             # guaranteed to be run before t one, since we use'd Test::Builder way up top.
338             # The other two alternatives would be either to replace Test::Builder::_ending
339             # similar to how we did Test::Builder::ok, or to call Test::Builder::no_ending
340             # and basically rewrite _ending in our own image. Neither is very palatable,
341             # considering _ending's initial underscore.
342              
343             END {
344 18     18   1637600 _deferred_plan_handler();
345             }
346              
347             1;
348             __END__