Commit | Line | Data |
---|---|---|
33459055 MS |
1 | package Test::Builder; |
2 | ||
6bdb8877 | 3 | use 5.008001; |
33459055 | 4 | use strict; |
ccbd73a4 | 5 | use warnings; |
cd06ac21 | 6 | |
6bdb8877 CG |
7 | use Test::Builder::Util qw/try protect/; |
8 | use Scalar::Util(); | |
9 | use Test::Builder::Stream; | |
10 | use Test::Builder::Result; | |
11 | use Test::Builder::Result::Ok; | |
12 | use Test::Builder::Result::Diag; | |
13 | use Test::Builder::Result::Note; | |
14 | use Test::Builder::Result::Plan; | |
15 | use Test::Builder::Result::Bail; | |
16 | use Test::Builder::Result::Child; | |
17 | use Test::Builder::Trace; | |
18 | ||
19 | our $VERSION = '1.301001_034'; | |
ccbd73a4 | 20 | $VERSION = eval $VERSION; ## no critic (BuiltinFunctions::ProhibitStringyEval) |
33459055 | 21 | |
6bdb8877 CG |
22 | # The mostly-singleton, and other package vars. |
23 | our $Test = Test::Builder->new; | |
24 | our $Level = 1; | |
25 | our $BLevel = 1; | |
7483b81c | 26 | |
6bdb8877 CG |
27 | #################### |
28 | # {{{ MAGIC things # | |
29 | #################### | |
7483b81c | 30 | |
6bdb8877 CG |
31 | sub DESTROY { |
32 | my $self = shift; | |
33 | if ( $self->parent and $$ == $self->{Original_Pid} ) { | |
34 | my $name = $self->name; | |
35 | $self->parent->{In_Destroy} = 1; | |
36 | $self->parent->ok(0, $name, "Child ($name) exited without calling finalize()\n"); | |
a344be10 MS |
37 | } |
38 | } | |
39 | ||
6bdb8877 CG |
40 | require Test::Builder::ExitMagic; |
41 | my $final = Test::Builder::ExitMagic->new( | |
42 | tb => Test::Builder->create(shared_stream => 1), | |
43 | ); | |
44 | END { $final->do_magic() } | |
33459055 | 45 | |
6bdb8877 CG |
46 | #################### |
47 | # }}} MAGIC things # | |
48 | #################### | |
33459055 | 49 | |
6bdb8877 CG |
50 | #################### |
51 | # {{{ Constructors # | |
52 | #################### | |
ccbd73a4 | 53 | |
33459055 | 54 | sub new { |
6bdb8877 CG |
55 | my $class = shift; |
56 | my %params = @_; | |
57 | $Test ||= $class->create(shared_stream => 1); | |
58 | ||
33459055 MS |
59 | return $Test; |
60 | } | |
61 | ||
5143c659 RGS |
62 | sub create { |
63 | my $class = shift; | |
6bdb8877 | 64 | my %params = @_; |
5143c659 RGS |
65 | |
66 | my $self = bless {}, $class; | |
6bdb8877 | 67 | $self->reset(%params); |
5143c659 RGS |
68 | |
69 | return $self; | |
70 | } | |
71 | ||
411e93ce SH |
72 | # Copy an object, currently a shallow. |
73 | # This does *not* bless the destination. This keeps the destructor from | |
74 | # firing when we're just storing a copy of the object to restore later. | |
75 | sub _copy { | |
76 | my($src, $dest) = @_; | |
77 | ||
78 | %$dest = %$src; | |
6bdb8877 | 79 | #_share_keys($dest); # Not sure the implications here. |
411e93ce SH |
80 | |
81 | return; | |
82 | } | |
83 | ||
6bdb8877 CG |
84 | #################### |
85 | # }}} Constructors # | |
86 | #################### | |
411e93ce | 87 | |
6bdb8877 CG |
88 | ############################################## |
89 | # {{{ Simple accessors/generators/deligators # | |
90 | ############################################## | |
2c4d5b9b | 91 | |
6bdb8877 CG |
92 | sub listen { shift->stream->listen(@_) } |
93 | sub munge { shift->stream->munge(@_) } | |
94 | sub tap { shift->stream->tap } | |
95 | sub lresults { shift->stream->lresults } | |
96 | sub is_passing { shift->stream->is_passing(@_) } | |
97 | sub use_fork { shift->stream->use_fork } | |
98 | sub no_fork { shift->stream->no_fork } | |
2c4d5b9b | 99 | |
6bdb8877 CG |
100 | BEGIN { |
101 | Test::Builder::Util::accessors(qw/Parent Name _old_level _bailed_out default_name/); | |
102 | Test::Builder::Util::accessor(modern => sub {$ENV{TB_MODERN} || 0}); | |
103 | Test::Builder::Util::accessor(depth => sub { 0 }); | |
104 | } | |
2c4d5b9b | 105 | |
6bdb8877 CG |
106 | ############################################## |
107 | # }}} Simple accessors/generators/deligators # | |
108 | ############################################## | |
2c4d5b9b | 109 | |
6bdb8877 CG |
110 | ######################### |
111 | # {{{ Stream Management # | |
112 | ######################### | |
113 | ||
114 | sub stream { | |
115 | my $self = shift; | |
116 | ||
117 | ($self->{stream}) = @_ if @_; | |
2c4d5b9b | 118 | |
6bdb8877 CG |
119 | # If no stream is set use shared. We do not want to cache that we use |
120 | # shared cause shared is a stack, not a constant, and we always want the | |
121 | # top. | |
122 | return $self->{stream} || Test::Builder::Stream->shared; | |
123 | } | |
124 | ||
125 | sub intercept { | |
126 | my $self = shift; | |
127 | my ($code) = @_; | |
128 | ||
129 | Carp::croak("argument to intercept must be a coderef, got: $code") | |
130 | unless reftype $code eq 'CODE'; | |
131 | ||
132 | my $stream = Test::Builder::Stream->new(no_follow => 1) || die "Internal Error!"; | |
133 | $stream->exception_followup; | |
134 | ||
135 | local $self->{stream} = $stream; | |
136 | ||
137 | my @results; | |
138 | $stream->listen(INTERCEPTOR => sub { | |
139 | my ($item) = @_; | |
140 | push @results => $item; | |
141 | }); | |
142 | $code->($stream); | |
143 | ||
144 | return \@results; | |
145 | } | |
146 | ||
147 | ######################### | |
148 | # }}} Stream Management # | |
149 | ######################### | |
150 | ||
151 | ############################# | |
152 | # {{{ Children and subtests # | |
153 | ############################# | |
2c4d5b9b SH |
154 | |
155 | sub child { | |
6bdb8877 | 156 | my( $self, $name, $is_subtest ) = @_; |
2c4d5b9b | 157 | |
6bdb8877 CG |
158 | $self->croak("You already have a child named ($self->{Child_Name}) running") |
159 | if $self->{Child_Name}; | |
2c4d5b9b | 160 | |
809046db CBW |
161 | my $parent_in_todo = $self->in_todo; |
162 | ||
163 | # Clear $TODO for the child. | |
164 | my $orig_TODO = $self->find_TODO(undef, 1, undef); | |
165 | ||
6bdb8877 | 166 | my $class = Scalar::Util::blessed($self); |
411e93ce | 167 | my $child = $class->create; |
2c4d5b9b | 168 | |
6bdb8877 | 169 | $child->{stream} = $self->stream->spawn; |
411e93ce SH |
170 | |
171 | # Ensure the child understands if they're inside a TODO | |
6bdb8877 CG |
172 | $child->tap->failure_output($self->tap->todo_output) |
173 | if $parent_in_todo && $self->tap; | |
2c4d5b9b SH |
174 | |
175 | # This will be reset in finalize. We do this here lest one child failure | |
176 | # cause all children to fail. | |
177 | $child->{Child_Error} = $?; | |
178 | $? = 0; | |
6bdb8877 | 179 | |
2c4d5b9b | 180 | $child->{Parent} = $self; |
809046db | 181 | $child->{Parent_TODO} = $orig_TODO; |
2c4d5b9b | 182 | $child->{Name} = $name || "Child of " . $self->name; |
2c4d5b9b | 183 | |
6bdb8877 | 184 | $self->{Child_Name} = $child->name; |
2c4d5b9b | 185 | |
6bdb8877 | 186 | $child->depth($self->depth + 1); |
2c4d5b9b | 187 | |
6bdb8877 CG |
188 | my $res = Test::Builder::Result::Child->new( |
189 | $self->context, | |
190 | name => $child->name, | |
191 | action => 'push', | |
192 | in_todo => $self->in_todo || 0, | |
193 | is_subtest => $is_subtest || 0, | |
194 | ); | |
195 | $self->stream->send($res); | |
2c4d5b9b | 196 | |
6bdb8877 CG |
197 | return $child; |
198 | } | |
2c4d5b9b SH |
199 | |
200 | sub subtest { | |
201 | my $self = shift; | |
6bdb8877 | 202 | my($name, $subtests, @args) = @_; |
2c4d5b9b | 203 | |
6bdb8877 CG |
204 | $self->croak("subtest()'s second argument must be a code ref") |
205 | unless $subtests && 'CODE' eq Scalar::Util::reftype($subtests); | |
2c4d5b9b SH |
206 | |
207 | # Turn the child into the parent so anyone who has stored a copy of | |
208 | # the Test::Builder singleton will get the child. | |
6bdb8877 | 209 | my ($success, $error, $child); |
411e93ce | 210 | my $parent = {}; |
809046db | 211 | { |
6bdb8877 | 212 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
2c4d5b9b | 213 | |
411e93ce | 214 | # Store the guts of $self as $parent and turn $child into $self. |
6bdb8877 CG |
215 | $child = $self->child($name, 1); |
216 | ||
411e93ce SH |
217 | _copy($self, $parent); |
218 | _copy($child, $self); | |
809046db CBW |
219 | |
220 | my $run_the_subtests = sub { | |
6bdb8877 CG |
221 | $subtests->(@args); |
222 | $self->done_testing unless defined $self->stream->plan; | |
809046db CBW |
223 | 1; |
224 | }; | |
225 | ||
6bdb8877 | 226 | ($success, $error) = try { Test::Builder::Trace->nest($run_the_subtests) }; |
2c4d5b9b SH |
227 | } |
228 | ||
229 | # Restore the parent and the copied child. | |
411e93ce SH |
230 | _copy($self, $child); |
231 | _copy($parent, $self); | |
2c4d5b9b | 232 | |
809046db CBW |
233 | # Restore the parent's $TODO |
234 | $self->find_TODO(undef, 1, $child->{Parent_TODO}); | |
235 | ||
2c4d5b9b | 236 | # Die *after* we restore the parent. |
6bdb8877 | 237 | die $error if $error && !(Scalar::Util::blessed($error) && $error->isa('Test::Builder::Exception')); |
2c4d5b9b | 238 | |
6bdb8877 CG |
239 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
240 | my $finalize = $child->finalize(1); | |
411e93ce | 241 | |
6bdb8877 | 242 | $self->BAIL_OUT($child->{Bailed_Out_Reason}) if $child->_bailed_out; |
411e93ce SH |
243 | |
244 | return $finalize; | |
2c4d5b9b SH |
245 | } |
246 | ||
2c4d5b9b SH |
247 | sub finalize { |
248 | my $self = shift; | |
6bdb8877 | 249 | my ($is_subtest) = @_; |
2c4d5b9b SH |
250 | |
251 | return unless $self->parent; | |
252 | if( $self->{Child_Name} ) { | |
253 | $self->croak("Can't call finalize() with child ($self->{Child_Name}) active"); | |
254 | } | |
c8c13991 CBW |
255 | |
256 | local $? = 0; # don't fail if $subtests happened to set $? nonzero | |
2c4d5b9b SH |
257 | $self->_ending; |
258 | ||
6bdb8877 | 259 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
2c4d5b9b SH |
260 | my $ok = 1; |
261 | $self->parent->{Child_Name} = undef; | |
6bdb8877 CG |
262 | |
263 | unless ($self->_bailed_out) { | |
411e93ce SH |
264 | if ( $self->{Skip_All} ) { |
265 | $self->parent->skip($self->{Skip_All}); | |
266 | } | |
6bdb8877 | 267 | elsif ( ! $self->stream->tests_run ) { |
411e93ce SH |
268 | $self->parent->ok( 0, sprintf q[No tests run for subtest "%s"], $self->name ); |
269 | } | |
270 | else { | |
271 | $self->parent->ok( $self->is_passing, $self->name ); | |
272 | } | |
2c4d5b9b | 273 | } |
6bdb8877 | 274 | |
2c4d5b9b | 275 | $? = $self->{Child_Error}; |
6bdb8877 CG |
276 | my $parent = delete $self->{Parent}; |
277 | ||
278 | my $res = Test::Builder::Result::Child->new( | |
279 | $self->context, | |
280 | name => $self->{Name} || undef, | |
281 | action => 'pop', | |
282 | in_todo => $self->in_todo || 0, | |
283 | is_subtest => $is_subtest || 0, | |
284 | ); | |
285 | $parent->stream->send($res); | |
2c4d5b9b SH |
286 | |
287 | return $self->is_passing; | |
288 | } | |
289 | ||
6bdb8877 CG |
290 | ############################# |
291 | # }}} Children and subtests # | |
292 | ############################# | |
2c4d5b9b | 293 | |
6bdb8877 CG |
294 | ##################################### |
295 | # {{{ Finding Testers and Providers # | |
296 | ##################################### | |
2c4d5b9b | 297 | |
6bdb8877 CG |
298 | sub trace_test { |
299 | my $out; | |
300 | protect { $out = Test::Builder::Trace->new }; | |
301 | return $out; | |
2c4d5b9b SH |
302 | } |
303 | ||
6bdb8877 CG |
304 | sub find_TODO { |
305 | my( $self, $pack, $set, $new_value ) = @_; | |
2c4d5b9b | 306 | |
6bdb8877 CG |
307 | $pack ||= $self->trace_test->todo_package || $self->exported_to; |
308 | return unless $pack; | |
2c4d5b9b | 309 | |
6bdb8877 CG |
310 | no strict 'refs'; ## no critic |
311 | no warnings 'once'; | |
312 | my $old_value = ${ $pack . '::TODO' }; | |
313 | $set and ${ $pack . '::TODO' } = $new_value; | |
314 | return $old_value; | |
315 | } | |
2c4d5b9b | 316 | |
6bdb8877 CG |
317 | ##################################### |
318 | # }}} Finding Testers and Providers # | |
319 | ##################################### | |
2c4d5b9b | 320 | |
6bdb8877 CG |
321 | ################ |
322 | # {{{ Planning # | |
323 | ################ | |
2c4d5b9b | 324 | |
6bdb8877 CG |
325 | my %PLAN_CMDS = ( |
326 | no_plan => 'no_plan', | |
327 | skip_all => 'skip_all', | |
328 | tests => '_plan_tests', | |
329 | ); | |
2c4d5b9b | 330 | |
6bdb8877 CG |
331 | sub plan { |
332 | my( $self, $cmd, $arg ) = @_; | |
2c4d5b9b | 333 | |
6bdb8877 | 334 | return unless $cmd; |
2c4d5b9b | 335 | |
6bdb8877 | 336 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
2c4d5b9b | 337 | |
6bdb8877 CG |
338 | if( my $method = $PLAN_CMDS{$cmd} ) { |
339 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; | |
340 | $self->$method($arg); | |
341 | } | |
342 | else { | |
343 | my @args = grep { defined } ( $cmd, $arg ); | |
344 | $self->croak("plan() doesn't understand @args"); | |
2c4d5b9b | 345 | } |
6bdb8877 CG |
346 | |
347 | return 1; | |
2c4d5b9b SH |
348 | } |
349 | ||
6bdb8877 CG |
350 | sub skip_all { |
351 | my( $self, $reason ) = @_; | |
30e302f8 | 352 | |
6bdb8877 | 353 | $self->{Skip_All} = $self->parent ? $reason : 1; |
30e302f8 | 354 | |
6bdb8877 CG |
355 | die bless {} => 'Test::Builder::Exception' if $self->parent; |
356 | $self->_issue_plan(0, "SKIP", $reason); | |
357 | } | |
30e302f8 | 358 | |
6bdb8877 CG |
359 | sub no_plan { |
360 | my($self, $arg) = @_; | |
30e302f8 | 361 | |
6bdb8877 | 362 | $self->carp("no_plan takes no arguments") if $arg; |
30e302f8 | 363 | |
6bdb8877 | 364 | $self->_issue_plan(undef, "NO_PLAN"); |
30e302f8 | 365 | |
6bdb8877 CG |
366 | return 1; |
367 | } | |
5143c659 | 368 | |
6bdb8877 CG |
369 | sub _plan_tests { |
370 | my($self, $arg) = @_; | |
3e887aae | 371 | |
6bdb8877 CG |
372 | if($arg) { |
373 | $self->croak("Number of tests must be a positive integer. You gave it '$arg'") | |
374 | unless $arg =~ /^\+?\d+$/; | |
30e302f8 | 375 | |
6bdb8877 CG |
376 | $self->_issue_plan($arg); |
377 | } | |
378 | elsif( !defined $arg ) { | |
379 | $self->croak("Got an undefined number of tests"); | |
380 | } | |
381 | else { | |
382 | $self->croak("You said to run 0 tests"); | |
383 | } | |
30e302f8 | 384 | |
6bdb8877 CG |
385 | return; |
386 | } | |
30e302f8 | 387 | |
6bdb8877 CG |
388 | sub _issue_plan { |
389 | my($self, $max, $directive, $reason) = @_; | |
30e302f8 | 390 | |
6bdb8877 CG |
391 | if ($directive && $directive eq 'OVERRIDE') { |
392 | $directive = undef; | |
393 | } | |
394 | elsif ($self->stream->plan) { | |
395 | $self->croak("You tried to plan twice"); | |
396 | } | |
5143c659 | 397 | |
6bdb8877 CG |
398 | my $plan = Test::Builder::Result::Plan->new( |
399 | $self->context, | |
400 | directive => $directive || undef, | |
401 | reason => $reason || undef, | |
402 | in_todo => $self->in_todo || 0, | |
30e302f8 | 403 | |
6bdb8877 CG |
404 | max => defined($max) ? $max : undef, |
405 | ); | |
04955c14 | 406 | |
6bdb8877 | 407 | $self->stream->send($plan); |
30e302f8 | 408 | |
6bdb8877 | 409 | return $plan; |
30e302f8 NC |
410 | } |
411 | ||
6bdb8877 CG |
412 | sub done_testing { |
413 | my($self, $num_tests) = @_; | |
411e93ce | 414 | |
6bdb8877 CG |
415 | my $expected = $self->stream->expected_tests; |
416 | my $total = $self->stream->tests_run; | |
411e93ce | 417 | |
6bdb8877 CG |
418 | # If done_testing() specified the number of tests, shut off no_plan. |
419 | if(defined $num_tests && !defined $expected) { | |
420 | $self->_issue_plan($num_tests, 'OVERRIDE'); | |
421 | $expected = $num_tests; | |
422 | } | |
411e93ce | 423 | |
6bdb8877 CG |
424 | if( $self->{Done_Testing} ) { |
425 | my($file, $line) = @{$self->{Done_Testing}}[1,2]; | |
426 | my $ok = Test::Builder::Result::Ok->new( | |
427 | $self->context, | |
428 | real_bool => 0, | |
429 | name => "done_testing() was already called at $file line $line", | |
430 | bool => $self->in_todo ? 1 : 0, | |
431 | in_todo => $self->in_todo || 0, | |
432 | todo => $self->in_todo ? $self->todo() || "" : "", | |
433 | ); | |
434 | $self->stream->send($ok); | |
435 | $self->is_passing(0) unless $self->in_todo; | |
411e93ce | 436 | |
6bdb8877 CG |
437 | return; |
438 | } | |
411e93ce | 439 | |
6bdb8877 | 440 | $self->{Done_Testing} = [caller]; |
33459055 | 441 | |
6bdb8877 CG |
442 | if ($expected && defined($num_tests) && $num_tests != $expected) { |
443 | my $ok = Test::Builder::Result::Ok->new( | |
444 | $self->context, | |
445 | real_bool => 0, | |
446 | name => "planned to run $expected but done_testing() expects $num_tests", | |
447 | bool => $self->in_todo ? 1 : 0, | |
448 | in_todo => $self->in_todo || 0, | |
449 | todo => $self->in_todo ? $self->todo() || "" : "", | |
450 | ); | |
451 | $self->stream->send($ok); | |
452 | $self->is_passing(0) unless $self->in_todo; | |
453 | } | |
33459055 | 454 | |
33459055 | 455 | |
6bdb8877 | 456 | $self->_issue_plan($total) unless $expected; |
33459055 | 457 | |
6bdb8877 CG |
458 | # The wrong number of tests were run |
459 | $self->is_passing(0) if defined $expected && $expected != $total; | |
33459055 | 460 | |
6bdb8877 CG |
461 | # No tests were run |
462 | $self->is_passing(0) unless $total; | |
33459055 | 463 | |
6bdb8877 CG |
464 | return 1; |
465 | } | |
33459055 | 466 | |
6bdb8877 CG |
467 | ################ |
468 | # }}} Planning # | |
469 | ################ | |
33459055 | 470 | |
6bdb8877 CG |
471 | ############################# |
472 | # {{{ Base Result Producers # | |
473 | ############################# | |
2c4d5b9b | 474 | |
6bdb8877 CG |
475 | sub _ok_obj { |
476 | my $self = shift; | |
477 | my( $test, $name, @diag ) = @_; | |
2c4d5b9b | 478 | |
6bdb8877 CG |
479 | if ( $self->{Child_Name} and not $self->{In_Destroy} ) { |
480 | $name = 'unnamed test' unless defined $name; | |
481 | $self->is_passing(0); | |
482 | $self->croak("Cannot run test ($name) with active children"); | |
483 | } | |
33459055 | 484 | |
6bdb8877 CG |
485 | # $test might contain an object which we don't want to accidentally |
486 | # store, so we turn it into a boolean. | |
487 | $test = $test ? 1 : 0; | |
3e887aae | 488 | |
6bdb8877 CG |
489 | # In case $name is a string overloaded object, force it to stringify. |
490 | $self->_unoverload_str( \$name ); | |
33459055 | 491 | |
6bdb8877 CG |
492 | # Capture the value of $TODO for the rest of this ok() call |
493 | # so it can more easily be found by other routines. | |
494 | my $todo = $self->todo(); | |
495 | my $in_todo = $self->in_todo; | |
496 | local $self->{Todo} = $todo if $in_todo; | |
33459055 | 497 | |
6bdb8877 | 498 | $self->_unoverload_str( \$todo ); |
004caa16 | 499 | |
6bdb8877 CG |
500 | my $ok = Test::Builder::Result::Ok->new( |
501 | $self->context, | |
502 | real_bool => $test, | |
503 | bool => $self->in_todo ? 1 : $test, | |
504 | name => $name || $self->default_name || undef, | |
505 | in_todo => $self->in_todo || 0, | |
506 | diag => \@diag, | |
507 | ); | |
a344be10 | 508 | |
6bdb8877 CG |
509 | # # in a name can confuse Test::Harness. |
510 | $name =~ s|#|\\#|g if defined $name; | |
511 | ||
512 | if( $self->in_todo ) { | |
513 | $ok->todo($todo); | |
514 | $ok->in_todo(1); | |
33459055 | 515 | } |
6bdb8877 CG |
516 | |
517 | if (defined $name and $name =~ /^[\d\s]+$/) { | |
518 | $ok->diag(<<" ERR"); | |
519 | You named your test '$name'. You shouldn't use numbers for your test names. | |
520 | Very confusing. | |
521 | ERR | |
89c1e84a | 522 | } |
a344be10 | 523 | |
6bdb8877 | 524 | return $ok; |
33459055 MS |
525 | } |
526 | ||
6bdb8877 CG |
527 | sub ok { |
528 | my $self = shift; | |
529 | my( $test, $name, @diag ) = @_; | |
3e887aae | 530 | |
6bdb8877 CG |
531 | my $ok = $self->_ok_obj($test, $name, @diag); |
532 | $self->_record_ok($ok); | |
3e887aae | 533 | |
6bdb8877 | 534 | return $test ? 1 : 0; |
3e887aae DM |
535 | } |
536 | ||
6bdb8877 CG |
537 | sub _record_ok { |
538 | my $self = shift; | |
539 | my ($ok) = @_; | |
33459055 | 540 | |
6bdb8877 | 541 | $self->stream->send($ok); |
33459055 | 542 | |
6bdb8877 | 543 | $self->is_passing(0) unless $ok->real_bool || $self->in_todo; |
33459055 | 544 | |
6bdb8877 CG |
545 | # Check that we haven't violated the plan |
546 | $self->_check_is_passing_plan(); | |
547 | } | |
7483b81c | 548 | |
6bdb8877 CG |
549 | sub BAIL_OUT { |
550 | my( $self, $reason ) = @_; | |
33459055 | 551 | |
6bdb8877 | 552 | $self->_bailed_out(1); |
33459055 | 553 | |
6bdb8877 CG |
554 | if ($self->parent) { |
555 | $self->{Bailed_Out_Reason} = $reason; | |
556 | $self->no_ending(1); | |
557 | die bless {} => 'Test::Builder::Exception'; | |
33459055 | 558 | } |
6bdb8877 CG |
559 | |
560 | my $bail = Test::Builder::Result::Bail->new( | |
561 | $self->context, | |
562 | reason => $reason, | |
563 | in_todo => $self->in_todo || 0, | |
564 | ); | |
565 | $self->stream->send($bail); | |
33459055 MS |
566 | } |
567 | ||
6bdb8877 CG |
568 | sub skip { |
569 | my( $self, $why ) = @_; | |
570 | $why ||= ''; | |
571 | $self->_unoverload_str( \$why ); | |
33459055 | 572 | |
6bdb8877 CG |
573 | my $ok = Test::Builder::Result::Ok->new( |
574 | $self->context, | |
575 | real_bool => 1, | |
576 | bool => 1, | |
577 | in_todo => $self->in_todo || 0, | |
578 | skip => $why, | |
579 | ); | |
33459055 | 580 | |
6bdb8877 CG |
581 | $self->stream->send($ok); |
582 | } | |
33459055 | 583 | |
6bdb8877 CG |
584 | sub todo_skip { |
585 | my( $self, $why ) = @_; | |
586 | $why ||= ''; | |
33459055 | 587 | |
6bdb8877 CG |
588 | my $ok = Test::Builder::Result::Ok->new( |
589 | $self->context, | |
590 | real_bool => 0, | |
591 | bool => 1, | |
592 | in_todo => $self->in_todo || 0, | |
593 | skip => $why, | |
594 | todo => $why, | |
595 | ); | |
3e887aae | 596 | |
6bdb8877 CG |
597 | $self->stream->send($ok); |
598 | } | |
5143c659 | 599 | |
6bdb8877 CG |
600 | sub diag { |
601 | my $self = shift; | |
ccbd73a4 | 602 | |
6bdb8877 | 603 | my $msg = join '', map { defined($_) ? $_ : 'undef' } @_; |
33459055 | 604 | |
6bdb8877 CG |
605 | my $r = Test::Builder::Result::Diag->new( |
606 | $self->context, | |
607 | in_todo => $self->in_todo || 0, | |
608 | message => $msg, | |
609 | ); | |
610 | $self->stream->send($r); | |
611 | } | |
3e887aae | 612 | |
6bdb8877 CG |
613 | sub note { |
614 | my $self = shift; | |
3e887aae | 615 | |
6bdb8877 | 616 | my $msg = join '', map { defined($_) ? $_ : 'undef' } @_; |
3e887aae | 617 | |
6bdb8877 CG |
618 | my $r = Test::Builder::Result::Note->new( |
619 | $self->context, | |
620 | in_todo => $self->in_todo || 0, | |
621 | message => $msg, | |
622 | ); | |
623 | $self->stream->send($r); | |
624 | } | |
3e887aae | 625 | |
6bdb8877 CG |
626 | ############################# |
627 | # }}} Base Result Producers # | |
628 | ############################# | |
3e887aae | 629 | |
6bdb8877 CG |
630 | ################################# |
631 | # {{{ Advanced Result Producers # | |
632 | ################################# | |
3e887aae | 633 | |
6bdb8877 | 634 | my %numeric_cmps = map { ( $_, 1 ) } ( "<", "<=", ">", ">=", "==", "!=", "<=>" ); |
3e887aae | 635 | |
6bdb8877 CG |
636 | # Bad, these are not comparison operators. Should we include more? |
637 | my %cmp_ok_bl = map { ( $_, 1 ) } ( "=", "+=", ".=", "x=", "^=", "|=", "||=", "&&=", "..."); | |
3e887aae | 638 | |
6bdb8877 CG |
639 | sub cmp_ok { |
640 | my( $self, $got, $type, $expect, $name ) = @_; | |
3e887aae | 641 | |
6bdb8877 CG |
642 | if ($cmp_ok_bl{$type}) { |
643 | $self->croak("$type is not a valid comparison operator in cmp_ok()"); | |
644 | } | |
3e887aae | 645 | |
6bdb8877 CG |
646 | my $test; |
647 | my $error; | |
648 | my @diag; | |
3e887aae | 649 | |
6bdb8877 | 650 | my($pack, $file, $line) = $self->trace_test->report->call; |
3e887aae | 651 | |
6bdb8877 CG |
652 | (undef, $error) = try { |
653 | # This is so that warnings come out at the caller's level | |
654 | ## no critic (BuiltinFunctions::ProhibitStringyEval) | |
655 | eval qq[ | |
656 | #line $line "(eval in cmp_ok) $file" | |
657 | \$test = \$got $type \$expect; | |
658 | 1; | |
659 | ] || die $@; | |
660 | }; | |
3e887aae | 661 | |
6bdb8877 CG |
662 | # Treat overloaded objects as numbers if we're asked to do a |
663 | # numeric comparison. | |
664 | my $unoverload | |
665 | = $numeric_cmps{$type} | |
666 | ? '_unoverload_num' | |
667 | : '_unoverload_str'; | |
3e887aae | 668 | |
6bdb8877 CG |
669 | push @diag => <<"END" if $error; |
670 | An error occurred while using $type: | |
671 | ------------------------------------ | |
672 | $error | |
673 | ------------------------------------ | |
674 | END | |
3e887aae | 675 | |
6bdb8877 CG |
676 | unless($test) { |
677 | $self->$unoverload( \$got, \$expect ); | |
809046db | 678 | |
6bdb8877 CG |
679 | if( $type =~ /^(eq|==)$/ ) { |
680 | push @diag => $self->_is_diag( $got, $type, $expect ); | |
681 | } | |
682 | elsif( $type =~ /^(ne|!=)$/ ) { | |
683 | push @diag => $self->_isnt_diag( $got, $type ); | |
684 | } | |
685 | else { | |
686 | push @diag => $self->_cmp_diag( $got, $type, $expect ); | |
687 | } | |
688 | } | |
3e887aae | 689 | |
6bdb8877 CG |
690 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
691 | $self->ok($test, $name, @diag); | |
3e887aae | 692 | |
6bdb8877 CG |
693 | return $test ? 1 : 0; |
694 | } | |
3e887aae | 695 | |
3e887aae | 696 | |
6bdb8877 CG |
697 | sub is_eq { |
698 | my( $self, $got, $expect, $name ) = @_; | |
699 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; | |
3e887aae | 700 | |
6bdb8877 CG |
701 | if( !defined $got || !defined $expect ) { |
702 | # undef only matches undef and nothing else | |
703 | my $test = !defined $got && !defined $expect; | |
3e887aae | 704 | |
6bdb8877 CG |
705 | $self->ok($test, $name, $test ? () : $self->_is_diag( $got, 'eq', $expect )); |
706 | return $test; | |
707 | } | |
3e887aae | 708 | |
6bdb8877 CG |
709 | return $self->cmp_ok( $got, 'eq', $expect, $name ); |
710 | } | |
3e887aae | 711 | |
6bdb8877 CG |
712 | sub is_num { |
713 | my( $self, $got, $expect, $name ) = @_; | |
714 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; | |
3e887aae | 715 | |
6bdb8877 CG |
716 | if( !defined $got || !defined $expect ) { |
717 | # undef only matches undef and nothing else | |
718 | my $test = !defined $got && !defined $expect; | |
3e887aae | 719 | |
6bdb8877 CG |
720 | $self->ok($test, $name, $test ? () : $self->_is_diag( $got, '==', $expect )); |
721 | return $test; | |
3e887aae | 722 | } |
3e887aae | 723 | |
6bdb8877 CG |
724 | return $self->cmp_ok( $got, '==', $expect, $name ); |
725 | } | |
3e887aae | 726 | |
6bdb8877 CG |
727 | sub isnt_eq { |
728 | my( $self, $got, $dont_expect, $name ) = @_; | |
729 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; | |
3e887aae | 730 | |
6bdb8877 CG |
731 | if( !defined $got || !defined $dont_expect ) { |
732 | # undef only matches undef and nothing else | |
733 | my $test = defined $got || defined $dont_expect; | |
3e887aae | 734 | |
6bdb8877 CG |
735 | $self->ok( $test, $name, $test ? () : $self->_isnt_diag( $got, 'ne' )); |
736 | return $test; | |
3e887aae DM |
737 | } |
738 | ||
6bdb8877 CG |
739 | return $self->cmp_ok( $got, 'ne', $dont_expect, $name ); |
740 | } | |
3e887aae | 741 | |
6bdb8877 CG |
742 | sub isnt_num { |
743 | my( $self, $got, $dont_expect, $name ) = @_; | |
744 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; | |
3e887aae | 745 | |
6bdb8877 CG |
746 | if( !defined $got || !defined $dont_expect ) { |
747 | # undef only matches undef and nothing else | |
748 | my $test = defined $got || defined $dont_expect; | |
3e887aae | 749 | |
6bdb8877 CG |
750 | $self->ok( $test, $name, $test ? () : $self->_isnt_diag( $got, '!=' )); |
751 | return $test; | |
752 | } | |
3e887aae | 753 | |
6bdb8877 CG |
754 | return $self->cmp_ok( $got, '!=', $dont_expect, $name ); |
755 | } | |
2c4d5b9b | 756 | |
6bdb8877 CG |
757 | sub like { |
758 | my( $self, $thing, $regex, $name ) = @_; | |
759 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; | |
2c4d5b9b | 760 | |
6bdb8877 | 761 | return $self->_regex_ok( $thing, $regex, '=~', $name ); |
3e887aae DM |
762 | } |
763 | ||
6bdb8877 CG |
764 | sub unlike { |
765 | my( $self, $thing, $regex, $name ) = @_; | |
766 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; | |
3e887aae | 767 | |
6bdb8877 CG |
768 | return $self->_regex_ok( $thing, $regex, '!~', $name ); |
769 | } | |
60ffb308 | 770 | |
5143c659 | 771 | |
60ffb308 | 772 | |
6bdb8877 CG |
773 | ################################# |
774 | # }}} Advanced Result Producers # | |
775 | ################################# | |
60ffb308 | 776 | |
6bdb8877 CG |
777 | ####################### |
778 | # {{{ Public helpers # | |
779 | ####################### | |
5143c659 | 780 | |
6bdb8877 CG |
781 | sub explain { |
782 | my $self = shift; | |
60ffb308 | 783 | |
6bdb8877 CG |
784 | return map { |
785 | ref $_ | |
786 | ? do { | |
787 | $self->_try(sub { require Data::Dumper }, die_on_fail => 1); | |
33459055 | 788 | |
6bdb8877 CG |
789 | my $dumper = Data::Dumper->new( [$_] ); |
790 | $dumper->Indent(1)->Terse(1); | |
791 | $dumper->Sortkeys(1) if $dumper->can("Sortkeys"); | |
792 | $dumper->Dump; | |
793 | } | |
794 | : $_ | |
795 | } @_; | |
796 | } | |
33459055 | 797 | |
6bdb8877 CG |
798 | sub carp { |
799 | my $self = shift; | |
800 | return warn $self->_message_at_caller(@_); | |
801 | } | |
33459055 | 802 | |
6bdb8877 CG |
803 | sub croak { |
804 | my $self = shift; | |
805 | return die $self->_message_at_caller(@_); | |
806 | } | |
33459055 | 807 | |
6bdb8877 CG |
808 | sub context { |
809 | my $self = shift; | |
33459055 | 810 | |
6bdb8877 | 811 | my $trace = $self->trace_test; |
33459055 | 812 | |
6bdb8877 CG |
813 | return ( |
814 | depth => $self->depth, | |
815 | source => $self->name || "", | |
816 | trace => $trace, | |
817 | ); | |
33459055 MS |
818 | } |
819 | ||
6bdb8877 CG |
820 | sub has_plan { |
821 | my $self = shift; | |
04955c14 | 822 | |
6bdb8877 CG |
823 | return($self->stream->expected_tests) if $self->stream->expected_tests; |
824 | return('no_plan') if $self->stream->plan; | |
825 | return(undef); | |
826 | } | |
04955c14 | 827 | |
6bdb8877 CG |
828 | sub reset { ## no critic (Subroutines::ProhibitBuiltinHomonyms) |
829 | my $self = shift; | |
830 | my %params; | |
04955c14 | 831 | |
6bdb8877 CG |
832 | if (@_) { |
833 | %params = @_; | |
834 | $self->{reset_params} = \%params; | |
835 | } | |
836 | else { | |
837 | %params = %{$self->{reset_params} || {}}; | |
838 | } | |
04955c14 | 839 | |
6bdb8877 CG |
840 | my $modern = $params{modern} || $self->modern || 0; |
841 | $self->modern($modern); | |
04955c14 | 842 | |
6bdb8877 CG |
843 | # We leave this a global because it has to be localized and localizing |
844 | # hash keys is just asking for pain. Also, it was documented. | |
845 | $Level = 1; | |
846 | $BLevel = 1; | |
04955c14 | 847 | |
6bdb8877 CG |
848 | if ($params{new_stream} || !$params{shared_stream}) { |
849 | my $olds = $self->stream; | |
850 | $self->{stream} = Test::Builder::Stream->new; | |
851 | $self->{stream}->use_lresults if $olds->lresults; | |
04955c14 | 852 | } |
33459055 | 853 | |
6bdb8877 | 854 | $final->pid($$) if $final; |
33459055 | 855 | |
6bdb8877 | 856 | $self->stream->use_tap unless $params{no_tap} || $ENV{TB_NO_TAP}; |
c00d8759 | 857 | |
6bdb8877 | 858 | $self->stream->plan(undef) unless $params{no_reset_plan}; |
33459055 | 859 | |
6bdb8877 CG |
860 | # Don't reset stream stuff when reseting/creating a modern TB object |
861 | unless ($modern) { | |
862 | $self->stream->no_ending(0); | |
863 | $self->tap->reset if $self->tap; | |
864 | $self->lresults->reset if $self->lresults; | |
865 | } | |
33459055 | 866 | |
6bdb8877 | 867 | $self->{Name} = $0; |
33459055 | 868 | |
6bdb8877 CG |
869 | $self->{Have_Issued_Plan} = 0; |
870 | $self->{Done_Testing} = 0; | |
871 | $self->{Skip_All} = 0; | |
33459055 | 872 | |
6bdb8877 CG |
873 | $self->{Original_Pid} = $$; |
874 | $self->{Child_Name} = undef; | |
875 | $self->{Indent} ||= ''; | |
876 | $self->{Depth} = 0; | |
33459055 | 877 | |
6bdb8877 CG |
878 | $self->{Exported_To} = undef; |
879 | $self->{Expected_Tests} = 0; | |
33459055 | 880 | |
6bdb8877 CG |
881 | $self->{Todo} = undef; |
882 | $self->{Todo_Stack} = []; | |
883 | $self->{Start_Todo} = 0; | |
884 | $self->{Opened_Testhandles} = 0; | |
33459055 | 885 | |
6bdb8877 CG |
886 | return; |
887 | } | |
60ffb308 | 888 | |
a344be10 | 889 | |
6bdb8877 CG |
890 | ####################### |
891 | # }}} Public helpers # | |
892 | ####################### | |
30e302f8 | 893 | |
6bdb8877 CG |
894 | #################### |
895 | # {{{ TODO related # | |
896 | #################### | |
33459055 | 897 | |
6bdb8877 CG |
898 | sub todo { |
899 | my( $self, $pack ) = @_; | |
04955c14 | 900 | |
6bdb8877 | 901 | return $self->{Todo} if defined $self->{Todo}; |
33459055 | 902 | |
6bdb8877 CG |
903 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
904 | my $todo = $self->find_TODO($pack); | |
905 | return $todo if defined $todo; | |
60ffb308 | 906 | |
6bdb8877 CG |
907 | return ''; |
908 | } | |
33459055 | 909 | |
6bdb8877 CG |
910 | sub in_todo { |
911 | my $self = shift; | |
33459055 | 912 | |
6bdb8877 CG |
913 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
914 | return( defined $self->{Todo} || $self->find_TODO ) ? 1 : 0; | |
915 | } | |
916 | ||
917 | sub todo_start { | |
918 | my $self = shift; | |
919 | my $message = @_ ? shift : ''; | |
33459055 | 920 | |
6bdb8877 | 921 | $self->{Start_Todo}++; |
ccbd73a4 | 922 | if( $self->in_todo ) { |
6bdb8877 | 923 | push @{ $self->{Todo_Stack} } => $self->todo; |
33459055 | 924 | } |
6bdb8877 | 925 | $self->{Todo} = $message; |
33459055 | 926 | |
6bdb8877 CG |
927 | return; |
928 | } | |
33459055 | 929 | |
6bdb8877 CG |
930 | sub todo_end { |
931 | my $self = shift; | |
b1ddf169 | 932 | |
6bdb8877 CG |
933 | if( !$self->{Start_Todo} ) { |
934 | $self->croak('todo_end() called without todo_start()'); | |
ccbd73a4 | 935 | } |
33459055 | 936 | |
6bdb8877 | 937 | $self->{Start_Todo}--; |
2c4d5b9b | 938 | |
6bdb8877 CG |
939 | if( $self->{Start_Todo} && @{ $self->{Todo_Stack} } ) { |
940 | $self->{Todo} = pop @{ $self->{Todo_Stack} }; | |
941 | } | |
942 | else { | |
943 | delete $self->{Todo}; | |
944 | } | |
2c4d5b9b | 945 | |
6bdb8877 | 946 | return; |
33459055 MS |
947 | } |
948 | ||
6bdb8877 CG |
949 | #################### |
950 | # }}} TODO related # | |
951 | #################### | |
952 | ||
953 | ####################### | |
954 | # {{{ Private helpers # | |
955 | ####################### | |
2c4d5b9b SH |
956 | |
957 | # Check that we haven't yet violated the plan and set | |
958 | # is_passing() accordingly | |
959 | sub _check_is_passing_plan { | |
960 | my $self = shift; | |
961 | ||
6bdb8877 | 962 | my $plan = $self->stream->expected_tests; |
2c4d5b9b SH |
963 | return unless defined $plan; # no plan yet defined |
964 | return unless $plan !~ /\D/; # no numeric plan | |
6bdb8877 | 965 | $self->is_passing(0) if $plan < $self->stream->tests_run; |
2c4d5b9b SH |
966 | } |
967 | ||
6bdb8877 CG |
968 | sub _is_object { |
969 | my( $self, $thing ) = @_; | |
970 | ||
971 | return $self->_try( sub { ref $thing && $thing->isa('UNIVERSAL') } ) ? 1 : 0; | |
972 | } | |
2c4d5b9b | 973 | |
7483b81c | 974 | sub _unoverload { |
ccbd73a4 SP |
975 | my $self = shift; |
976 | my $type = shift; | |
7483b81c | 977 | |
8f70d4fd | 978 | $self->_try(sub { require overload; }, die_on_fail => 1); |
7483b81c RGS |
979 | |
980 | foreach my $thing (@_) { | |
c00d8759 | 981 | if( $self->_is_object($$thing) ) { |
ccbd73a4 | 982 | if( my $string_meth = overload::Method( $$thing, $type ) ) { |
c00d8759 | 983 | $$thing = $$thing->$string_meth(); |
7483b81c | 984 | } |
c00d8759 | 985 | } |
7483b81c | 986 | } |
7483b81c | 987 | |
ccbd73a4 SP |
988 | return; |
989 | } | |
7483b81c | 990 | |
b1ddf169 RGS |
991 | sub _unoverload_str { |
992 | my $self = shift; | |
993 | ||
ccbd73a4 SP |
994 | return $self->_unoverload( q[""], @_ ); |
995 | } | |
b1ddf169 RGS |
996 | |
997 | sub _unoverload_num { | |
998 | my $self = shift; | |
999 | ||
ccbd73a4 | 1000 | $self->_unoverload( '0+', @_ ); |
b1ddf169 RGS |
1001 | |
1002 | for my $val (@_) { | |
1003 | next unless $self->_is_dualvar($$val); | |
ccbd73a4 | 1004 | $$val = $$val + 0; |
b1ddf169 | 1005 | } |
b1ddf169 | 1006 | |
ccbd73a4 SP |
1007 | return; |
1008 | } | |
b1ddf169 RGS |
1009 | |
1010 | # This is a hack to detect a dualvar such as $! | |
1011 | sub _is_dualvar { | |
ccbd73a4 | 1012 | my( $self, $val ) = @_; |
b1ddf169 | 1013 | |
82d700dc SH |
1014 | # Objects are not dualvars. |
1015 | return 0 if ref $val; | |
1016 | ||
ccbd73a4 SP |
1017 | no warnings 'numeric'; |
1018 | my $numval = $val + 0; | |
13c65ef8 | 1019 | return ($numval != 0 and $numval ne $val ? 1 : 0); |
b1ddf169 RGS |
1020 | } |
1021 | ||
ccbd73a4 SP |
1022 | sub _diag_fmt { |
1023 | my( $self, $type, $val ) = @_; | |
a9153838 | 1024 | |
ccbd73a4 SP |
1025 | if( defined $$val ) { |
1026 | if( $type eq 'eq' or $type eq 'ne' ) { | |
1027 | # quote and force string context | |
1028 | $$val = "'$$val'"; | |
a9153838 MS |
1029 | } |
1030 | else { | |
ccbd73a4 SP |
1031 | # force numeric context |
1032 | $self->_unoverload_num($val); | |
a9153838 MS |
1033 | } |
1034 | } | |
ccbd73a4 SP |
1035 | else { |
1036 | $$val = 'undef'; | |
1037 | } | |
1038 | ||
1039 | return; | |
1040 | } | |
1041 | ||
1042 | sub _is_diag { | |
1043 | my( $self, $got, $type, $expect ) = @_; | |
6bdb8877 | 1044 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
ccbd73a4 SP |
1045 | |
1046 | $self->_diag_fmt( $type, $_ ) for \$got, \$expect; | |
33459055 | 1047 | |
6bdb8877 | 1048 | return <<"DIAGNOSTIC"; |
ccbd73a4 SP |
1049 | got: $got |
1050 | expected: $expect | |
a9153838 | 1051 | DIAGNOSTIC |
ccbd73a4 SP |
1052 | } |
1053 | ||
1054 | sub _isnt_diag { | |
1055 | my( $self, $got, $type ) = @_; | |
6bdb8877 | 1056 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
ccbd73a4 SP |
1057 | |
1058 | $self->_diag_fmt( $type, \$got ); | |
1059 | ||
6bdb8877 | 1060 | return <<"DIAGNOSTIC"; |
ccbd73a4 SP |
1061 | got: $got |
1062 | expected: anything else | |
1063 | DIAGNOSTIC | |
1064 | } | |
a9153838 | 1065 | |
a9153838 MS |
1066 | |
1067 | sub _cmp_diag { | |
ccbd73a4 SP |
1068 | my( $self, $got, $type, $expect ) = @_; |
1069 | ||
a9153838 MS |
1070 | $got = defined $got ? "'$got'" : 'undef'; |
1071 | $expect = defined $expect ? "'$expect'" : 'undef'; | |
ccbd73a4 | 1072 | |
6bdb8877 CG |
1073 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
1074 | return <<"DIAGNOSTIC"; | |
ccbd73a4 SP |
1075 | $got |
1076 | $type | |
1077 | $expect | |
a9153838 MS |
1078 | DIAGNOSTIC |
1079 | } | |
1080 | ||
b1ddf169 RGS |
1081 | sub _caller_context { |
1082 | my $self = shift; | |
1083 | ||
6bdb8877 | 1084 | my($pack, $file, $line) = $self->trace_test->report->call; |
b1ddf169 RGS |
1085 | |
1086 | my $code = ''; | |
1087 | $code .= "#line $line $file\n" if defined $file and defined $line; | |
1088 | ||
1089 | return $code; | |
1090 | } | |
1091 | ||
6bdb8877 CG |
1092 | sub _regex_ok { |
1093 | my( $self, $thing, $regex, $cmp, $name ) = @_; | |
c00d8759 | 1094 | |
6bdb8877 CG |
1095 | my $ok = 0; |
1096 | my $usable_regex = _is_qr($regex) ? $regex : $self->maybe_regex($regex); | |
1097 | unless( defined $usable_regex ) { | |
1098 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; | |
1099 | $ok = $self->ok( 0, $name, " '$regex' doesn't look much like a regex to me."); | |
1100 | return $ok; | |
1101 | } | |
c00d8759 | 1102 | |
6bdb8877 CG |
1103 | my $test; |
1104 | my $context = $self->_caller_context; | |
c00d8759 | 1105 | |
6bdb8877 CG |
1106 | try { |
1107 | # No point in issuing an uninit warning, they'll see it in the diagnostics | |
1108 | no warnings 'uninitialized'; | |
1109 | ## no critic (BuiltinFunctions::ProhibitStringyEval) | |
1110 | $test = eval $context . q{$test = $thing =~ /$usable_regex/ ? 1 : 0}; | |
1111 | }; | |
c00d8759 | 1112 | |
6bdb8877 | 1113 | $test = !$test if $cmp eq '!~'; |
b1ddf169 | 1114 | |
6bdb8877 CG |
1115 | my @diag; |
1116 | unless($test) { | |
1117 | $thing = defined $thing ? "'$thing'" : 'undef'; | |
1118 | my $match = $cmp eq '=~' ? "doesn't match" : "matches"; | |
b1ddf169 | 1119 | |
6bdb8877 CG |
1120 | push @diag => sprintf( <<'DIAGNOSTIC', $thing, $match, $regex ); |
1121 | %s | |
1122 | %13s '%s' | |
1123 | DIAGNOSTIC | |
1124 | } | |
a9153838 | 1125 | |
6bdb8877 CG |
1126 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
1127 | $self->ok( $test, $name, @diag ); | |
a9153838 | 1128 | |
6bdb8877 CG |
1129 | return $test; |
1130 | } | |
a9153838 | 1131 | |
6bdb8877 CG |
1132 | # I'm not ready to publish this. It doesn't deal with array return |
1133 | # values from the code or context. | |
1134 | sub _try { | |
1135 | my( $self, $code, %opts ) = @_; | |
a9153838 | 1136 | |
6bdb8877 CG |
1137 | my $result; |
1138 | my ($ok, $error) = try { $result = $code->() }; | |
1139 | ||
1140 | die $error if $opts{die_on_fail} && !$ok; | |
1141 | ||
1142 | return wantarray ? ( $result, $error ) : $result; | |
1143 | } | |
1144 | ||
1145 | sub _message_at_caller { | |
1146 | my $self = shift; | |
1147 | ||
1148 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; | |
1149 | my $trace = $self->trace_test; | |
1150 | my( $pack, $file, $line ) = $trace->report->call; | |
1151 | return join( "", @_ ) . " at $file line $line.\n"; | |
1152 | } | |
1153 | ||
1154 | #'# | |
1155 | sub _sanity_check { | |
1156 | my $self = shift; | |
1157 | ||
1158 | $self->_whoa( $self->stream->tests_run < 0, 'Says here you ran a negative number of tests!' ); | |
1159 | ||
1160 | $self->lresults->sanity_check($self) if $self->lresults; | |
a9153838 | 1161 | |
6bdb8877 CG |
1162 | return; |
1163 | } | |
411e93ce | 1164 | |
6bdb8877 CG |
1165 | sub _whoa { |
1166 | my( $self, $check, $desc ) = @_; | |
1167 | if($check) { | |
1168 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; | |
1169 | $self->croak(<<"WHOA"); | |
1170 | WHOA! $desc | |
1171 | This should never happen! Please contact the author immediately! | |
1172 | WHOA | |
411e93ce SH |
1173 | } |
1174 | ||
6bdb8877 | 1175 | return; |
a9153838 MS |
1176 | } |
1177 | ||
6bdb8877 CG |
1178 | sub _ending { |
1179 | my $self = shift; | |
1180 | require Test::Builder::ExitMagic; | |
1181 | my $ending = Test::Builder::ExitMagic->new(tb => $self, stream => $self->stream); | |
1182 | $ending->do_magic; | |
1183 | } | |
b1ddf169 | 1184 | |
6bdb8877 CG |
1185 | sub _is_qr { |
1186 | my $regex = shift; | |
845d7e37 | 1187 | |
6bdb8877 CG |
1188 | # is_regexp() checks for regexes in a robust manner, say if they're |
1189 | # blessed. | |
1190 | return re::is_regexp($regex) if defined &re::is_regexp; | |
1191 | return ref $regex eq 'Regexp'; | |
2c4d5b9b | 1192 | } |
b1ddf169 | 1193 | |
6bdb8877 CG |
1194 | ####################### |
1195 | # }}} Private helpers # | |
1196 | ####################### | |
33459055 | 1197 | |
6bdb8877 CG |
1198 | ################################################ |
1199 | # {{{ Everything below this line is deprecated # | |
1200 | # But it must be maintained for legacy... # | |
1201 | ################################################ | |
33459055 | 1202 | |
6bdb8877 CG |
1203 | BEGIN { |
1204 | my %generate = ( | |
1205 | lresults => [qw/summary details/], | |
1206 | stream => [qw/no_ending/], | |
1207 | tap => [qw/ | |
1208 | no_header no_diag output failure_output todo_output reset_outputs | |
1209 | use_numbers _new_fh | |
1210 | /], | |
1211 | ); | |
33459055 | 1212 | |
6bdb8877 CG |
1213 | for my $delegate (keys %generate) { |
1214 | for my $method (@{$generate{$delegate}}) { | |
1215 | #print STDERR "Adding: $method ($delegate)\n"; | |
1216 | my $code = sub { | |
1217 | my $self = shift; | |
33459055 | 1218 | |
6bdb8877 CG |
1219 | $self->carp("Use of \$TB->$method() is deprecated.") if $self->modern; |
1220 | my $d = $self->$delegate || $self->croak("$method() method only applies when $delegate is in use"); | |
33459055 | 1221 | |
6bdb8877 CG |
1222 | $d->$method(@_); |
1223 | }; | |
33459055 | 1224 | |
6bdb8877 CG |
1225 | no strict 'refs'; ## no critic |
1226 | *{$method} = $code; | |
ccbd73a4 | 1227 | } |
6bdb8877 CG |
1228 | } |
1229 | } | |
33459055 | 1230 | |
6bdb8877 CG |
1231 | sub exported_to { |
1232 | my($self, $pack) = @_; | |
1233 | $self->carp("exported_to() is deprecated") if $self->modern; | |
1234 | $self->{Exported_To} = $pack if defined $pack; | |
1235 | return $self->{Exported_To}; | |
33459055 MS |
1236 | } |
1237 | ||
6bdb8877 CG |
1238 | sub _indent { |
1239 | my $self = shift; | |
1240 | $self->carp("_indent() is deprecated") if $self->modern; | |
1241 | return '' unless $self->depth; | |
1242 | return ' ' x $self->depth | |
1243 | } | |
a9153838 | 1244 | |
6bdb8877 CG |
1245 | sub _output_plan { |
1246 | my ($self) = @_; | |
1247 | $self->carp("_output_plan() is deprecated") if $self->modern; | |
1248 | goto &_issue_plan; | |
1249 | } | |
a9153838 | 1250 | |
6bdb8877 CG |
1251 | sub _diag_fh { |
1252 | my $self = shift; | |
a9153838 | 1253 | |
6bdb8877 CG |
1254 | $self->carp("Use of \$TB->_diag_fh() is deprecated.") if $self->modern; |
1255 | my $tap = $self->tap || $self->croak("_diag_fh() method only applies when TAP is in use"); | |
a9153838 | 1256 | |
6bdb8877 CG |
1257 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
1258 | return $tap->_diag_fh($self->in_todo) | |
1259 | } | |
a9153838 | 1260 | |
6bdb8877 CG |
1261 | sub _print { |
1262 | my $self = shift; | |
a9153838 | 1263 | |
6bdb8877 CG |
1264 | $self->carp("Use of \$TB->_print() is deprecated.") if $self->modern; |
1265 | my $tap = $self->tap || $self->croak("_print() method only applies when TAP is in use"); | |
a9153838 | 1266 | |
6bdb8877 CG |
1267 | return $tap->_print($self->_indent, @_); |
1268 | } | |
a9153838 | 1269 | |
6bdb8877 CG |
1270 | sub _print_to_fh { |
1271 | my( $self, $fh, @msgs ) = @_; | |
a9153838 | 1272 | |
6bdb8877 CG |
1273 | $self->carp("Use of \$TB->_print_to_fh() is deprecated.") if $self->modern; |
1274 | my $tap = $self->tap || $self->croak("_print_to_fh() method only applies when TAP is in use"); | |
a9153838 | 1275 | |
6bdb8877 | 1276 | return $tap->_print_to_fh($fh, $self->_indent, @msgs); |
a9153838 MS |
1277 | } |
1278 | ||
6bdb8877 CG |
1279 | sub is_fh { |
1280 | my $self = shift; | |
33459055 | 1281 | |
6bdb8877 CG |
1282 | $self->carp("Use of \$TB->is_fh() is deprecated.") |
1283 | if Scalar::Util::blessed($self) && $self->modern; | |
33459055 | 1284 | |
6bdb8877 CG |
1285 | require Test::Builder::Formatter::TAP; |
1286 | return Test::Builder::Formatter::TAP->is_fh(@_); | |
1287 | } | |
33459055 | 1288 | |
6bdb8877 CG |
1289 | sub current_test { |
1290 | my $self = shift; | |
33459055 | 1291 | |
6bdb8877 CG |
1292 | my $tap = $self->tap; |
1293 | my $lresults = $self->lresults; | |
33459055 | 1294 | |
6bdb8877 CG |
1295 | if (@_) { |
1296 | my ($num) = @_; | |
33459055 | 1297 | |
6bdb8877 CG |
1298 | $lresults->current_test($num) if $lresults; |
1299 | $tap->current_test($num) if $tap; | |
c00d8759 | 1300 | |
6bdb8877 CG |
1301 | $self->stream->tests_run(0 - $self->stream->tests_run + $num); |
1302 | } | |
c00d8759 | 1303 | |
6bdb8877 CG |
1304 | return $self->stream->tests_run; |
1305 | } | |
c00d8759 | 1306 | |
6bdb8877 CG |
1307 | sub BAILOUT { |
1308 | my ($self) = @_; | |
1309 | $self->carp("Use of \$TB->BAILOUT() is deprecated.") if $self->modern; | |
1310 | goto &BAIL_OUT; | |
1311 | } | |
c00d8759 | 1312 | |
6bdb8877 CG |
1313 | sub expected_tests { |
1314 | my $self = shift; | |
c00d8759 | 1315 | |
6bdb8877 CG |
1316 | if(@_) { |
1317 | my ($max) = @_; | |
1318 | $self->carp("Use of \$TB->expected_tests(\$max) is deprecated.") if $self->modern; | |
1319 | $self->_issue_plan($max); | |
1320 | } | |
2c4d5b9b | 1321 | |
6bdb8877 CG |
1322 | return $self->stream->expected_tests || 0; |
1323 | } | |
c00d8759 | 1324 | |
6bdb8877 CG |
1325 | sub caller { ## no critic (Subroutines::ProhibitBuiltinHomonyms) |
1326 | my $self = shift; | |
c00d8759 | 1327 | |
6bdb8877 | 1328 | Carp::confess("Use of Test::Builder->caller() is deprecated.\n") if $self->modern; |
c00d8759 | 1329 | |
6bdb8877 CG |
1330 | local $Level = $Level + 1; local $BLevel = $BLevel + 1; |
1331 | my $trace = $self->trace_test; | |
1332 | return unless $trace && $trace->report; | |
1333 | my @call = $trace->report->call; | |
c00d8759 | 1334 | |
6bdb8877 CG |
1335 | return wantarray ? @call : $call[0]; |
1336 | } | |
c00d8759 | 1337 | |
6bdb8877 CG |
1338 | sub level { |
1339 | my( $self, $level ) = @_; | |
1340 | $Level = $level if defined $level; | |
1341 | return $Level; | |
1342 | } | |
c00d8759 | 1343 | |
c00d8759 | 1344 | sub maybe_regex { |
6bdb8877 | 1345 | my ($self, $regex) = @_; |
c00d8759 SP |
1346 | my $usable_regex = undef; |
1347 | ||
6bdb8877 CG |
1348 | $self->carp("Use of \$TB->maybe_regex() is deprecated.") if $self->modern; |
1349 | ||
c00d8759 SP |
1350 | return $usable_regex unless defined $regex; |
1351 | ||
ccbd73a4 | 1352 | my( $re, $opts ); |
c00d8759 SP |
1353 | |
1354 | # Check for qr/foo/ | |
bdff39c7 | 1355 | if( _is_qr($regex) ) { |
c00d8759 SP |
1356 | $usable_regex = $regex; |
1357 | } | |
1358 | # Check for '/foo/' or 'm,foo,' | |
ccbd73a4 SP |
1359 | elsif(( $re, $opts ) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or |
1360 | ( undef, $re, $opts ) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx | |
1361 | ) | |
c00d8759 SP |
1362 | { |
1363 | $usable_regex = length $opts ? "(?$opts)$re" : $re; | |
1364 | } | |
1365 | ||
1366 | return $usable_regex; | |
04955c14 SP |
1367 | } |
1368 | ||
ccbd73a4 | 1369 | |
6bdb8877 CG |
1370 | ################################### |
1371 | # }}} End of deprecations section # | |
1372 | ################################### | |
1373 | ||
1374 | #################### | |
1375 | # {{{ TB1.5 stuff # | |
1376 | #################### | |
1377 | ||
1378 | # This is just a list of method Test::Builder current does not have that Test::Builder 1.5 does. | |
1379 | my %TB15_METHODS = map {$_ => 1} qw{ | |
1380 | _file_and_line _join_message _make_default _my_exit _reset_todo_state | |
1381 | _result_to_hash _results _todo_state formatter history in_subtest in_test | |
1382 | no_change_exit_code post_event post_result set_formatter set_plan test_end | |
1383 | test_exit_code test_start test_state | |
1384 | }; | |
1385 | ||
1386 | our $AUTOLOAD; | |
1387 | sub AUTOLOAD { | |
1388 | $AUTOLOAD =~ m/^(.*)::([^:]+)$/; | |
1389 | my ($package, $sub) = ($1, $2); | |
1390 | ||
1391 | my @caller = CORE::caller(); | |
1392 | my $msg = qq{Can't locate object method "$sub" via package "$package" at $caller[1] line $caller[2]\n}; | |
1393 | ||
1394 | $msg .= <<" EOT" if $TB15_METHODS{$sub}; | |
1395 | ||
1396 | ************************************************************************* | |
1397 | '$sub' is a Test::Builder 1.5 method. Test::Builder 1.5 is a dead branch. | |
1398 | You need to update your code so that it no longer treats Test::Builders | |
1399 | over a specific version number as anything special. | |
1400 | ||
1401 | See: http://blogs.perl.org/users/chad_exodist_granum/2014/03/testmore---new-maintainer-also-stop-version-checking.html | |
1402 | ************************************************************************* | |
1403 | EOT | |
1404 | ||
1405 | die $msg; | |
04955c14 SP |
1406 | } |
1407 | ||
6bdb8877 CG |
1408 | #################### |
1409 | # }}} TB1.5 stuff # | |
1410 | #################### | |
c00d8759 | 1411 | |
6bdb8877 | 1412 | 1; |
c00d8759 | 1413 | |
6bdb8877 | 1414 | __END__ |
c00d8759 | 1415 | |
6bdb8877 | 1416 | =head1 NAME |
c00d8759 | 1417 | |
6bdb8877 | 1418 | Test::Builder - Backend for building test libraries |
411e93ce | 1419 | |
6bdb8877 | 1420 | =head1 NOTE ON DEPRECATIONS |
411e93ce | 1421 | |
6bdb8877 CG |
1422 | With version 1.301001 many old methods and practices have been deprecated. What |
1423 | we mean when we say "deprecated" is that the practices or methods are not to be | |
1424 | used in any new code. Old code that uses them will still continue to work, | |
1425 | possibly forever, but new code should use the newer and better alternatives. | |
c00d8759 | 1426 | |
6bdb8877 CG |
1427 | In the future, if enough (read: pretty much everything) is updated and few if |
1428 | any modules still use these old items, they will be removed completely. This is | |
1429 | not super likely to happen just because of the sheer number of modules that use | |
1430 | Test::Builder. | |
c00d8759 | 1431 | |
6bdb8877 | 1432 | =head1 SYNOPSIS |
c00d8759 | 1433 | |
6bdb8877 CG |
1434 | In general you probably do not want to use this module directly, but instead |
1435 | want to use L<Test::Builder::Provider> which will help you roll out a testing | |
1436 | library. | |
04955c14 | 1437 | |
6bdb8877 CG |
1438 | package My::Test::Module; |
1439 | use Test::Builder::Provider; | |
c00d8759 | 1440 | |
6bdb8877 CG |
1441 | # Export a test tool from an anonymous sub |
1442 | provide ok => sub { | |
1443 | my ($test, $name) = @_; | |
1444 | builder()->ok($test, $name); | |
1445 | }; | |
c00d8759 | 1446 | |
6bdb8877 CG |
1447 | # Export tools that are package subs |
1448 | provides qw/is is_deeply/; | |
1449 | sub is { ... } | |
1450 | sub is_deeply { ... } | |
c00d8759 | 1451 | |
6bdb8877 CG |
1452 | See L<Test::Builder::Provider> for more details. |
1453 | ||
1454 | B<Note:> You MUST use 'provide', or 'provides' to export testing tools, this | |
1455 | allows you to use the C<< builder()->trace_test >> tools to determine what | |
1456 | file/line a failed test came from. | |
1457 | ||
1458 | =head2 LOW-LEVEL | |
eb820256 | 1459 | |
6bdb8877 CG |
1460 | use Test::Builder; |
1461 | my $tb = Test::Builder->create(modern => 1, shared_stream => 1); | |
1462 | $tb->ok(1); | |
1463 | .... | |
c00d8759 | 1464 | |
6bdb8877 | 1465 | =head2 DEPRECATED |
c00d8759 | 1466 | |
6bdb8877 CG |
1467 | use Test::Builder; |
1468 | my $tb = Test::Builder->new; | |
1469 | $tb->ok(1); | |
1470 | ... | |
c00d8759 | 1471 | |
6bdb8877 | 1472 | =head1 DESCRIPTION |
c00d8759 | 1473 | |
6bdb8877 CG |
1474 | L<Test::Simple> and L<Test::More> have proven to be popular testing modules, |
1475 | but they're not always flexible enough. Test::Builder provides a | |
1476 | building block upon which to write your own test libraries I<which can | |
1477 | work together>. | |
c00d8759 | 1478 | |
6bdb8877 | 1479 | =head1 TEST COMPONENT MAP |
c00d8759 | 1480 | |
6bdb8877 CG |
1481 | [Test Script] > [Test Tool] > [Test::Builder] > [Test::Bulder::Stream] > [Result Formatter] |
1482 | ^ | |
1483 | You are here | |
c00d8759 | 1484 | |
6bdb8877 CG |
1485 | A test script uses a test tool such as L<Test::More>, which uses Test::Builder |
1486 | to produce results. The results are sent to L<Test::Builder::Stream> which then | |
1487 | forwards them on to one or more formatters. The default formatter is | |
1488 | L<Test::Builder::Fromatter::TAP> which produces TAP output. | |
ccbd73a4 | 1489 | |
6bdb8877 | 1490 | =head1 METHODS |
82d700dc | 1491 | |
6bdb8877 | 1492 | =head2 CONSTRUCTION |
ccbd73a4 | 1493 | |
6bdb8877 | 1494 | =over 4 |
c00d8759 | 1495 | |
6bdb8877 | 1496 | =item $Test = Test::Builder->create(%params) |
c00d8759 | 1497 | |
6bdb8877 | 1498 | Create a completely independant Test::Builder object. |
c00d8759 | 1499 | |
6bdb8877 | 1500 | my $Test = Test::Builder->create; |
c00d8759 | 1501 | |
6bdb8877 CG |
1502 | Create a Test::Builder object that sends results to the shared output stream |
1503 | (usually what you want). | |
c00d8759 | 1504 | |
6bdb8877 | 1505 | my $Test = Test::Builder->create(shared_stream => 1); |
c00d8759 | 1506 | |
6bdb8877 | 1507 | Create a Test::Builder object that does not include any legacy cruft. |
c00d8759 | 1508 | |
6bdb8877 | 1509 | my $Test = Test::Builder->create(modern => 1); |
c00d8759 | 1510 | |
6bdb8877 | 1511 | =item $Test = Test::Builder->new B<***DEPRECATED***> |
c00d8759 | 1512 | |
6bdb8877 CG |
1513 | my $Test = Test::Builder->new; |
1514 | ||
1515 | B<This usage is DEPRECATED!> | |
1516 | ||
1517 | Returns the Test::Builder singleton object representing the current state of | |
1518 | the test. | |
1519 | ||
1520 | Since you only run one test per program C<new> always returns the same | |
1521 | Test::Builder object. No matter how many times you call C<new()>, you're | |
1522 | getting the same object. This is called a singleton. This is done so that | |
1523 | multiple modules share such global information as the test counter and | |
1524 | where test output is going. B<No longer necessary> | |
1525 | ||
1526 | If you want a completely new Test::Builder object different from the | |
1527 | singleton, use C<create>. | |
1528 | ||
1529 | =back | |
1530 | ||
1531 | =head2 SIMPLE ACCESSORS AND SHORTCUTS | |
1532 | ||
1533 | =head3 READ/WRITE ATTRIBUTES | |
1534 | ||
1535 | =over 4 | |
1536 | ||
1537 | =item $parent = $Test->parent | |
1538 | ||
1539 | Returns the parent C<Test::Builder> instance, if any. Only used with child | |
1540 | builders for nested TAP. | |
1541 | ||
1542 | =item $Test->name | |
1543 | ||
1544 | Defaults to $0, but subtests and child tests will set this. | |
1545 | ||
1546 | =item $Test->modern | |
1547 | ||
1548 | Defaults to $ENV{TB_MODERN}, or 0. True when the builder object was constructed | |
1549 | with modern practices instead of deprecated ones. | |
1550 | ||
1551 | =item $Test->depth | |
1552 | ||
1553 | Get/Set the depth. This is usually set for Child tests. | |
1554 | ||
1555 | =item $Test->default_name | |
1556 | ||
1557 | Get/Set the default name for tests where no name was provided. Typically this | |
1558 | should be set to undef, there are very few real-world use cases for this. | |
1559 | B<Note:> This functionality was added specifically for L<Test::Exception>, | |
1560 | which has one of the few real-world use cases. | |
c00d8759 | 1561 | |
c00d8759 SP |
1562 | =back |
1563 | ||
6bdb8877 | 1564 | =head3 DELEGATES TO STREAM |
c00d8759 | 1565 | |
6bdb8877 | 1566 | Each of these is a shortcut to C<< $Test->stream->NAME >> |
33459055 | 1567 | |
6bdb8877 | 1568 | See the L<Test::Builder::Stream> documentation for details. |
c00d8759 | 1569 | |
33459055 MS |
1570 | =over 4 |
1571 | ||
6bdb8877 | 1572 | =item $Test->is_passing(...) |
33459055 | 1573 | |
6bdb8877 | 1574 | =item $Test->listen(...) |
33459055 | 1575 | |
6bdb8877 | 1576 | =item $Test->munge(...) |
33459055 | 1577 | |
6bdb8877 | 1578 | =item $Test->tap |
33459055 | 1579 | |
6bdb8877 | 1580 | =item $Test->lresults |
33459055 | 1581 | |
6bdb8877 | 1582 | =item $Test->use_fork |
c00d8759 | 1583 | |
6bdb8877 | 1584 | =item $Test->no_fork |
33459055 | 1585 | |
6bdb8877 | 1586 | =back |
c00d8759 | 1587 | |
6bdb8877 | 1588 | =head2 CHILDREN AND SUBTESTS |
33459055 | 1589 | |
6bdb8877 | 1590 | =over 4 |
33459055 | 1591 | |
6bdb8877 | 1592 | =item $Test->subtest($name, \&subtests, @args) |
33459055 | 1593 | |
6bdb8877 | 1594 | See documentation of C<subtest> in Test::More. |
33459055 | 1595 | |
6bdb8877 CG |
1596 | C<subtest> also, and optionally, accepts arguments which will be passed to the |
1597 | subtests reference. | |
33459055 | 1598 | |
6bdb8877 | 1599 | =item $child = $Test->child($name) |
33459055 | 1600 | |
6bdb8877 CG |
1601 | my $child = $builder->child($name_of_child); |
1602 | $child->plan( tests => 4 ); | |
1603 | $child->ok(some_code()); | |
1604 | ... | |
1605 | $child->finalize; | |
33459055 | 1606 | |
6bdb8877 CG |
1607 | Returns a new instance of C<Test::Builder>. Any output from this child will |
1608 | be indented four spaces more than the parent's indentation. When done, the | |
1609 | C<finalize> method I<must> be called explicitly. | |
33459055 | 1610 | |
6bdb8877 CG |
1611 | Trying to create a new child with a previous child still active (i.e., |
1612 | C<finalize> not called) will C<croak>. | |
33459055 | 1613 | |
6bdb8877 CG |
1614 | Trying to run a test when you have an open child will also C<croak> and cause |
1615 | the test suite to fail. | |
33459055 | 1616 | |
6bdb8877 | 1617 | =item $ok = $Child->finalize |
33459055 | 1618 | |
6bdb8877 CG |
1619 | When your child is done running tests, you must call C<finalize> to clean up |
1620 | and tell the parent your pass/fail status. | |
33459055 | 1621 | |
6bdb8877 | 1622 | Calling C<finalize> on a child with open children will C<croak>. |
33459055 | 1623 | |
6bdb8877 CG |
1624 | If the child falls out of scope before C<finalize> is called, a failure |
1625 | diagnostic will be issued and the child is considered to have failed. | |
33459055 | 1626 | |
6bdb8877 CG |
1627 | No attempt to call methods on a child after C<finalize> is called is |
1628 | guaranteed to succeed. | |
33459055 | 1629 | |
6bdb8877 | 1630 | Calling this on the root builder is a no-op. |
b1ddf169 | 1631 | |
6bdb8877 | 1632 | =back |
33459055 | 1633 | |
6bdb8877 | 1634 | =head2 STREAM MANAGEMENT |
33459055 | 1635 | |
6bdb8877 | 1636 | =over 4 |
33459055 | 1637 | |
6bdb8877 | 1638 | =item $stream = $Test->stream |
33459055 | 1639 | |
6bdb8877 | 1640 | =item $Test->stream($stream) |
33459055 | 1641 | |
6bdb8877 | 1642 | =item $Test->stream(undef) |
b1ddf169 | 1643 | |
6bdb8877 CG |
1644 | Get/Set the stream. When no stream is set, or is undef it will return the |
1645 | shared stream. | |
b1ddf169 | 1646 | |
6bdb8877 CG |
1647 | B<Note:> Do not set this to the shared stream yourself, set it to undef. This |
1648 | is because the shared stream is actually a stack, and this always returns the | |
1649 | top of the stack. | |
b1ddf169 | 1650 | |
6bdb8877 | 1651 | =item $results = $Test->intercept(\&code) |
33459055 | 1652 | |
6bdb8877 CG |
1653 | Any tests run inside the codeblock will be intercepted and not sent to the |
1654 | normal stream. Instead they will be added to C<$results> which is an array of | |
1655 | L<Test::Builder::Result> objects. | |
33459055 | 1656 | |
6bdb8877 | 1657 | B<Note:> This will also intercept BAIL_OUT and skipall. |
33459055 | 1658 | |
6bdb8877 CG |
1659 | B<Note:> This will only intercept results generated with the Test::Builder |
1660 | object on which C<intercept()> was called. Other builders will still send to | |
1661 | the normal places. | |
33459055 | 1662 | |
6bdb8877 CG |
1663 | See L<Test::Tester2> for a method of capturing results sent to the global |
1664 | stream. | |
1665 | ||
1666 | =back | |
1667 | ||
1668 | =head2 TRACING THE TEST/PROVIDER BOUNDRY | |
1669 | ||
1670 | When a test fails it will report the filename and line where the failure | |
1671 | occured. In order to do this it needs to look at the stack and figure out where | |
1672 | your tests stop, and the tools you are using begin. These methods help you find | |
1673 | the desired caller frame. | |
1674 | ||
1675 | See the L<Test::Builder::Trace> module for more details. | |
33459055 | 1676 | |
6bdb8877 | 1677 | =over 4 |
33459055 | 1678 | |
6bdb8877 | 1679 | =item $trace = $Test->trace_test() |
33459055 | 1680 | |
6bdb8877 | 1681 | Returns an L<Test::Builder::Trace> object. |
33459055 | 1682 | |
6bdb8877 | 1683 | =item $reason = $Test->find_TODO |
4bd4e70a | 1684 | |
6bdb8877 | 1685 | =item $reason = $Test->find_TODO($pack) |
33459055 | 1686 | |
6bdb8877 | 1687 | =item $old_reason = $Test->find_TODO($pack, 1, $new_reason); |
33459055 | 1688 | |
6bdb8877 CG |
1689 | Like C<todo()> but only returns the value of C<$TODO> ignoring |
1690 | C<todo_start()>. | |
33459055 | 1691 | |
6bdb8877 CG |
1692 | Can also be used to set C<$TODO> to a new value while returning the |
1693 | old value. | |
7483b81c | 1694 | |
6bdb8877 | 1695 | =back |
33459055 | 1696 | |
6bdb8877 | 1697 | =head2 TEST PLAN |
33459055 | 1698 | |
6bdb8877 | 1699 | =over 4 |
33459055 | 1700 | |
6bdb8877 | 1701 | =item $Test->plan('no_plan'); |
89c1e84a | 1702 | |
6bdb8877 | 1703 | =item $Test->plan( skip_all => $reason ); |
89c1e84a | 1704 | |
6bdb8877 | 1705 | =item $Test->plan( tests => $num_tests ); |
89c1e84a | 1706 | |
6bdb8877 CG |
1707 | A convenient way to set up your tests. Call this and Test::Builder |
1708 | will print the appropriate headers and take the appropriate actions. | |
33459055 | 1709 | |
6bdb8877 | 1710 | If you call C<plan()>, don't call any of the other methods below. |
ccbd73a4 | 1711 | |
6bdb8877 CG |
1712 | If a child calls "skip_all" in the plan, a C<Test::Builder::Exception> is |
1713 | thrown. Trap this error, call C<finalize()> and don't run any more tests on | |
1714 | the child. | |
ccbd73a4 | 1715 | |
6bdb8877 CG |
1716 | my $child = $Test->child('some child'); |
1717 | eval { $child->plan( $condition ? ( skip_all => $reason ) : ( tests => 3 ) ) }; | |
1718 | if ( eval { $@->isa('Test::Builder::Exception') } ) { | |
1719 | $child->finalize; | |
1720 | return; | |
1721 | } | |
1722 | # run your tests | |
ccbd73a4 | 1723 | |
6bdb8877 | 1724 | =item $Test->no_plan; |
ccbd73a4 | 1725 | |
6bdb8877 | 1726 | Declares that this test will run an indeterminate number of tests. |
ccbd73a4 | 1727 | |
6bdb8877 | 1728 | =item $Test->skip_all |
ccbd73a4 | 1729 | |
6bdb8877 | 1730 | =item $Test->skip_all($reason) |
ccbd73a4 | 1731 | |
6bdb8877 | 1732 | Skips all the tests, using the given C<$reason>. Exits immediately with 0. |
ccbd73a4 | 1733 | |
6bdb8877 | 1734 | =item $Test->done_testing |
ccbd73a4 | 1735 | |
6bdb8877 | 1736 | =item $Test->done_testing($count) |
ccbd73a4 | 1737 | |
6bdb8877 | 1738 | Declares that you are done testing, no more tests will be run after this point. |
b1ddf169 | 1739 | |
6bdb8877 | 1740 | If a plan has not yet been output, it will do so. |
33459055 | 1741 | |
6bdb8877 CG |
1742 | $num_tests is the number of tests you planned to run. If a numbered |
1743 | plan was already declared, and if this contradicts, a failing result | |
1744 | will be run to reflect the planning mistake. If C<no_plan> was declared, | |
1745 | this will override. | |
33459055 | 1746 | |
6bdb8877 CG |
1747 | If C<done_testing()> is called twice, the second call will issue a |
1748 | failing result. | |
7483b81c | 1749 | |
6bdb8877 CG |
1750 | If C<$num_tests> is omitted, the number of tests run will be used, like |
1751 | no_plan. | |
a9153838 | 1752 | |
6bdb8877 CG |
1753 | C<done_testing()> is, in effect, used when you'd want to use C<no_plan>, but |
1754 | safer. You'd use it like so: | |
89c1e84a | 1755 | |
6bdb8877 CG |
1756 | $Test->ok($a == $b); |
1757 | $Test->done_testing(); | |
33459055 | 1758 | |
6bdb8877 | 1759 | Or to plan a variable number of tests: |
ccbd73a4 | 1760 | |
6bdb8877 CG |
1761 | for my $test (@tests) { |
1762 | $Test->ok($test); | |
1763 | } | |
1764 | $Test->done_testing(scalar @tests); | |
ccbd73a4 | 1765 | |
6bdb8877 | 1766 | =back |
ccbd73a4 | 1767 | |
6bdb8877 | 1768 | =head2 SIMPLE RESULT PRODUCERS |
ccbd73a4 | 1769 | |
6bdb8877 CG |
1770 | Each of these produces 1 or more L<Test::Builder::Result> objects which are fed |
1771 | into the result stream. | |
ccbd73a4 | 1772 | |
6bdb8877 | 1773 | =over 4 |
ccbd73a4 | 1774 | |
6bdb8877 | 1775 | =item $Test->ok($test) |
ccbd73a4 | 1776 | |
6bdb8877 | 1777 | =item $Test->ok($test, $name) |
ccbd73a4 | 1778 | |
6bdb8877 | 1779 | =item $Test->ok($test, $name, @diag) |
ccbd73a4 | 1780 | |
6bdb8877 CG |
1781 | Your basic test. Pass if C<$test> is true, fail if $test is false. Just |
1782 | like L<Test::Simple>'s C<ok()>. | |
ccbd73a4 | 1783 | |
6bdb8877 CG |
1784 | You may also specify diagnostics messages in the form of simple strings, or |
1785 | complete <Test::Builder::Result> objects. Typically you would only do this in a | |
1786 | failure, but you are allowed to add diags to passes as well. | |
33459055 | 1787 | |
6bdb8877 | 1788 | =item $Test->BAIL_OUT($reason); |
33459055 | 1789 | |
6bdb8877 CG |
1790 | Indicates to the L<Test::Harness> that things are going so badly all |
1791 | testing should terminate. This includes running any additional test | |
1792 | scripts. | |
33459055 | 1793 | |
6bdb8877 | 1794 | It will exit with 255. |
33459055 | 1795 | |
6bdb8877 | 1796 | =item $Test->skip |
33459055 | 1797 | |
6bdb8877 | 1798 | =item $Test->skip($why) |
33459055 | 1799 | |
6bdb8877 | 1800 | Skips the current test, reporting C<$why>. |
ccbd73a4 | 1801 | |
6bdb8877 | 1802 | =item $Test->todo_skip |
33459055 | 1803 | |
6bdb8877 | 1804 | =item $Test->todo_skip($why) |
33459055 | 1805 | |
6bdb8877 CG |
1806 | Like C<skip()>, only it will declare the test as failing and TODO. Similar |
1807 | to | |
7483b81c | 1808 | |
6bdb8877 | 1809 | print "not ok $tnum # TODO $why\n"; |
89c1e84a | 1810 | |
6bdb8877 | 1811 | =item $Test->diag(@msgs) |
89c1e84a | 1812 | |
6bdb8877 CG |
1813 | Prints out the given C<@msgs>. Like C<print>, arguments are simply |
1814 | appended together. | |
89c1e84a | 1815 | |
6bdb8877 CG |
1816 | Normally, it uses the C<failure_output()> handle, but if this is for a |
1817 | TODO test, the C<todo_output()> handle is used. | |
33459055 | 1818 | |
6bdb8877 CG |
1819 | Output will be indented and marked with a # so as not to interfere |
1820 | with test output. A newline will be put on the end if there isn't one | |
1821 | already. | |
33459055 | 1822 | |
6bdb8877 | 1823 | We encourage using this rather than calling print directly. |
33459055 | 1824 | |
6bdb8877 CG |
1825 | Returns false. Why? Because C<diag()> is often used in conjunction with |
1826 | a failing test (C<ok() || diag()>) it "passes through" the failure. | |
33459055 | 1827 | |
6bdb8877 | 1828 | return ok(...) || diag(...); |
33459055 | 1829 | |
6bdb8877 | 1830 | =item $Test->note(@msgs) |
33459055 | 1831 | |
6bdb8877 CG |
1832 | Like C<diag()>, but it prints to the C<output()> handle so it will not |
1833 | normally be seen by the user except in verbose mode. | |
33459055 | 1834 | |
6bdb8877 | 1835 | =back |
33459055 | 1836 | |
6bdb8877 | 1837 | =head2 ADVANCED RESULT PRODUCERS |
33459055 | 1838 | |
6bdb8877 | 1839 | =over 4 |
33459055 | 1840 | |
6bdb8877 | 1841 | =item $Test->is_eq($got, $expected, $name) |
33459055 | 1842 | |
6bdb8877 CG |
1843 | Like Test::More's C<is()>. Checks if C<$got eq $expected>. This is the |
1844 | string version. | |
33459055 | 1845 | |
6bdb8877 | 1846 | C<undef> only ever matches another C<undef>. |
33459055 | 1847 | |
6bdb8877 | 1848 | =item $Test->is_num($got, $expected, $name) |
33459055 | 1849 | |
6bdb8877 CG |
1850 | Like Test::More's C<is()>. Checks if C<$got == $expected>. This is the |
1851 | numeric version. | |
33459055 | 1852 | |
6bdb8877 | 1853 | C<undef> only ever matches another C<undef>. |
33459055 | 1854 | |
6bdb8877 | 1855 | =item $Test->isnt_eq($got, $dont_expect, $name) |
33459055 | 1856 | |
6bdb8877 CG |
1857 | Like L<Test::More>'s C<isnt()>. Checks if C<$got ne $dont_expect>. This is |
1858 | the string version. | |
33459055 | 1859 | |
6bdb8877 | 1860 | =item $Test->isnt_num($got, $dont_expect, $name) |
33459055 | 1861 | |
6bdb8877 CG |
1862 | Like L<Test::More>'s C<isnt()>. Checks if C<$got ne $dont_expect>. This is |
1863 | the numeric version. | |
33459055 | 1864 | |
6bdb8877 | 1865 | =item $Test->like($thing, qr/$regex/, $name) |
33459055 | 1866 | |
6bdb8877 | 1867 | =item $Test->like($thing, '/$regex/', $name) |
33459055 | 1868 | |
6bdb8877 | 1869 | Like L<Test::More>'s C<like()>. Checks if $thing matches the given C<$regex>. |
ccbd73a4 | 1870 | |
6bdb8877 | 1871 | =item $Test->unlike($thing, qr/$regex/, $name) |
30e302f8 | 1872 | |
6bdb8877 | 1873 | =item $Test->unlike($thing, '/$regex/', $name) |
30e302f8 | 1874 | |
6bdb8877 CG |
1875 | Like L<Test::More>'s C<unlike()>. Checks if $thing $Test->does not match the |
1876 | given C<$regex>. | |
30e302f8 | 1877 | |
6bdb8877 | 1878 | =item $Test->cmp_ok($thing, $type, $that, $name) |
a9153838 | 1879 | |
6bdb8877 | 1880 | Works just like L<Test::More>'s C<cmp_ok()>. |
a9153838 | 1881 | |
6bdb8877 | 1882 | $Test->cmp_ok($big_num, '!=', $other_big_num); |
33459055 | 1883 | |
6bdb8877 | 1884 | =back |
5143c659 | 1885 | |
6bdb8877 | 1886 | =head2 PUBLIC HELPERS |
ccbd73a4 | 1887 | |
6bdb8877 | 1888 | =over 4 |
ccbd73a4 | 1889 | |
6bdb8877 | 1890 | =item @dump = $Test->explain(@msgs) |
ccbd73a4 | 1891 | |
6bdb8877 CG |
1892 | Will dump the contents of any references in a human readable format. |
1893 | Handy for things like... | |
04955c14 | 1894 | |
6bdb8877 | 1895 | is_deeply($have, $want) || diag explain $have; |
33459055 | 1896 | |
6bdb8877 | 1897 | or |
33459055 | 1898 | |
6bdb8877 | 1899 | is_deeply($have, $want) || note explain $have; |
ccbd73a4 | 1900 | |
6bdb8877 | 1901 | =item $tb->carp(@message) |
ccbd73a4 | 1902 | |
6bdb8877 CG |
1903 | Warns with C<@message> but the message will appear to come from the |
1904 | point where the original test function was called (C<< $tb->caller >>). | |
ccbd73a4 | 1905 | |
6bdb8877 | 1906 | =item $tb->croak(@message) |
ccbd73a4 | 1907 | |
6bdb8877 CG |
1908 | Dies with C<@message> but the message will appear to come from the |
1909 | point where the original test function was called (C<< $tb->caller >>). | |
544cdeac | 1910 | |
6bdb8877 | 1911 | =item $plan = $Test->has_plan |
544cdeac | 1912 | |
6bdb8877 CG |
1913 | Find out whether a plan has been defined. C<$plan> is either C<undef> (no plan |
1914 | has been set), C<no_plan> (indeterminate # of tests) or an integer (the number | |
1915 | of expected tests). | |
ccbd73a4 | 1916 | |
6bdb8877 | 1917 | =item $Test->reset |
ccbd73a4 | 1918 | |
6bdb8877 CG |
1919 | Reinitializes the Test::Builder singleton to its original state. |
1920 | Mostly useful for tests run in persistent environments where the same | |
1921 | test might be run multiple times in the same process. | |
ccbd73a4 | 1922 | |
6bdb8877 | 1923 | =item %context = $Test->context |
ccbd73a4 | 1924 | |
6bdb8877 | 1925 | Returns a hash of contextual info. |
bdff39c7 | 1926 | |
6bdb8877 CG |
1927 | ( |
1928 | depth => DEPTH, | |
1929 | source => NAME, | |
1930 | trace => TRACE, | |
1931 | ) | |
ccbd73a4 | 1932 | |
6bdb8877 | 1933 | =back |
04955c14 | 1934 | |
6bdb8877 | 1935 | =head2 TODO MANAGEMENT |
b7f9bbeb | 1936 | |
6bdb8877 | 1937 | =over 4 |
b7f9bbeb | 1938 | |
6bdb8877 | 1939 | =item $todo_reason = $Test->todo |
b7f9bbeb | 1940 | |
6bdb8877 | 1941 | =item $todo_reason = $Test->todo($pack) |
b7f9bbeb | 1942 | |
6bdb8877 CG |
1943 | If the current tests are considered "TODO" it will return the reason, |
1944 | if any. This reason can come from a C<$TODO> variable or the last call | |
1945 | to C<todo_start()>. | |
b7f9bbeb | 1946 | |
6bdb8877 CG |
1947 | Since a TODO test does not need a reason, this function can return an |
1948 | empty string even when inside a TODO block. Use C<< $Test->in_todo >> | |
1949 | to determine if you are currently inside a TODO block. | |
b7f9bbeb | 1950 | |
6bdb8877 CG |
1951 | C<todo()> is about finding the right package to look for C<$TODO> in. It's |
1952 | pretty good at guessing the right package to look at. It considers the stack | |
1953 | trace, C<$Level>, and metadata associated with various packages. | |
b7f9bbeb | 1954 | |
6bdb8877 CG |
1955 | Sometimes there is some confusion about where C<todo()> should be looking |
1956 | for the C<$TODO> variable. If you want to be sure, tell it explicitly | |
1957 | what $pack to use. | |
b7f9bbeb | 1958 | |
6bdb8877 | 1959 | =item $in_todo = $Test->in_todo |
b7f9bbeb | 1960 | |
6bdb8877 | 1961 | Returns true if the test is currently inside a TODO block. |
b7f9bbeb | 1962 | |
6bdb8877 | 1963 | =item $Test->todo_start() |
b7f9bbeb | 1964 | |
6bdb8877 | 1965 | =item $Test->todo_start($message) |
b7f9bbeb | 1966 | |
6bdb8877 CG |
1967 | This method allows you declare all subsequent tests as TODO tests, up until |
1968 | the C<todo_end> method has been called. | |
33459055 | 1969 | |
6bdb8877 CG |
1970 | The C<TODO:> and C<$TODO> syntax is generally pretty good about figuring out |
1971 | whether or not we're in a TODO test. However, often we find that this is not | |
1972 | possible to determine (such as when we want to use C<$TODO> but | |
1973 | the tests are being executed in other packages which can't be inferred | |
1974 | beforehand). | |
33459055 | 1975 | |
6bdb8877 | 1976 | Note that you can use this to nest "todo" tests |
33459055 | 1977 | |
6bdb8877 CG |
1978 | $Test->todo_start('working on this'); |
1979 | # lots of code | |
1980 | $Test->todo_start('working on that'); | |
1981 | # more code | |
1982 | $Test->todo_end; | |
1983 | $Test->todo_end; | |
33459055 | 1984 | |
6bdb8877 CG |
1985 | This is generally not recommended, but large testing systems often have weird |
1986 | internal needs. | |
33459055 | 1987 | |
6bdb8877 CG |
1988 | We've tried to make this also work with the TODO: syntax, but it's not |
1989 | guaranteed and its use is also discouraged: | |
33459055 | 1990 | |
6bdb8877 CG |
1991 | TODO: { |
1992 | local $TODO = 'We have work to do!'; | |
1993 | $Test->todo_start('working on this'); | |
1994 | # lots of code | |
1995 | $Test->todo_start('working on that'); | |
1996 | # more code | |
1997 | $Test->todo_end; | |
1998 | $Test->todo_end; | |
1999 | } | |
33459055 | 2000 | |
6bdb8877 | 2001 | Pick one style or another of "TODO" to be on the safe side. |
33459055 | 2002 | |
6bdb8877 | 2003 | =item $Test->todo_end |
33459055 | 2004 | |
6bdb8877 CG |
2005 | Stops running tests as "TODO" tests. This method is fatal if called without a |
2006 | preceding C<todo_start> method call. | |
33459055 | 2007 | |
6bdb8877 | 2008 | =back |
2c4d5b9b | 2009 | |
6bdb8877 | 2010 | =head2 DEPRECATED/LEGACY |
2c4d5b9b | 2011 | |
6bdb8877 CG |
2012 | All of these will issue warnings if called on a modern Test::Builder object. |
2013 | That is any Test::Builder instance that was created with the 'modern' flag. | |
2c4d5b9b | 2014 | |
6bdb8877 | 2015 | =over |
2c4d5b9b | 2016 | |
6bdb8877 | 2017 | =item $self->no_ending |
2c4d5b9b | 2018 | |
6bdb8877 | 2019 | B<Deprecated:> Moved to the L<Test::Builder::Stream> object. |
2c4d5b9b | 2020 | |
6bdb8877 | 2021 | $Test->no_ending($no_ending); |
2c4d5b9b | 2022 | |
6bdb8877 CG |
2023 | Normally, Test::Builder does some extra diagnostics when the test |
2024 | ends. It also changes the exit code as described below. | |
2c4d5b9b | 2025 | |
6bdb8877 | 2026 | If this is true, none of that will be done. |
2c4d5b9b | 2027 | |
6bdb8877 | 2028 | =item $self->summary |
2c4d5b9b | 2029 | |
6bdb8877 | 2030 | B<Deprecated:> Moved to the L<Test::Builder::Stream> object. |
2c4d5b9b | 2031 | |
6bdb8877 CG |
2032 | The style of result recording used here is deprecated. The functionality was |
2033 | moved to its own object to contain the legacy code. | |
33459055 MS |
2034 | |
2035 | my @tests = $Test->summary; | |
2036 | ||
2037 | A simple summary of the tests so far. True for pass, false for fail. | |
2038 | This is a logical pass/fail, so todos are passes. | |
2039 | ||
2040 | Of course, test #1 is $tests[0], etc... | |
2041 | ||
6bdb8877 | 2042 | =item $self->details |
33459055 | 2043 | |
6bdb8877 | 2044 | B<Deprecated:> Moved to the L<Test::Builder::Formatter::LegacyResults> object. |
33459055 | 2045 | |
6bdb8877 CG |
2046 | The style of result recording used here is deprecated. The functionality was |
2047 | moved to its own object to contain the legacy code. | |
33459055 MS |
2048 | |
2049 | my @tests = $Test->details; | |
2050 | ||
3e887aae | 2051 | Like C<summary()>, but with a lot more detail. |
33459055 | 2052 | |
6bdb8877 | 2053 | $tests[$test_num - 1] = |
60ffb308 | 2054 | { 'ok' => is the test considered a pass? |
33459055 MS |
2055 | actual_ok => did it literally say 'ok'? |
2056 | name => name of the test (if any) | |
60ffb308 | 2057 | type => type of test (if any, see below). |
33459055 MS |
2058 | reason => reason for the above (if any) |
2059 | }; | |
2060 | ||
60ffb308 MS |
2061 | 'ok' is true if Test::Harness will consider the test to be a pass. |
2062 | ||
2063 | 'actual_ok' is a reflection of whether or not the test literally | |
2064 | printed 'ok' or 'not ok'. This is for examining the result of 'todo' | |
3e887aae | 2065 | tests. |
60ffb308 MS |
2066 | |
2067 | 'name' is the name of the test. | |
2068 | ||
2069 | 'type' indicates if it was a special test. Normal tests have a type | |
2070 | of ''. Type can be one of the following: | |
2071 | ||
2072 | skip see skip() | |
2073 | todo see todo() | |
2074 | todo_skip see todo_skip() | |
2075 | unknown see below | |
2076 | ||
2077 | Sometimes the Test::Builder test counter is incremented without it | |
3e887aae | 2078 | printing any test output, for example, when C<current_test()> is changed. |
60ffb308 | 2079 | In these cases, Test::Builder doesn't know the result of the test, so |
ccbd73a4 | 2080 | its type is 'unknown'. These details for these tests are filled in. |
3e887aae | 2081 | They are considered ok, but the name and actual_ok is left C<undef>. |
60ffb308 MS |
2082 | |
2083 | For example "not ok 23 - hole count # TODO insufficient donuts" would | |
2084 | result in this structure: | |
2085 | ||
2086 | $tests[22] = # 23 - 1, since arrays start from 0. | |
3e887aae | 2087 | { ok => 1, # logically, the test passed since its todo |
60ffb308 MS |
2088 | actual_ok => 0, # in absolute terms, it failed |
2089 | name => 'hole count', | |
2090 | type => 'todo', | |
2091 | reason => 'insufficient donuts' | |
2092 | }; | |
2093 | ||
6bdb8877 | 2094 | =item $self->no_header |
60ffb308 | 2095 | |
6bdb8877 | 2096 | B<Deprecated:> moved to the L<Test::Builder::Formatter::TAP> object. |
33459055 | 2097 | |
6bdb8877 | 2098 | $Test->no_header($no_header); |
33459055 | 2099 | |
6bdb8877 | 2100 | If set to true, no "1..N" header will be printed. |
ccbd73a4 | 2101 | |
6bdb8877 | 2102 | =item $self->no_diag |
33459055 | 2103 | |
6bdb8877 | 2104 | B<Deprecated:> moved to the L<Test::Builder::Formatter::TAP> object. |
33459055 | 2105 | |
6bdb8877 CG |
2106 | If set true no diagnostics will be printed. This includes calls to |
2107 | C<diag()>. | |
33459055 | 2108 | |
6bdb8877 | 2109 | =item $self->output |
33459055 | 2110 | |
6bdb8877 | 2111 | =item $self->failure_output |
ccbd73a4 | 2112 | |
6bdb8877 | 2113 | =item $self->todo_output |
ccbd73a4 | 2114 | |
6bdb8877 | 2115 | B<Deprecated:> moved to the L<Test::Builder::Formatter::TAP> object. |
ccbd73a4 | 2116 | |
6bdb8877 CG |
2117 | my $filehandle = $Test->output; |
2118 | $Test->output($filehandle); | |
2119 | $Test->output($filename); | |
2120 | $Test->output(\$scalar); | |
ccbd73a4 | 2121 | |
6bdb8877 CG |
2122 | These methods control where Test::Builder will print its output. |
2123 | They take either an open C<$filehandle>, a C<$filename> to open and write to | |
2124 | or a C<$scalar> reference to append to. It will always return a C<$filehandle>. | |
33459055 | 2125 | |
6bdb8877 | 2126 | B<output> is where normal "ok/not ok" test output goes. |
ccbd73a4 | 2127 | |
6bdb8877 | 2128 | Defaults to STDOUT. |
ccbd73a4 | 2129 | |
6bdb8877 CG |
2130 | B<failure_output> is where diagnostic output on test failures and |
2131 | C<diag()> goes. It is normally not read by Test::Harness and instead is | |
2132 | displayed to the user. | |
809046db | 2133 | |
6bdb8877 | 2134 | Defaults to STDERR. |
809046db | 2135 | |
6bdb8877 CG |
2136 | C<todo_output> is used instead of C<failure_output()> for the |
2137 | diagnostics of a failing TODO test. These will not be seen by the | |
2138 | user. | |
ccbd73a4 | 2139 | |
6bdb8877 | 2140 | Defaults to STDOUT. |
04955c14 | 2141 | |
6bdb8877 | 2142 | =item $self->reset_outputs |
33459055 | 2143 | |
6bdb8877 | 2144 | B<Deprecated:> moved to the L<Test::Builder::Formatter::TAP> object. |
ccbd73a4 | 2145 | |
6bdb8877 | 2146 | $tb->reset_outputs; |
ccbd73a4 | 2147 | |
6bdb8877 | 2148 | Resets all the output filehandles back to their defaults. |
ccbd73a4 | 2149 | |
6bdb8877 | 2150 | =item $self->use_numbers |
ccbd73a4 | 2151 | |
6bdb8877 | 2152 | B<Deprecated:> moved to the L<Test::Builder::Formatter::TAP> object. |
ccbd73a4 | 2153 | |
6bdb8877 | 2154 | $Test->use_numbers($on_or_off); |
ccbd73a4 | 2155 | |
6bdb8877 | 2156 | Whether or not the test should output numbers. That is, this if true: |
ccbd73a4 | 2157 | |
6bdb8877 CG |
2158 | ok 1 |
2159 | ok 2 | |
2160 | ok 3 | |
ccbd73a4 | 2161 | |
6bdb8877 | 2162 | or this if false |
ccbd73a4 | 2163 | |
6bdb8877 CG |
2164 | ok |
2165 | ok | |
2166 | ok | |
ccbd73a4 | 2167 | |
6bdb8877 CG |
2168 | Most useful when you can't depend on the test output order, such as |
2169 | when threads or forking is involved. | |
ccbd73a4 | 2170 | |
6bdb8877 | 2171 | Defaults to on. |
ccbd73a4 | 2172 | |
6bdb8877 | 2173 | =item $pack = $Test->exported_to |
ccbd73a4 | 2174 | |
6bdb8877 | 2175 | =item $Test->exported_to($pack) |
ccbd73a4 | 2176 | |
6bdb8877 CG |
2177 | B<Deprecated:> Use C<< Test::Builder::Trace->anoint($package) >> and |
2178 | C<< $Test->trace_anointed >> instead. | |
ccbd73a4 | 2179 | |
6bdb8877 | 2180 | Tells Test::Builder what package you exported your functions to. |
ccbd73a4 | 2181 | |
6bdb8877 CG |
2182 | This method isn't terribly useful since modules which share the same |
2183 | Test::Builder object might get exported to different packages and only | |
2184 | the last one will be honored. | |
ccbd73a4 | 2185 | |
6bdb8877 | 2186 | =item $is_fh = $Test->is_fh($thing); |
ccbd73a4 | 2187 | |
6bdb8877 | 2188 | Determines if the given C<$thing> can be used as a filehandle. |
ccbd73a4 | 2189 | |
6bdb8877 | 2190 | =item $curr_test = $Test->current_test; |
ccbd73a4 | 2191 | |
6bdb8877 | 2192 | =item $Test->current_test($num); |
ccbd73a4 | 2193 | |
6bdb8877 CG |
2194 | Gets/sets the current test number we're on. You usually shouldn't |
2195 | have to set this. | |
ccbd73a4 | 2196 | |
6bdb8877 CG |
2197 | If set forward, the details of the missing tests are filled in as 'unknown'. |
2198 | if set backward, the details of the intervening tests are deleted. You | |
2199 | can erase history if you really want to. | |
ccbd73a4 | 2200 | |
6bdb8877 | 2201 | =item $Test->BAIL_OUT($reason); |
ccbd73a4 | 2202 | |
6bdb8877 CG |
2203 | Indicates to the L<Test::Harness> that things are going so badly all |
2204 | testing should terminate. This includes running any additional test | |
2205 | scripts. | |
ccbd73a4 | 2206 | |
6bdb8877 | 2207 | It will exit with 255. |
ccbd73a4 | 2208 | |
6bdb8877 | 2209 | =item $max = $Test->expected_tests |
ccbd73a4 | 2210 | |
6bdb8877 | 2211 | =item $Test->expected_tests($max) |
ccbd73a4 | 2212 | |
6bdb8877 CG |
2213 | Gets/sets the number of tests we expect this test to run and prints out |
2214 | the appropriate headers. | |
ccbd73a4 | 2215 | |
6bdb8877 | 2216 | =item $package = $Test->caller |
33459055 | 2217 | |
6bdb8877 | 2218 | =item ($pack, $file, $line) = $Test->caller |
33459055 | 2219 | |
6bdb8877 | 2220 | =item ($pack, $file, $line) = $Test->caller($height) |
33459055 | 2221 | |
3e887aae | 2222 | Like the normal C<caller()>, except it reports according to your C<level()>. |
33459055 | 2223 | |
3e887aae | 2224 | C<$height> will be added to the C<level()>. |
04955c14 | 2225 | |
3e887aae | 2226 | If C<caller()> winds up off the top of the stack it report the highest context. |
82d700dc | 2227 | |
6bdb8877 | 2228 | =item $Test->level($how_high) |
33459055 | 2229 | |
6bdb8877 CG |
2230 | B<DEPRECATED> See deprecation notes at the top. The use of C<level()> is |
2231 | deprecated. | |
33459055 | 2232 | |
6bdb8877 CG |
2233 | How far up the call stack should C<$Test> look when reporting where the |
2234 | test failed. | |
33459055 | 2235 | |
6bdb8877 | 2236 | Defaults to 1. |
33459055 | 2237 | |
6bdb8877 CG |
2238 | Setting L<$Test::Builder::Level> overrides. This is typically useful |
2239 | localized: | |
33459055 | 2240 | |
6bdb8877 CG |
2241 | sub my_ok { |
2242 | my $test = shift; | |
33459055 | 2243 | |
6bdb8877 CG |
2244 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
2245 | $TB->ok($test); | |
33459055 | 2246 | } |
ccbd73a4 | 2247 | |
6bdb8877 CG |
2248 | To be polite to other functions wrapping your own you usually want to increment |
2249 | C<$Level> rather than set it to a constant. | |
33459055 | 2250 | |
6bdb8877 | 2251 | =item $Test->maybe_regex(qr/$regex/) |
33459055 | 2252 | |
6bdb8877 | 2253 | =item $Test->maybe_regex('/$regex/') |
33459055 | 2254 | |
6bdb8877 CG |
2255 | This method used to be useful back when Test::Builder worked on Perls |
2256 | before 5.6 which didn't have qr//. Now its pretty useless. | |
33459055 | 2257 | |
6bdb8877 CG |
2258 | Convenience method for building testing functions that take regular |
2259 | expressions as arguments. | |
33459055 | 2260 | |
6bdb8877 CG |
2261 | Takes a quoted regular expression produced by C<qr//>, or a string |
2262 | representing a regular expression. | |
33459055 | 2263 | |
6bdb8877 CG |
2264 | Returns a Perl value which may be used instead of the corresponding |
2265 | regular expression, or C<undef> if its argument is not recognised. | |
33459055 | 2266 | |
6bdb8877 CG |
2267 | For example, a version of C<like()>, sans the useful diagnostic messages, |
2268 | could be written as: | |
33459055 | 2269 | |
6bdb8877 CG |
2270 | sub laconic_like { |
2271 | my ($self, $thing, $regex, $name) = @_; | |
2272 | my $usable_regex = $self->maybe_regex($regex); | |
2273 | die "expecting regex, found '$regex'\n" | |
2274 | unless $usable_regex; | |
2275 | $self->ok($thing =~ m/$usable_regex/, $name); | |
2276 | } | |
33459055 | 2277 | |
6bdb8877 | 2278 | =back |
ccbd73a4 | 2279 | |
6bdb8877 | 2280 | =head1 PACKAGE VARIABLES |
411e93ce | 2281 | |
6bdb8877 CG |
2282 | B<NOTE>: These are tied to the package, not the instance. Basically that means |
2283 | touching these can affect more things than you expect. Using these can lead to | |
2284 | unexpected interactions at a distance. | |
411e93ce | 2285 | |
6bdb8877 | 2286 | =over 4 |
3e887aae | 2287 | |
6bdb8877 | 2288 | =item C<$Level> |
04955c14 | 2289 | |
6bdb8877 CG |
2290 | Originally this was the only way to tell Test::Builder where in the stack |
2291 | errors should be reported. Now the preferred method of finding where errors | |
2292 | should be reported is using the L<Test::Builder::Trace> and | |
2293 | L<Test::Builder::Provider> modules. | |
33459055 | 2294 | |
6bdb8877 CG |
2295 | C<$Level> should be considered deprecated when possible, that said it will not |
2296 | be removed any time soon. There is too much legacy code that depends on | |
2297 | C<$Level>. There are also a couple situations in which C<$Level> is necessary: | |
a344be10 | 2298 | |
6bdb8877 | 2299 | =over 4 |
33459055 | 2300 | |
6bdb8877 | 2301 | =item Backwards compatibility |
b1ddf169 | 2302 | |
6bdb8877 CG |
2303 | If code simply cannot depend on a recent version of Test::Builder, then $Level |
2304 | must be used as there is no alternative. See L<Test::Builder::Compat> for tools | |
2305 | to help make test tools that work in old and new versions. | |
b1ddf169 | 2306 | |
6bdb8877 | 2307 | =item Stack Management |
b1ddf169 | 2308 | |
6bdb8877 CG |
2309 | Using L<Test::Builder::Provider> is not practical for situations like in |
2310 | L<Test::Exception> where one needs to munge the call stack to hide frames. | |
b1ddf169 | 2311 | |
6bdb8877 | 2312 | =back |
33459055 | 2313 | |
6bdb8877 | 2314 | =item C<$BLevel> |
33459055 | 2315 | |
6bdb8877 CG |
2316 | Used internally by the L<Test::Builder::Trace>, do not modify or rely on this |
2317 | in your own code. Documented for completeness. | |
b1ddf169 | 2318 | |
6bdb8877 | 2319 | =item C<$Test> |
ccbd73a4 | 2320 | |
6bdb8877 CG |
2321 | The singleton returned by C<new()>, which is deprecated in favor of |
2322 | C<create()>. | |
33459055 | 2323 | |
6bdb8877 | 2324 | =back |
33459055 | 2325 | |
30e302f8 NC |
2326 | =head1 EXIT CODES |
2327 | ||
2328 | If all your tests passed, Test::Builder will exit with zero (which is | |
2329 | normal). If anything failed it will exit with how many failed. If | |
2330 | you run less (or more) tests than you planned, the missing (or extras) | |
2331 | will be considered failures. If no tests were ever run Test::Builder | |
2332 | will throw a warning and exit with 255. If the test died, even after | |
2333 | having successfully completed all its tests, it will still be | |
2334 | considered a failure and will exit with 255. | |
2335 | ||
2336 | So the exit codes are... | |
2337 | ||
2338 | 0 all tests successful | |
b1ddf169 | 2339 | 255 test died or all passed but wrong # of tests run |
30e302f8 NC |
2340 | any other number how many failed (including missing or extras) |
2341 | ||
2342 | If you fail more than 254 tests, it will be reported as 254. | |
2343 | ||
6bdb8877 CG |
2344 | B<Note:> The magic that accomplishes this has been moved to |
2345 | L<Test::Builder::ExitMagic> | |
2346 | ||
a344be10 MS |
2347 | =head1 THREADS |
2348 | ||
b7f9bbeb | 2349 | In perl 5.8.1 and later, Test::Builder is thread-safe. The test |
6bdb8877 | 2350 | number is shared amongst all threads. |
a344be10 | 2351 | |
b7f9bbeb SP |
2352 | While versions earlier than 5.8.1 had threads they contain too many |
2353 | bugs to support. | |
2354 | ||
30e302f8 NC |
2355 | Test::Builder is only thread-aware if threads.pm is loaded I<before> |
2356 | Test::Builder. | |
2357 | ||
3e887aae DM |
2358 | =head1 MEMORY |
2359 | ||
6bdb8877 CG |
2360 | B<Note:> This only applies if you turn lresults on. |
2361 | ||
2362 | $Test->stream->no_lresults; | |
2363 | ||
2364 | An informative hash, accessible via C<details()>, is stored for each | |
3e887aae DM |
2365 | test you perform. So memory usage will scale linearly with each test |
2366 | run. Although this is not a problem for most test suites, it can | |
2367 | become an issue if you do large (hundred thousands to million) | |
2368 | combinatorics tests in the same run. | |
2369 | ||
2370 | In such cases, you are advised to either split the test file into smaller | |
2371 | ones, or use a reverse approach, doing "normal" (code) compares and | |
6bdb8877 | 2372 | triggering C<fail()> should anything go unexpected. |
3e887aae | 2373 | |
33459055 MS |
2374 | =head1 EXAMPLES |
2375 | ||
6bdb8877 CG |
2376 | CPAN can provide the best examples. L<Test::Simple>, L<Test::More>, |
2377 | L<Test::Exception> and L<Test::Differences> all use Test::Builder. | |
33459055 | 2378 | |
4bd4e70a JH |
2379 | =head1 SEE ALSO |
2380 | ||
6bdb8877 | 2381 | L<Test::Simple>, L<Test::More>, L<Test::Harness>, L<Fennec> |
4bd4e70a JH |
2382 | |
2383 | =head1 AUTHORS | |
33459055 MS |
2384 | |
2385 | Original code by chromatic, maintained by Michael G Schwern | |
6bdb8877 CG |
2386 | E<lt>schwern@pobox.comE<gt> until 2014. Currently maintained by Chad Granum |
2387 | E<lt>exodist7@gmail.comE<gt>. | |
33459055 | 2388 | |
9e15e51b CBW |
2389 | =head1 MAINTAINERS |
2390 | ||
2391 | =over 4 | |
2392 | ||
2393 | =item Chad Granum E<lt>exodist@cpan.orgE<gt> | |
2394 | ||
2395 | =back | |
2396 | ||
4bd4e70a | 2397 | =head1 COPYRIGHT |
33459055 | 2398 | |
6bdb8877 CG |
2399 | Copyright 2002-2014 by chromatic E<lt>chromatic@wgz.orgE<gt> and |
2400 | Michael G Schwern E<lt>schwern@pobox.comE<gt> and | |
2401 | Chad Granum E<lt>exodist7@gmail.comE<gt> | |
4bd4e70a | 2402 | |
3e887aae | 2403 | This program is free software; you can redistribute it and/or |
4bd4e70a JH |
2404 | modify it under the same terms as Perl itself. |
2405 | ||
a9153838 | 2406 | See F<http://www.perl.com/perl/misc/Artistic.html> |
33459055 | 2407 |