7 our $VERSION = '1.301001_070';
8 $VERSION = eval $VERSION; ## no critic (BuiltinFunctions::ProhibitStringyEval)
10 use Test::More::Tools;
12 use Test::Stream qw/ -internal STATE_LEGACY STATE_PLAN STATE_COUNT /;
13 use Test::Stream::Toolset;
14 use Test::Stream::Context;
15 use Test::Stream::Carp qw/confess/;
16 use Test::Stream::Meta qw/MODERN/;
18 use Test::Stream::Util qw/try protect unoverload_str is_regex/;
19 use Scalar::Util qw/blessed reftype/;
22 my $meta = Test::Stream::Meta->is_tester('main');
23 Test::Stream->shared->set_use_legacy(1)
24 unless $meta && $meta->[MODERN];
27 # The mostly-singleton, and other package vars.
28 our $Test = Test::Builder->new;
29 our $_ORIG_Test = $Test;
33 my $self = shift || die "No self in context";
35 my $ctx = Test::Stream::Context::context(2 + ($add || 0), $self->{stream});
36 if (defined $self->{Todo}) {
38 $ctx->set_todo($self->{Todo});
39 $ctx->set_diag_todo(1);
46 return $self->{stream} || Test::Stream->shared;
49 sub depth { $_[0]->{depth} || 0 }
51 # This is only for unit tests at this point.
55 require Test::Stream::ExitMagic;
56 $self->{stream}->set_no_ending(0);
57 Test::Stream::ExitMagic->new->do_magic($self->{stream}, $ctx);
67 done_testing => \&done_testing,
70 sub WARN_OF_OVERRIDE {
73 return unless $ctx->modern;
74 my $old = $ORIG{$sub};
75 # Use package instead of self, we want replaced subs, not subclass overrides.
76 my $new = __PACKAGE__->can($sub);
78 return if $new == $old;
81 my $o = B::svref_2object($new);
85 my $pkg = $gv->STASH->NAME;
89 warn <<" EOT" unless $WARNED{"$pkg $name $file $line"}++;
91 *******************************************************************************
92 Something monkeypatched Test::Builder::$sub()!
93 The new sub is '$pkg\::$name' defined in $file around line $line.
94 In the near future monkeypatching Test::Builder::ok() will no longer work
96 *******************************************************************************
108 $Test ||= $class->create(shared_stream => 1);
117 my $self = bless {}, $class;
118 $self->reset(%params);
123 # Copy an object, currently a shallow.
124 # This does *not* bless the destination. This keeps the destructor from
125 # firing when we're just storing a copy of the object to restore later.
127 my ($src, $dest) = @_;
136 #############################
137 # {{{ Children and subtests #
138 #############################
142 my $ctx = $self->ctx();
143 return tmt->subtest(@_);
147 my( $self, $name ) = @_;
149 my $ctx = $self->ctx;
151 if ($self->{child}) {
152 my $cname = $self->{child}->{Name};
153 $ctx->throw("You already have a child named ($cname) running");
156 $name ||= "Child of " . $self->{Name};
157 $ctx->child('push', $name, 1);
159 my $stream = $self->{stream} || Test::Stream->shared;
168 $child->{Name} = $name;
169 $self->{child} = $child;
170 Scalar::Util::weaken($self->{child});
178 return unless $self->{parent};
180 my $ctx = $self->ctx;
182 if ($self->{child}) {
183 my $cname = $self->{child}->{Name};
184 $ctx->throw("Can't call finalize() with child ($cname) active");
187 $self->_ending($ctx);
188 my $passing = $ctx->stream->is_passing;
189 my $count = $ctx->stream->count;
190 my $name = $self->{Name};
193 my $stream = $self->{stream} || Test::Stream->shared;
195 my $parent = $self->parent;
196 $self->{parent}->{child} = undef;
197 $self->{parent} = undef;
202 $ctx->child('pop', $self->{Name});
207 my $ctx = $self->ctx;
208 return scalar @{$ctx->stream->subtests};
211 sub parent { $_[0]->{parent} }
212 sub name { $_[0]->{Name} }
216 return unless $self->{parent};
217 return if $self->{Skip_All};
218 $self->{parent}->is_passing(0);
219 my $name = $self->{Name};
220 die "Child ($name) exited without calling finalize()";
223 #############################
224 # }}} Children and subtests #
225 #############################
227 #####################################
228 # {{{ stuff for TODO status #
229 #####################################
232 my ($self, $pack, $set, $new_value) = @_;
235 if (my $ctx = Test::Stream::Context->peek) {
236 $pack = $ctx->package;
237 my $old = $ctx->todo;
238 $ctx->set_todo($new_value) if $set;
242 $pack = $self->exported_to || return;
245 no strict 'refs'; ## no critic
247 my $old_value = ${$pack . '::TODO'};
248 $set and ${$pack . '::TODO'} = $new_value;
253 my ($self, $pack) = @_;
255 return $self->{Todo} if defined $self->{Todo};
257 my $ctx = $self->ctx;
259 my $todo = $self->find_TODO($pack);
260 return $todo if defined $todo;
268 my $ctx = $self->ctx;
269 return 1 if $ctx->in_todo;
271 return (defined $self->{Todo} || $self->find_TODO) ? 1 : 0;
276 my $message = @_ ? shift : '';
278 $self->{Start_Todo}++;
279 if ($self->in_todo) {
280 push @{$self->{Todo_Stack}} => $self->todo;
282 $self->{Todo} = $message;
290 if (!$self->{Start_Todo}) {
291 $self->ctx(-1)->throw('todo_end() called without todo_start()');
294 $self->{Start_Todo}--;
296 if ($self->{Start_Todo} && @{$self->{Todo_Stack}}) {
297 $self->{Todo} = pop @{$self->{Todo_Stack}};
300 delete $self->{Todo};
306 #####################################
307 # }}} Finding Testers and Providers #
308 #####################################
315 no_plan => 'no_plan',
316 skip_all => 'skip_all',
317 tests => '_plan_tests',
321 my ($self, $cmd, @args) = @_;
323 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
324 WARN_OF_OVERRIDE(plan => $ctx);
328 if (my $method = $PLAN_CMDS{$cmd}) {
329 $self->$method(@args);
332 my @in = grep { defined } ($cmd, @args);
333 $self->ctx->throw("plan() doesn't understand @in");
340 my ($self, $reason) = @_;
342 $self->{Skip_All} = 1;
344 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
346 $ctx->_plan(0, 'SKIP', $reason);
350 my ($self, @args) = @_;
352 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
354 $ctx->alert("no_plan takes no arguments") if @args;
355 $ctx->_plan(0, 'NO PLAN');
361 my ($self, $arg) = @_;
363 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
366 $ctx->throw("Number of tests must be a positive integer. You gave it '$arg'")
367 unless $arg =~ /^\+?\d+$/;
371 elsif (!defined $arg) {
372 $ctx->throw("Got an undefined number of tests");
375 $ctx->throw("You said to run 0 tests");
382 my ($self, $num_tests) = @_;
384 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
385 WARN_OF_OVERRIDE(done_testing => $ctx);
387 my $out = $ctx->stream->done_testing($ctx, $num_tests);
395 #############################
396 # {{{ Base Event Producers #
397 #############################
401 my($test, $name) = @_;
403 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
404 WARN_OF_OVERRIDE(ok => $ctx);
406 if ($self->{child}) {
407 $self->is_passing(0);
408 $ctx->throw("Cannot run test ($name) with active children");
411 $ctx->_unwind_ok($test, $name);
412 return $test ? 1 : 0;
416 my( $self, $reason ) = @_;
417 $self->ctx()->bail($reason);
421 my( $self, $why ) = @_;
423 unoverload_str( \$why );
425 my $ctx = $self->ctx();
426 $ctx->set_skip($why);
428 $ctx->set_skip(undef);
432 my( $self, $why ) = @_;
434 unoverload_str( \$why );
436 my $ctx = $self->ctx();
437 $ctx->set_skip($why);
438 $ctx->set_todo($why);
440 $ctx->set_skip(undef);
441 $ctx->set_todo(undef);
446 my $msg = join '', map { defined($_) ? $_ : 'undef' } @_;
448 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
449 WARN_OF_OVERRIDE(diag => $ctx);
457 my $msg = join '', map { defined($_) ? $_ : 'undef' } @_;
459 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
460 WARN_OF_OVERRIDE(note => $ctx);
465 #############################
466 # }}} Base Event Producers #
467 #############################
469 #######################
470 # {{{ Public helpers #
471 #######################
479 protect { require Data::Dumper };
480 my $dumper = Data::Dumper->new( [$_] );
481 $dumper->Indent(1)->Terse(1);
482 $dumper->Sortkeys(1) if $dumper->can("Sortkeys");
491 $self->ctx->alert(join '' => @_);
496 $self->ctx->throw(join '' => @_);
502 my $plan = $self->ctx->stream->plan || return undef;
503 return 'no_plan' if $plan->directive && $plan->directive eq 'NO PLAN';
511 $self->{use_shared} = 1 if $params{shared_stream};
513 if ($self->{use_shared}) {
514 Test::Stream->shared->_reset;
515 Test::Stream->shared->state->[-1]->[STATE_LEGACY] = [];
518 $self->{stream} = Test::Stream->new();
519 $self->{stream}->set_use_legacy(1);
520 $self->{stream}->state->[-1]->[STATE_LEGACY] = [];
523 # We leave this a global because it has to be localized and localizing
524 # hash keys is just asking for pain. Also, it was documented.
529 $self->{Original_Pid} = $$;
530 $self->{Child_Name} = undef;
532 $self->{Exported_To} = undef;
534 $self->{Todo} = undef;
535 $self->{Todo_Stack} = [];
536 $self->{Start_Todo} = 0;
537 $self->{Opened_Testhandles} = 0;
542 #######################
543 # }}} Public helpers #
544 #######################
546 #################################
547 # {{{ Advanced Event Producers #
548 #################################
551 my( $self, $got, $type, $expect, $name ) = @_;
552 my $ctx = $self->ctx;
553 my ($ok, @diag) = tmt->cmp_check($got, $type, $expect);
554 $ctx->ok($ok, $name, \@diag);
559 my( $self, $got, $expect, $name ) = @_;
560 my $ctx = $self->ctx;
561 my ($ok, @diag) = tmt->is_eq($got, $expect);
562 $ctx->ok($ok, $name, \@diag);
567 my( $self, $got, $expect, $name ) = @_;
568 my $ctx = $self->ctx;
569 my ($ok, @diag) = tmt->is_num($got, $expect);
570 $ctx->ok($ok, $name, \@diag);
575 my( $self, $got, $dont_expect, $name ) = @_;
576 my $ctx = $self->ctx;
577 my ($ok, @diag) = tmt->isnt_eq($got, $dont_expect);
578 $ctx->ok($ok, $name, \@diag);
583 my( $self, $got, $dont_expect, $name ) = @_;
584 my $ctx = $self->ctx;
585 my ($ok, @diag) = tmt->isnt_num($got, $dont_expect);
586 $ctx->ok($ok, $name, \@diag);
591 my( $self, $thing, $regex, $name ) = @_;
592 my $ctx = $self->ctx;
593 my ($ok, @diag) = tmt->regex_check($thing, $regex, '=~');
594 $ctx->ok($ok, $name, \@diag);
599 my( $self, $thing, $regex, $name ) = @_;
600 my $ctx = $self->ctx;
601 my ($ok, @diag) = tmt->regex_check($thing, $regex, '!~');
602 $ctx->ok($ok, $name, \@diag);
606 #################################
607 # }}} Advanced Event Producers #
608 #################################
610 ################################################
612 ################################################
616 my($file_or_fh) = shift;
618 return $file_or_fh if $self->is_fh($file_or_fh);
621 if( ref $file_or_fh eq 'SCALAR' ) {
622 open $fh, ">>", $file_or_fh
623 or croak("Can't open scalar ref $file_or_fh: $!");
626 open $fh, ">", $file_or_fh
627 or croak("Can't open test output log $file_or_fh: $!");
628 Test::Stream::IOSets->_autoflush($fh);
636 my $handles = $self->ctx->stream->io_sets->init_encoding('legacy');
637 $handles->[0] = $self->_new_fh(@_) if @_;
638 return $handles->[0];
643 my $handles = $self->ctx->stream->io_sets->init_encoding('legacy');
644 $handles->[1] = $self->_new_fh(@_) if @_;
645 return $handles->[1];
650 my $handles = $self->ctx->stream->io_sets->init_encoding('legacy');
651 $handles->[2] = $self->_new_fh(@_) if @_;
652 return $handles->[2] || $handles->[0];
657 my $ctx = $self->ctx;
658 $ctx->stream->io_sets->reset_legacy;
663 my $ctx = $self->ctx;
664 $ctx->stream->set_use_numbers(@_) if @_;
665 $ctx->stream->use_numbers;
670 my $ctx = $self->ctx;
671 $ctx->stream->set_no_ending(@_) if @_;
672 $ctx->stream->no_ending || 0;
677 my $ctx = $self->ctx;
678 $ctx->stream->set_no_header(@_) if @_;
679 $ctx->stream->no_header || 0;
684 my $ctx = $self->ctx;
685 $ctx->stream->set_no_diag(@_) if @_;
686 $ctx->stream->no_diag || 0;
690 my($self, $pack) = @_;
691 $self->{Exported_To} = $pack if defined $pack;
692 return $self->{Exported_To};
697 my $maybe_fh = shift;
698 return 0 unless defined $maybe_fh;
700 return 1 if ref $maybe_fh eq 'GLOB'; # its a glob ref
701 return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob
705 $out = eval { $maybe_fh->isa("IO::Handle") }
706 || eval { tied($maybe_fh)->can('TIEHANDLE') };
712 sub BAILOUT { goto &BAIL_OUT }
717 my $ctx = $self->ctx;
718 $ctx->plan(@_) if @_;
720 my $plan = $ctx->stream->state->[-1]->[STATE_PLAN] || return 0;
721 return $plan->max || 0;
724 sub caller { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
727 my $ctx = $self->ctx;
729 return wantarray ? $ctx->call : $ctx->package;
733 my( $self, $level ) = @_;
734 $Level = $level if defined $level;
739 my ($self, $regex) = @_;
740 return is_regex($regex);
745 my $ctx = $self->ctx;
746 $ctx->stream->is_passing(@_);
749 # Yeah, this is not efficient, but it is only legacy support, barely anything
750 # uses it, and they really should not.
754 my $ctx = $self->ctx;
758 my $state = $ctx->stream->state->[-1];
759 $state->[STATE_COUNT] = $num;
761 my $old = $state->[STATE_LEGACY] || [];
764 my $nctx = $ctx->snapshot;
765 $nctx->set_todo('incrementing test number');
766 $nctx->set_in_todo(1);
770 $i = shift @$old while @$old && (!$i || !$i->isa('Test::Stream::Event::Ok'));
771 $i ||= Test::Stream::Event::Ok->new(
784 $state->[STATE_LEGACY] = $new;
792 my $ctx = $self->ctx;
793 my $state = $ctx->stream->state->[-1];
795 return @out unless $state->[STATE_LEGACY];
797 for my $e (@{$state->[STATE_LEGACY]}) {
798 next unless $e && $e->isa('Test::Stream::Event::Ok');
799 push @out => $e->to_legacy;
807 my $ctx = $self->ctx;
808 my $state = $ctx->stream->state->[-1];
809 return @{[]} unless $state->[STATE_LEGACY];
810 return map { $_->isa('Test::Stream::Event::Ok') ? ($_->bool ? 1 : 0) : ()} @{$state->[STATE_LEGACY]};
813 ###################################
815 ###################################
821 # This is just a list of method Test::Builder current does not have that Test::Builder 1.5 does.
822 my %TB15_METHODS = map { $_ => 1 } qw{
823 _file_and_line _join_message _make_default _my_exit _reset_todo_state
824 _result_to_hash _results _todo_state formatter history in_test
825 no_change_exit_code post_event post_result set_formatter set_plan test_end
826 test_exit_code test_start test_state
832 $AUTOLOAD =~ m/^(.*)::([^:]+)$/;
833 my ($package, $sub) = ($1, $2);
835 my @caller = CORE::caller();
836 my $msg = qq{Can't locate object method "$sub" via package "$package" at $caller[1] line $caller[2].\n};
838 $msg .= <<" EOT" if $TB15_METHODS{$sub};
840 *************************************************************************
841 '$sub' is a Test::Builder 1.5 method. Test::Builder 1.5 is a dead branch.
842 You need to update your code so that it no longer treats Test::Builders
843 over a specific version number as anything special.
845 See: http://blogs.perl.org/users/chad_exodist_granum/2014/03/testmore---new-maintainer-also-stop-version-checking.html
846 *************************************************************************
864 Test::Builder - *DEPRECATED* Module for building testing libraries.
868 This module was previously the base module for almost any testing library. This
869 module is now little more than a compatability wrapper around L<Test::Stream>.
870 If you are looking to write or update a testing library you should look at
871 L<Test::Stream::Toolset>.
877 =item $Test::Builder::Test
879 The variable that holds the Test::Builder singleton.
881 =item $Test::Builder::Level
883 In the past this variable was used to track stack depth so that Test::Builder
884 could report the correct line number. If you use Test::Builder this will still
885 work, but in new code it is better to use the L<Test::Stream::Context> module.
895 =item Test::Builder->new
897 Returns the singleton stored in C<$Test::Builder::Test>.
899 =item Test::Builder->create
901 =item Test::Builder->create(use_shared => 1)
903 Returns a new instance of Test::Builder. It is important to note that this
904 instance will not use the shared L<Test::Stream> object unless you pass in the
905 C<< use_shared => 1 >> argument.
915 Helper method for Test::Builder to get a L<Test::Stream::Context> object.
919 Get the subtest depth
927 These all check on todo state and value
941 These let you figure out when/where the test is defined in the test file.
945 Start a subtest (Please do not use this)
949 Finish a subtest (Please do not use this)
953 Interface to Data::Dumper that dumps whatever you give it.
955 =item $TB->exported_to
957 This used to tell you what package used Test::Builder, it never worked well.
958 The previous bad and unpredictable behavior of this has largely been preserved,
959 however nothing internal uses it in any meaningful way anymore.
963 Check if something is a filehandle
967 Get/Set C<$Test::Builder::Level>. $Level is a package var, and most thigns
968 localize it, so this method is pretty useless.
970 =item $TB->maybe_regex
972 Check if something might be a regex.
976 Reset the builder object to a very basic and default state. You almost
977 certainly do not need this unless you are writing a tool to test testing
978 libraries. Even then you probably do not want this.
982 =item $TB->todo_start
984 Start/end TODO state, there are better ways to do this now.
988 =head2 STREAM INTERFACE
990 These simply interface into functionality of L<Test::Stream>.
994 =item $TB->failure_output
998 =item $TB->reset_outputs
1000 =item $TB->todo_output
1002 These get/set the IO handle used in the 'legacy' tap encoding.
1006 Do not display L<Test::Stream::Event::Diag> events.
1008 =item $TB->no_ending
1010 Do not do some special magic at the end that tells you what went wrong with
1013 =item $TB->no_header
1015 Do not display the plan
1017 =item $TB->use_numbers
1019 Turn numbers in TAP on and off.
1029 Get all the events that occured on this object. Each event will be transformed
1030 into a hash that matches the legacy output of this method.
1032 =item $TB->expected_tests
1034 Set/Get expected number of tests
1038 Check if there is a plan
1042 List of pass/fail results.
1046 =head2 EVENT GENERATORS
1048 See L<Test::Stream::Context>, L<Test::Stream::Toolset>, and
1049 L<Test::More::Tools>. Calling the methods below is not advised.
1059 =item $TB->current_test
1063 =item $TB->done_testing
1069 =item $TB->is_passing
1091 =item $TB->todo_skip
1103 Get the stream used by this builder (or the shared stream).
1111 Parent if this is a child.
1115 =head1 MONKEYPATCHING
1117 Many legacy testing modules monkeypatch C<ok()>, C<plan()>, and others. The
1118 abillity to monkeypatch these to effect all events of the specified type is now
1119 considered discouraged. For backwords compatability monkeypatching continues to
1120 work, however in the distant future it will be removed. L<Test::Stream> upon
1121 which Test::Builder is now built, provides hooks and API's for doing everything
1122 that previously required monkeypatching.
1130 =item L<Test::Tutorial>
1132 The original L<Test::Tutorial>. Uses comedy to introduce you to testing from
1135 =item L<Test::Tutorial::WritingTests>
1137 The L<Test::Tutorial::WritingTests> tutorial takes a more technical approach.
1138 The idea behind this tutorial is to give you a technical introduction to
1139 testing that can easily be used as a reference. This is for people who say
1140 "Just tell me how to do it, and quickly!".
1142 =item L<Test::Tutorial::WritingTools>
1144 The L<Test::Tutorial::WritingTools> tutorial is an introduction to writing
1145 testing tools that play nicely with other L<Test::Stream> and L<Test::Builder>
1146 based tools. This is what you should look at if you want to write
1153 The source code repository for Test::More can be found at
1154 F<http://github.com/Test-More/test-more/>.
1160 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
1166 The following people have all contributed to the Test-More dist (sorted using
1167 VIM's sort function).
1171 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
1173 =item Fergal Daly E<lt>fergal@esatclear.ie>E<gt>
1175 =item Mark Fowler E<lt>mark@twoshortplanks.comE<gt>
1177 =item Michael G Schwern E<lt>schwern@pobox.comE<gt>
1185 There has been a lot of code migration between modules,
1186 here are all the original copyrights together:
1192 =item Test::Stream::Tester
1194 Copyright 2014 Chad Granum E<lt>exodist7@gmail.comE<gt>.
1196 This program is free software; you can redistribute it and/or
1197 modify it under the same terms as Perl itself.
1199 See F<http://www.perl.com/perl/misc/Artistic.html>
1207 Originally authored by Michael G Schwern E<lt>schwern@pobox.comE<gt> with much
1208 inspiration from Joshua Pritikin's Test module and lots of help from Barrie
1209 Slaymaker, Tony Bowden, blackstar.co.uk, chromatic, Fergal Daly and the perl-qa
1212 Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
1213 E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
1215 Copyright 2001-2008 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
1217 This program is free software; you can redistribute it and/or
1218 modify it under the same terms as Perl itself.
1220 See F<http://www.perl.com/perl/misc/Artistic.html>
1224 To the extent possible under law, 唐鳳 has waived all copyright and related
1225 or neighboring rights to L<Test-use-ok>.
1227 This work is published from Taiwan.
1229 L<http://creativecommons.org/publicdomain/zero/1.0>
1233 This module is copyright 2005 Fergal Daly <fergal@esatclear.ie>, some parts
1234 are based on other people's work.
1236 Under the same license as Perl itself
1238 See http://www.perl.com/perl/misc/Artistic.html
1240 =item Test::Builder::Tester
1242 Copyright Mark Fowler E<lt>mark@twoshortplanks.comE<gt> 2002, 2004.
1244 This program is free software; you can redistribute it
1245 and/or modify it under the same terms as Perl itself.