7 our $VERSION = '1.301001_068';
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 return (defined $self->{Todo} || $self->find_TODO) ? 1 : 0;
273 my $message = @_ ? shift : '';
275 $self->{Start_Todo}++;
276 if ($self->in_todo) {
277 push @{$self->{Todo_Stack}} => $self->todo;
279 $self->{Todo} = $message;
287 if (!$self->{Start_Todo}) {
288 $self->ctx(-1)->throw('todo_end() called without todo_start()');
291 $self->{Start_Todo}--;
293 if ($self->{Start_Todo} && @{$self->{Todo_Stack}}) {
294 $self->{Todo} = pop @{$self->{Todo_Stack}};
297 delete $self->{Todo};
303 #####################################
304 # }}} Finding Testers and Providers #
305 #####################################
312 no_plan => 'no_plan',
313 skip_all => 'skip_all',
314 tests => '_plan_tests',
318 my ($self, $cmd, @args) = @_;
320 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
321 WARN_OF_OVERRIDE(plan => $ctx);
325 if (my $method = $PLAN_CMDS{$cmd}) {
326 $self->$method(@args);
329 my @in = grep { defined } ($cmd, @args);
330 $self->ctx->throw("plan() doesn't understand @in");
337 my ($self, $reason) = @_;
339 $self->{Skip_All} = 1;
341 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
343 $ctx->_plan(0, 'SKIP', $reason);
347 my ($self, @args) = @_;
349 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
351 $ctx->alert("no_plan takes no arguments") if @args;
352 $ctx->_plan(0, 'NO PLAN');
358 my ($self, $arg) = @_;
360 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
363 $ctx->throw("Number of tests must be a positive integer. You gave it '$arg'")
364 unless $arg =~ /^\+?\d+$/;
368 elsif (!defined $arg) {
369 $ctx->throw("Got an undefined number of tests");
372 $ctx->throw("You said to run 0 tests");
379 my ($self, $num_tests) = @_;
381 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
382 WARN_OF_OVERRIDE(done_testing => $ctx);
384 my $out = $ctx->stream->done_testing($ctx, $num_tests);
392 #############################
393 # {{{ Base Event Producers #
394 #############################
398 my($test, $name) = @_;
400 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
401 WARN_OF_OVERRIDE(ok => $ctx);
403 if ($self->{child}) {
404 $self->is_passing(0);
405 $ctx->throw("Cannot run test ($name) with active children");
408 $ctx->_unwind_ok($test, $name);
409 return $test ? 1 : 0;
413 my( $self, $reason ) = @_;
414 $self->ctx()->bail($reason);
418 my( $self, $why ) = @_;
420 unoverload_str( \$why );
422 my $ctx = $self->ctx();
423 $ctx->set_skip($why);
425 $ctx->set_skip(undef);
429 my( $self, $why ) = @_;
431 unoverload_str( \$why );
433 my $ctx = $self->ctx();
434 $ctx->set_skip($why);
435 $ctx->set_todo($why);
437 $ctx->set_skip(undef);
438 $ctx->set_todo(undef);
443 my $msg = join '', map { defined($_) ? $_ : 'undef' } @_;
445 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
446 WARN_OF_OVERRIDE(diag => $ctx);
454 my $msg = join '', map { defined($_) ? $_ : 'undef' } @_;
456 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
457 WARN_OF_OVERRIDE(note => $ctx);
462 #############################
463 # }}} Base Event Producers #
464 #############################
466 #######################
467 # {{{ Public helpers #
468 #######################
476 protect { require Data::Dumper };
477 my $dumper = Data::Dumper->new( [$_] );
478 $dumper->Indent(1)->Terse(1);
479 $dumper->Sortkeys(1) if $dumper->can("Sortkeys");
488 $self->ctx->alert(join '' => @_);
493 $self->ctx->throw(join '' => @_);
499 my $plan = $self->ctx->stream->plan || return undef;
500 return 'no_plan' if $plan->directive && $plan->directive eq 'NO PLAN';
508 $self->{use_shared} = 1 if $params{shared_stream};
510 if ($self->{use_shared}) {
511 Test::Stream->shared->_reset;
512 Test::Stream->shared->state->[-1]->[STATE_LEGACY] = [];
515 $self->{stream} = Test::Stream->new();
516 $self->{stream}->set_use_legacy(1);
517 $self->{stream}->state->[-1]->[STATE_LEGACY] = [];
520 # We leave this a global because it has to be localized and localizing
521 # hash keys is just asking for pain. Also, it was documented.
526 $self->{Original_Pid} = $$;
527 $self->{Child_Name} = undef;
529 $self->{Exported_To} = undef;
531 $self->{Todo} = undef;
532 $self->{Todo_Stack} = [];
533 $self->{Start_Todo} = 0;
534 $self->{Opened_Testhandles} = 0;
539 #######################
540 # }}} Public helpers #
541 #######################
543 #################################
544 # {{{ Advanced Event Producers #
545 #################################
548 my( $self, $got, $type, $expect, $name ) = @_;
549 my $ctx = $self->ctx;
550 my ($ok, @diag) = tmt->cmp_check($got, $type, $expect);
551 $ctx->ok($ok, $name, \@diag);
556 my( $self, $got, $expect, $name ) = @_;
557 my $ctx = $self->ctx;
558 my ($ok, @diag) = tmt->is_eq($got, $expect);
559 $ctx->ok($ok, $name, \@diag);
564 my( $self, $got, $expect, $name ) = @_;
565 my $ctx = $self->ctx;
566 my ($ok, @diag) = tmt->is_num($got, $expect);
567 $ctx->ok($ok, $name, \@diag);
572 my( $self, $got, $dont_expect, $name ) = @_;
573 my $ctx = $self->ctx;
574 my ($ok, @diag) = tmt->isnt_eq($got, $dont_expect);
575 $ctx->ok($ok, $name, \@diag);
580 my( $self, $got, $dont_expect, $name ) = @_;
581 my $ctx = $self->ctx;
582 my ($ok, @diag) = tmt->isnt_num($got, $dont_expect);
583 $ctx->ok($ok, $name, \@diag);
588 my( $self, $thing, $regex, $name ) = @_;
589 my $ctx = $self->ctx;
590 my ($ok, @diag) = tmt->regex_check($thing, $regex, '=~');
591 $ctx->ok($ok, $name, \@diag);
596 my( $self, $thing, $regex, $name ) = @_;
597 my $ctx = $self->ctx;
598 my ($ok, @diag) = tmt->regex_check($thing, $regex, '!~');
599 $ctx->ok($ok, $name, \@diag);
603 #################################
604 # }}} Advanced Event Producers #
605 #################################
607 ################################################
609 ################################################
613 my($file_or_fh) = shift;
615 return $file_or_fh if $self->is_fh($file_or_fh);
618 if( ref $file_or_fh eq 'SCALAR' ) {
619 open $fh, ">>", $file_or_fh
620 or croak("Can't open scalar ref $file_or_fh: $!");
623 open $fh, ">", $file_or_fh
624 or croak("Can't open test output log $file_or_fh: $!");
625 Test::Stream::IOSets->_autoflush($fh);
633 my $handles = $self->ctx->stream->io_sets->init_encoding('legacy');
634 $handles->[0] = $self->_new_fh(@_) if @_;
635 return $handles->[0];
640 my $handles = $self->ctx->stream->io_sets->init_encoding('legacy');
641 $handles->[1] = $self->_new_fh(@_) if @_;
642 return $handles->[1];
647 my $handles = $self->ctx->stream->io_sets->init_encoding('legacy');
648 $handles->[2] = $self->_new_fh(@_) if @_;
649 return $handles->[2] || $handles->[0];
654 my $ctx = $self->ctx;
655 $ctx->stream->io_sets->reset_legacy;
660 my $ctx = $self->ctx;
661 $ctx->stream->set_use_numbers(@_) if @_;
662 $ctx->stream->use_numbers;
667 my $ctx = $self->ctx;
668 $ctx->stream->set_no_ending(@_) if @_;
669 $ctx->stream->no_ending || 0;
674 my $ctx = $self->ctx;
675 $ctx->stream->set_no_header(@_) if @_;
676 $ctx->stream->no_header || 0;
681 my $ctx = $self->ctx;
682 $ctx->stream->set_no_diag(@_) if @_;
683 $ctx->stream->no_diag || 0;
687 my($self, $pack) = @_;
688 $self->{Exported_To} = $pack if defined $pack;
689 return $self->{Exported_To};
694 my $maybe_fh = shift;
695 return 0 unless defined $maybe_fh;
697 return 1 if ref $maybe_fh eq 'GLOB'; # its a glob ref
698 return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob
702 $out = eval { $maybe_fh->isa("IO::Handle") }
703 || eval { tied($maybe_fh)->can('TIEHANDLE') };
709 sub BAILOUT { goto &BAIL_OUT }
714 my $ctx = $self->ctx;
715 $ctx->plan(@_) if @_;
717 my $plan = $ctx->stream->state->[-1]->[STATE_PLAN] || return 0;
718 return $plan->max || 0;
721 sub caller { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
724 my $ctx = $self->ctx;
726 return wantarray ? $ctx->call : $ctx->package;
730 my( $self, $level ) = @_;
731 $Level = $level if defined $level;
736 my ($self, $regex) = @_;
737 return is_regex($regex);
742 my $ctx = $self->ctx;
743 $ctx->stream->is_passing(@_);
746 # Yeah, this is not efficient, but it is only legacy support, barely anything
747 # uses it, and they really should not.
751 my $ctx = $self->ctx;
755 my $state = $ctx->stream->state->[-1];
756 $state->[STATE_COUNT] = $num;
758 my $old = $state->[STATE_LEGACY] || [];
761 my $nctx = $ctx->snapshot;
762 $nctx->set_todo('incrementing test number');
763 $nctx->set_in_todo(1);
767 $i = shift @$old while @$old && (!$i || !$i->isa('Test::Stream::Event::Ok'));
768 $i ||= Test::Stream::Event::Ok->new(
781 $state->[STATE_LEGACY] = $new;
789 my $ctx = $self->ctx;
790 my $state = $ctx->stream->state->[-1];
792 return @out unless $state->[STATE_LEGACY];
794 for my $e (@{$state->[STATE_LEGACY]}) {
795 next unless $e && $e->isa('Test::Stream::Event::Ok');
796 push @out => $e->to_legacy;
804 my $ctx = $self->ctx;
805 my $state = $ctx->stream->state->[-1];
806 return @{[]} unless $state->[STATE_LEGACY];
807 return map { $_->isa('Test::Stream::Event::Ok') ? ($_->bool ? 1 : 0) : ()} @{$state->[STATE_LEGACY]};
810 ###################################
812 ###################################
818 # This is just a list of method Test::Builder current does not have that Test::Builder 1.5 does.
819 my %TB15_METHODS = map { $_ => 1 } qw{
820 _file_and_line _join_message _make_default _my_exit _reset_todo_state
821 _result_to_hash _results _todo_state formatter history in_test
822 no_change_exit_code post_event post_result set_formatter set_plan test_end
823 test_exit_code test_start test_state
829 $AUTOLOAD =~ m/^(.*)::([^:]+)$/;
830 my ($package, $sub) = ($1, $2);
832 my @caller = CORE::caller();
833 my $msg = qq{Can't locate object method "$sub" via package "$package" at $caller[1] line $caller[2].\n};
835 $msg .= <<" EOT" if $TB15_METHODS{$sub};
837 *************************************************************************
838 '$sub' is a Test::Builder 1.5 method. Test::Builder 1.5 is a dead branch.
839 You need to update your code so that it no longer treats Test::Builders
840 over a specific version number as anything special.
842 See: http://blogs.perl.org/users/chad_exodist_granum/2014/03/testmore---new-maintainer-also-stop-version-checking.html
843 *************************************************************************
861 Test::Builder - *DEPRECATED* Module for building testing libraries.
865 This module was previously the base module for almost any testing library. This
866 module is now little more than a compatability wrapper around L<Test::Stream>.
867 If you are looking to write or update a testing library you should look at
868 L<Test::Stream::Toolset>.
874 =item $Test::Builder::Test
876 The variable that holds the Test::Builder singleton.
878 =item $Test::Builder::Level
880 In the past this variable was used to track stack depth so that Test::Builder
881 could report the correct line number. If you use Test::Builder this will still
882 work, but in new code it is better to use the L<Test::Stream::Context> module.
892 =item Test::Builder->new
894 Returns the singleton stored in C<$Test::Builder::Test>.
896 =item Test::Builder->create
898 =item Test::Builder->create(use_shared => 1)
900 Returns a new instance of Test::Builder. It is important to note that this
901 instance will not use the shared L<Test::Stream> object unless you pass in the
902 C<< use_shared => 1 >> argument.
912 Helper method for Test::Builder to get a L<Test::Stream::Context> object.
916 Get the subtest depth
924 These all check on todo state and value
938 These let you figure out when/where the test is defined in the test file.
942 Start a subtest (Please do not use this)
946 Finish a subtest (Please do not use this)
950 Interface to Data::Dumper that dumps whatever you give it.
952 =item $TB->exported_to
954 This used to tell you what package used Test::Builder, it never worked well.
955 The previous bad and unpredictable behavior of this has largely been preserved,
956 however nothing internal uses it in any meaningful way anymore.
960 Check if something is a filehandle
964 Get/Set C<$Test::Builder::Level>. $Level is a package var, and most thigns
965 localize it, so this method is pretty useless.
967 =item $TB->maybe_regex
969 Check if something might be a regex.
973 Reset the builder object to a very basic and default state. You almost
974 certainly do not need this unless you are writing a tool to test testing
975 libraries. Even then you probably do not want this.
979 =item $TB->todo_start
981 Start/end TODO state, there are better ways to do this now.
985 =head2 STREAM INTERFACE
987 These simply interface into functionality of L<Test::Stream>.
991 =item $TB->failure_output
995 =item $TB->reset_outputs
997 =item $TB->todo_output
999 These get/set the IO handle used in the 'legacy' tap encoding.
1003 Do not display L<Test::Stream::Event::Diag> events.
1005 =item $TB->no_ending
1007 Do not do some special magic at the end that tells you what went wrong with
1010 =item $TB->no_header
1012 Do not display the plan
1014 =item $TB->use_numbers
1016 Turn numbers in TAP on and off.
1026 Get all the events that occured on this object. Each event will be transformed
1027 into a hash that matches the legacy output of this method.
1029 =item $TB->expected_tests
1031 Set/Get expected number of tests
1035 Check if there is a plan
1039 List of pass/fail results.
1043 =head2 EVENT GENERATORS
1045 See L<Test::Stream::Context>, L<Test::Stream::Toolset>, and
1046 L<Test::More::Tools>. Calling the methods below is not advised.
1056 =item $TB->current_test
1060 =item $TB->done_testing
1066 =item $TB->is_passing
1088 =item $TB->todo_skip
1100 Get the stream used by this builder (or the shared stream).
1108 Parent if this is a child.
1112 =head1 MONKEYPATCHING
1114 Many legacy testing modules monkeypatch C<ok()>, C<plan()>, and others. The
1115 abillity to monkeypatch these to effect all events of the specified type is now
1116 considered discouraged. For backwords compatability monkeypatching continues to
1117 work, however in the distant future it will be removed. L<Test::Stream> upon
1118 which Test::Builder is now built, provides hooks and API's for doing everything
1119 that previously required monkeypatching.
1127 =item L<Test::Tutorial>
1129 The original L<Test::Tutorial>. Uses comedy to introduce you to testing from
1132 =item L<Test::Tutorial::WritingTests>
1134 The L<Test::Tutorial::WritingTests> tutorial takes a more technical approach.
1135 The idea behind this tutorial is to give you a technical introduction to
1136 testing that can easily be used as a reference. This is for people who say
1137 "Just tell me how to do it, and quickly!".
1139 =item L<Test::Tutorial::WritingTools>
1141 The L<Test::Tutorial::WritingTools> tutorial is an introduction to writing
1142 testing tools that play nicely with other L<Test::Stream> and L<Test::Builder>
1143 based tools. This is what you should look at if you want to write
1150 The source code repository for Test::More can be found at
1151 F<http://github.com/Test-More/test-more/>.
1157 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
1163 The following people have all contributed to the Test-More dist (sorted using
1164 VIM's sort function).
1168 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
1170 =item Fergal Daly E<lt>fergal@esatclear.ie>E<gt>
1172 =item Mark Fowler E<lt>mark@twoshortplanks.comE<gt>
1174 =item Michael G Schwern E<lt>schwern@pobox.comE<gt>
1182 There has been a lot of code migration between modules,
1183 here are all the original copyrights together:
1189 =item Test::Stream::Tester
1191 Copyright 2014 Chad Granum E<lt>exodist7@gmail.comE<gt>.
1193 This program is free software; you can redistribute it and/or
1194 modify it under the same terms as Perl itself.
1196 See F<http://www.perl.com/perl/misc/Artistic.html>
1204 Originally authored by Michael G Schwern E<lt>schwern@pobox.comE<gt> with much
1205 inspiration from Joshua Pritikin's Test module and lots of help from Barrie
1206 Slaymaker, Tony Bowden, blackstar.co.uk, chromatic, Fergal Daly and the perl-qa
1209 Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
1210 E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
1212 Copyright 2001-2008 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
1214 This program is free software; you can redistribute it and/or
1215 modify it under the same terms as Perl itself.
1217 See F<http://www.perl.com/perl/misc/Artistic.html>
1221 To the extent possible under law, 唐鳳 has waived all copyright and related
1222 or neighboring rights to L<Test-use-ok>.
1224 This work is published from Taiwan.
1226 L<http://creativecommons.org/publicdomain/zero/1.0>
1230 This module is copyright 2005 Fergal Daly <fergal@esatclear.ie>, some parts
1231 are based on other people's work.
1233 Under the same license as Perl itself
1235 See http://www.perl.com/perl/misc/Artistic.html
1237 =item Test::Builder::Tester
1239 Copyright Mark Fowler E<lt>mark@twoshortplanks.comE<gt> 2002, 2004.
1241 This program is free software; you can redistribute it
1242 and/or modify it under the same terms as Perl itself.