7 our $VERSION = '1.301001_071';
8 $VERSION = eval $VERSION; ## no critic (BuiltinFunctions::ProhibitStringyEval)
11 use Test::Stream 1.301001 qw/ -internal STATE_LEGACY STATE_PLAN STATE_COUNT /;
12 use Test::Stream::Toolset;
13 use Test::Stream::Context;
14 use Test::Stream::Carp qw/confess/;
15 use Test::Stream::Meta qw/MODERN/;
17 use Test::Stream::Util qw/try protect unoverload_str is_regex/;
18 use Scalar::Util qw/blessed reftype/;
20 use Test::More::Tools;
23 my $meta = Test::Stream::Meta->is_tester('main');
24 Test::Stream->shared->set_use_legacy(1)
25 unless $meta && $meta->[MODERN];
28 # The mostly-singleton, and other package vars.
29 our $Test = Test::Builder->new;
30 our $_ORIG_Test = $Test;
34 my $self = shift || die "No self in context";
36 my $ctx = Test::Stream::Context::context(2 + ($add || 0), $self->{stream});
37 if (defined $self->{Todo}) {
39 $ctx->set_todo($self->{Todo});
40 $ctx->set_diag_todo(1);
47 return $self->{stream} || Test::Stream->shared;
50 sub depth { $_[0]->{depth} || 0 }
52 # This is only for unit tests at this point.
56 require Test::Stream::ExitMagic;
57 $self->{stream}->set_no_ending(0);
58 Test::Stream::ExitMagic->new->do_magic($self->{stream}, $ctx);
68 done_testing => \&done_testing,
71 sub WARN_OF_OVERRIDE {
74 return unless $ctx->modern;
75 my $old = $ORIG{$sub};
76 # Use package instead of self, we want replaced subs, not subclass overrides.
77 my $new = __PACKAGE__->can($sub);
79 return if $new == $old;
82 my $o = B::svref_2object($new);
86 my $pkg = $gv->STASH->NAME;
90 warn <<" EOT" unless $WARNED{"$pkg $name $file $line"}++;
92 *******************************************************************************
93 Something monkeypatched Test::Builder::$sub()!
94 The new sub is '$pkg\::$name' defined in $file around line $line.
95 In the near future monkeypatching Test::Builder::ok() will no longer work
97 *******************************************************************************
109 $Test ||= $class->create(shared_stream => 1);
118 my $self = bless {}, $class;
119 $self->reset(%params);
124 # Copy an object, currently a shallow.
125 # This does *not* bless the destination. This keeps the destructor from
126 # firing when we're just storing a copy of the object to restore later.
128 my ($src, $dest) = @_;
137 #############################
138 # {{{ Children and subtests #
139 #############################
143 my $ctx = $self->ctx();
144 return tmt->subtest(@_);
148 my( $self, $name ) = @_;
150 my $ctx = $self->ctx;
152 if ($self->{child}) {
153 my $cname = $self->{child}->{Name};
154 $ctx->throw("You already have a child named ($cname) running");
157 $name ||= "Child of " . $self->{Name};
158 $ctx->child('push', $name, 1);
160 my $stream = $self->{stream} || Test::Stream->shared;
169 $child->{Name} = $name;
170 $self->{child} = $child;
171 Scalar::Util::weaken($self->{child});
179 return unless $self->{parent};
181 my $ctx = $self->ctx;
183 if ($self->{child}) {
184 my $cname = $self->{child}->{Name};
185 $ctx->throw("Can't call finalize() with child ($cname) active");
188 $self->_ending($ctx);
189 my $passing = $ctx->stream->is_passing;
190 my $count = $ctx->stream->count;
191 my $name = $self->{Name};
194 my $stream = $self->{stream} || Test::Stream->shared;
196 my $parent = $self->parent;
197 $self->{parent}->{child} = undef;
198 $self->{parent} = undef;
203 $ctx->child('pop', $self->{Name});
208 my $ctx = $self->ctx;
209 return scalar @{$ctx->stream->subtests};
212 sub parent { $_[0]->{parent} }
213 sub name { $_[0]->{Name} }
217 return unless $self->{parent};
218 return if $self->{Skip_All};
219 $self->{parent}->is_passing(0);
220 my $name = $self->{Name};
221 die "Child ($name) exited without calling finalize()";
224 #############################
225 # }}} Children and subtests #
226 #############################
228 #####################################
229 # {{{ stuff for TODO status #
230 #####################################
233 my ($self, $pack, $set, $new_value) = @_;
236 if (my $ctx = Test::Stream::Context->peek) {
237 $pack = $ctx->package;
238 my $old = $ctx->todo;
239 $ctx->set_todo($new_value) if $set;
243 $pack = $self->exported_to || return;
246 no strict 'refs'; ## no critic
248 my $old_value = ${$pack . '::TODO'};
249 $set and ${$pack . '::TODO'} = $new_value;
254 my ($self, $pack) = @_;
256 return $self->{Todo} if defined $self->{Todo};
258 my $ctx = $self->ctx;
260 my $todo = $self->find_TODO($pack);
261 return $todo if defined $todo;
269 my $ctx = $self->ctx;
270 return 1 if $ctx->in_todo;
272 return (defined $self->{Todo} || $self->find_TODO) ? 1 : 0;
277 my $message = @_ ? shift : '';
279 $self->{Start_Todo}++;
280 if ($self->in_todo) {
281 push @{$self->{Todo_Stack}} => $self->todo;
283 $self->{Todo} = $message;
291 if (!$self->{Start_Todo}) {
292 $self->ctx(-1)->throw('todo_end() called without todo_start()');
295 $self->{Start_Todo}--;
297 if ($self->{Start_Todo} && @{$self->{Todo_Stack}}) {
298 $self->{Todo} = pop @{$self->{Todo_Stack}};
301 delete $self->{Todo};
307 #####################################
308 # }}} Finding Testers and Providers #
309 #####################################
316 no_plan => 'no_plan',
317 skip_all => 'skip_all',
318 tests => '_plan_tests',
322 my ($self, $cmd, @args) = @_;
324 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
325 WARN_OF_OVERRIDE(plan => $ctx);
329 if (my $method = $PLAN_CMDS{$cmd}) {
330 $self->$method(@args);
333 my @in = grep { defined } ($cmd, @args);
334 $self->ctx->throw("plan() doesn't understand @in");
341 my ($self, $reason) = @_;
343 $self->{Skip_All} = 1;
345 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
347 $ctx->_plan(0, 'SKIP', $reason);
351 my ($self, @args) = @_;
353 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
355 $ctx->alert("no_plan takes no arguments") if @args;
356 $ctx->_plan(0, 'NO PLAN');
362 my ($self, $arg) = @_;
364 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
367 $ctx->throw("Number of tests must be a positive integer. You gave it '$arg'")
368 unless $arg =~ /^\+?\d+$/;
372 elsif (!defined $arg) {
373 $ctx->throw("Got an undefined number of tests");
376 $ctx->throw("You said to run 0 tests");
383 my ($self, $num_tests) = @_;
385 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
386 WARN_OF_OVERRIDE(done_testing => $ctx);
388 my $out = $ctx->stream->done_testing($ctx, $num_tests);
396 #############################
397 # {{{ Base Event Producers #
398 #############################
402 my($test, $name) = @_;
404 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
405 WARN_OF_OVERRIDE(ok => $ctx);
407 if ($self->{child}) {
408 $self->is_passing(0);
409 $ctx->throw("Cannot run test ($name) with active children");
412 $ctx->_unwind_ok($test, $name);
413 return $test ? 1 : 0;
417 my( $self, $reason ) = @_;
418 $self->ctx()->bail($reason);
422 my( $self, $why ) = @_;
424 unoverload_str( \$why );
426 my $ctx = $self->ctx();
427 $ctx->set_skip($why);
429 $ctx->set_skip(undef);
433 my( $self, $why ) = @_;
435 unoverload_str( \$why );
437 my $ctx = $self->ctx();
438 $ctx->set_skip($why);
439 $ctx->set_todo($why);
441 $ctx->set_skip(undef);
442 $ctx->set_todo(undef);
447 my $msg = join '', map { defined($_) ? $_ : 'undef' } @_;
449 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
450 WARN_OF_OVERRIDE(diag => $ctx);
458 my $msg = join '', map { defined($_) ? $_ : 'undef' } @_;
460 my $ctx = $CTX || Test::Stream::Context->peek || $self->ctx();
461 WARN_OF_OVERRIDE(note => $ctx);
466 #############################
467 # }}} Base Event Producers #
468 #############################
470 #######################
471 # {{{ Public helpers #
472 #######################
480 protect { require Data::Dumper };
481 my $dumper = Data::Dumper->new( [$_] );
482 $dumper->Indent(1)->Terse(1);
483 $dumper->Sortkeys(1) if $dumper->can("Sortkeys");
492 $self->ctx->alert(join '' => @_);
497 $self->ctx->throw(join '' => @_);
503 my $plan = $self->ctx->stream->plan || return undef;
504 return 'no_plan' if $plan->directive && $plan->directive eq 'NO PLAN';
512 $self->{use_shared} = 1 if $params{shared_stream};
514 if ($self->{use_shared}) {
515 Test::Stream->shared->_reset;
516 Test::Stream->shared->state->[-1]->[STATE_LEGACY] = [];
519 $self->{stream} = Test::Stream->new();
520 $self->{stream}->set_use_legacy(1);
521 $self->{stream}->state->[-1]->[STATE_LEGACY] = [];
524 # We leave this a global because it has to be localized and localizing
525 # hash keys is just asking for pain. Also, it was documented.
530 $self->{Original_Pid} = $$;
531 $self->{Child_Name} = undef;
533 $self->{Exported_To} = undef;
535 $self->{Todo} = undef;
536 $self->{Todo_Stack} = [];
537 $self->{Start_Todo} = 0;
538 $self->{Opened_Testhandles} = 0;
543 #######################
544 # }}} Public helpers #
545 #######################
547 #################################
548 # {{{ Advanced Event Producers #
549 #################################
552 my( $self, $got, $type, $expect, $name ) = @_;
553 my $ctx = $self->ctx;
554 my ($ok, @diag) = tmt->cmp_check($got, $type, $expect);
555 $ctx->ok($ok, $name, \@diag);
560 my( $self, $got, $expect, $name ) = @_;
561 my $ctx = $self->ctx;
562 my ($ok, @diag) = tmt->is_eq($got, $expect);
563 $ctx->ok($ok, $name, \@diag);
568 my( $self, $got, $expect, $name ) = @_;
569 my $ctx = $self->ctx;
570 my ($ok, @diag) = tmt->is_num($got, $expect);
571 $ctx->ok($ok, $name, \@diag);
576 my( $self, $got, $dont_expect, $name ) = @_;
577 my $ctx = $self->ctx;
578 my ($ok, @diag) = tmt->isnt_eq($got, $dont_expect);
579 $ctx->ok($ok, $name, \@diag);
584 my( $self, $got, $dont_expect, $name ) = @_;
585 my $ctx = $self->ctx;
586 my ($ok, @diag) = tmt->isnt_num($got, $dont_expect);
587 $ctx->ok($ok, $name, \@diag);
592 my( $self, $thing, $regex, $name ) = @_;
593 my $ctx = $self->ctx;
594 my ($ok, @diag) = tmt->regex_check($thing, $regex, '=~');
595 $ctx->ok($ok, $name, \@diag);
600 my( $self, $thing, $regex, $name ) = @_;
601 my $ctx = $self->ctx;
602 my ($ok, @diag) = tmt->regex_check($thing, $regex, '!~');
603 $ctx->ok($ok, $name, \@diag);
607 #################################
608 # }}} Advanced Event Producers #
609 #################################
611 ################################################
613 ################################################
617 my($file_or_fh) = shift;
619 return $file_or_fh if $self->is_fh($file_or_fh);
622 if( ref $file_or_fh eq 'SCALAR' ) {
623 open $fh, ">>", $file_or_fh
624 or croak("Can't open scalar ref $file_or_fh: $!");
627 open $fh, ">", $file_or_fh
628 or croak("Can't open test output log $file_or_fh: $!");
629 Test::Stream::IOSets->_autoflush($fh);
637 my $handles = $self->ctx->stream->io_sets->init_encoding('legacy');
638 $handles->[0] = $self->_new_fh(@_) if @_;
639 return $handles->[0];
644 my $handles = $self->ctx->stream->io_sets->init_encoding('legacy');
645 $handles->[1] = $self->_new_fh(@_) if @_;
646 return $handles->[1];
651 my $handles = $self->ctx->stream->io_sets->init_encoding('legacy');
652 $handles->[2] = $self->_new_fh(@_) if @_;
653 return $handles->[2] || $handles->[0];
658 my $ctx = $self->ctx;
659 $ctx->stream->io_sets->reset_legacy;
664 my $ctx = $self->ctx;
665 $ctx->stream->set_use_numbers(@_) if @_;
666 $ctx->stream->use_numbers;
671 my $ctx = $self->ctx;
672 $ctx->stream->set_no_ending(@_) if @_;
673 $ctx->stream->no_ending || 0;
678 my $ctx = $self->ctx;
679 $ctx->stream->set_no_header(@_) if @_;
680 $ctx->stream->no_header || 0;
685 my $ctx = $self->ctx;
686 $ctx->stream->set_no_diag(@_) if @_;
687 $ctx->stream->no_diag || 0;
691 my($self, $pack) = @_;
692 $self->{Exported_To} = $pack if defined $pack;
693 return $self->{Exported_To};
698 my $maybe_fh = shift;
699 return 0 unless defined $maybe_fh;
701 return 1 if ref $maybe_fh eq 'GLOB'; # its a glob ref
702 return 1 if ref \$maybe_fh eq 'GLOB'; # its a glob
706 $out = eval { $maybe_fh->isa("IO::Handle") }
707 || eval { tied($maybe_fh)->can('TIEHANDLE') };
713 sub BAILOUT { goto &BAIL_OUT }
718 my $ctx = $self->ctx;
719 $ctx->plan(@_) if @_;
721 my $plan = $ctx->stream->state->[-1]->[STATE_PLAN] || return 0;
722 return $plan->max || 0;
725 sub caller { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
728 my $ctx = $self->ctx;
730 return wantarray ? $ctx->call : $ctx->package;
734 my( $self, $level ) = @_;
735 $Level = $level if defined $level;
740 my ($self, $regex) = @_;
741 return is_regex($regex);
746 my $ctx = $self->ctx;
747 $ctx->stream->is_passing(@_);
750 # Yeah, this is not efficient, but it is only legacy support, barely anything
751 # uses it, and they really should not.
755 my $ctx = $self->ctx;
759 my $state = $ctx->stream->state->[-1];
760 $state->[STATE_COUNT] = $num;
762 my $old = $state->[STATE_LEGACY] || [];
765 my $nctx = $ctx->snapshot;
766 $nctx->set_todo('incrementing test number');
767 $nctx->set_in_todo(1);
771 $i = shift @$old while @$old && (!$i || !$i->isa('Test::Stream::Event::Ok'));
772 $i ||= Test::Stream::Event::Ok->new(
785 $state->[STATE_LEGACY] = $new;
793 my $ctx = $self->ctx;
794 my $state = $ctx->stream->state->[-1];
796 return @out unless $state->[STATE_LEGACY];
798 for my $e (@{$state->[STATE_LEGACY]}) {
799 next unless $e && $e->isa('Test::Stream::Event::Ok');
800 push @out => $e->to_legacy;
808 my $ctx = $self->ctx;
809 my $state = $ctx->stream->state->[-1];
810 return @{[]} unless $state->[STATE_LEGACY];
811 return map { $_->isa('Test::Stream::Event::Ok') ? ($_->bool ? 1 : 0) : ()} @{$state->[STATE_LEGACY]};
814 ###################################
816 ###################################
822 # This is just a list of method Test::Builder current does not have that Test::Builder 1.5 does.
823 my %TB15_METHODS = map { $_ => 1 } qw{
824 _file_and_line _join_message _make_default _my_exit _reset_todo_state
825 _result_to_hash _results _todo_state formatter history in_test
826 no_change_exit_code post_event post_result set_formatter set_plan test_end
827 test_exit_code test_start test_state
833 $AUTOLOAD =~ m/^(.*)::([^:]+)$/;
834 my ($package, $sub) = ($1, $2);
836 my @caller = CORE::caller();
837 my $msg = qq{Can't locate object method "$sub" via package "$package" at $caller[1] line $caller[2].\n};
839 $msg .= <<" EOT" if $TB15_METHODS{$sub};
841 *************************************************************************
842 '$sub' is a Test::Builder 1.5 method. Test::Builder 1.5 is a dead branch.
843 You need to update your code so that it no longer treats Test::Builders
844 over a specific version number as anything special.
846 See: http://blogs.perl.org/users/chad_exodist_granum/2014/03/testmore---new-maintainer-also-stop-version-checking.html
847 *************************************************************************
865 Test::Builder - *DEPRECATED* Module for building testing libraries.
869 This module was previously the base module for almost any testing library. This
870 module is now little more than a compatability wrapper around L<Test::Stream>.
871 If you are looking to write or update a testing library you should look at
872 L<Test::Stream::Toolset>.
878 =item $Test::Builder::Test
880 The variable that holds the Test::Builder singleton.
882 =item $Test::Builder::Level
884 In the past this variable was used to track stack depth so that Test::Builder
885 could report the correct line number. If you use Test::Builder this will still
886 work, but in new code it is better to use the L<Test::Stream::Context> module.
896 =item Test::Builder->new
898 Returns the singleton stored in C<$Test::Builder::Test>.
900 =item Test::Builder->create
902 =item Test::Builder->create(use_shared => 1)
904 Returns a new instance of Test::Builder. It is important to note that this
905 instance will not use the shared L<Test::Stream> object unless you pass in the
906 C<< use_shared => 1 >> argument.
916 Helper method for Test::Builder to get a L<Test::Stream::Context> object.
920 Get the subtest depth
928 These all check on todo state and value
942 These let you figure out when/where the test is defined in the test file.
946 Start a subtest (Please do not use this)
950 Finish a subtest (Please do not use this)
954 Interface to Data::Dumper that dumps whatever you give it.
956 =item $TB->exported_to
958 This used to tell you what package used Test::Builder, it never worked well.
959 The previous bad and unpredictable behavior of this has largely been preserved,
960 however nothing internal uses it in any meaningful way anymore.
964 Check if something is a filehandle
968 Get/Set C<$Test::Builder::Level>. $Level is a package var, and most thigns
969 localize it, so this method is pretty useless.
971 =item $TB->maybe_regex
973 Check if something might be a regex.
977 Reset the builder object to a very basic and default state. You almost
978 certainly do not need this unless you are writing a tool to test testing
979 libraries. Even then you probably do not want this.
983 =item $TB->todo_start
985 Start/end TODO state, there are better ways to do this now.
989 =head2 STREAM INTERFACE
991 These simply interface into functionality of L<Test::Stream>.
995 =item $TB->failure_output
999 =item $TB->reset_outputs
1001 =item $TB->todo_output
1003 These get/set the IO handle used in the 'legacy' tap encoding.
1007 Do not display L<Test::Stream::Event::Diag> events.
1009 =item $TB->no_ending
1011 Do not do some special magic at the end that tells you what went wrong with
1014 =item $TB->no_header
1016 Do not display the plan
1018 =item $TB->use_numbers
1020 Turn numbers in TAP on and off.
1030 Get all the events that occured on this object. Each event will be transformed
1031 into a hash that matches the legacy output of this method.
1033 =item $TB->expected_tests
1035 Set/Get expected number of tests
1039 Check if there is a plan
1043 List of pass/fail results.
1047 =head2 EVENT GENERATORS
1049 See L<Test::Stream::Context>, L<Test::Stream::Toolset>, and
1050 L<Test::More::Tools>. Calling the methods below is not advised.
1060 =item $TB->current_test
1064 =item $TB->done_testing
1070 =item $TB->is_passing
1092 =item $TB->todo_skip
1104 Get the stream used by this builder (or the shared stream).
1112 Parent if this is a child.
1116 =head1 MONKEYPATCHING
1118 Many legacy testing modules monkeypatch C<ok()>, C<plan()>, and others. The
1119 abillity to monkeypatch these to effect all events of the specified type is now
1120 considered discouraged. For backwords compatability monkeypatching continues to
1121 work, however in the distant future it will be removed. L<Test::Stream> upon
1122 which Test::Builder is now built, provides hooks and API's for doing everything
1123 that previously required monkeypatching.
1131 =item L<Test::Tutorial>
1133 The original L<Test::Tutorial>. Uses comedy to introduce you to testing from
1136 =item L<Test::Tutorial::WritingTests>
1138 The L<Test::Tutorial::WritingTests> tutorial takes a more technical approach.
1139 The idea behind this tutorial is to give you a technical introduction to
1140 testing that can easily be used as a reference. This is for people who say
1141 "Just tell me how to do it, and quickly!".
1143 =item L<Test::Tutorial::WritingTools>
1145 The L<Test::Tutorial::WritingTools> tutorial is an introduction to writing
1146 testing tools that play nicely with other L<Test::Stream> and L<Test::Builder>
1147 based tools. This is what you should look at if you want to write
1154 The source code repository for Test::More can be found at
1155 F<http://github.com/Test-More/test-more/>.
1161 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
1167 The following people have all contributed to the Test-More dist (sorted using
1168 VIM's sort function).
1172 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
1174 =item Fergal Daly E<lt>fergal@esatclear.ie>E<gt>
1176 =item Mark Fowler E<lt>mark@twoshortplanks.comE<gt>
1178 =item Michael G Schwern E<lt>schwern@pobox.comE<gt>
1186 There has been a lot of code migration between modules,
1187 here are all the original copyrights together:
1193 =item Test::Stream::Tester
1195 Copyright 2014 Chad Granum E<lt>exodist7@gmail.comE<gt>.
1197 This program is free software; you can redistribute it and/or
1198 modify it under the same terms as Perl itself.
1200 See F<http://www.perl.com/perl/misc/Artistic.html>
1208 Originally authored by Michael G Schwern E<lt>schwern@pobox.comE<gt> with much
1209 inspiration from Joshua Pritikin's Test module and lots of help from Barrie
1210 Slaymaker, Tony Bowden, blackstar.co.uk, chromatic, Fergal Daly and the perl-qa
1213 Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern
1214 E<lt>schwern@pobox.comE<gt>, wardrobe by Calvin Klein.
1216 Copyright 2001-2008 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
1218 This program is free software; you can redistribute it and/or
1219 modify it under the same terms as Perl itself.
1221 See F<http://www.perl.com/perl/misc/Artistic.html>
1225 To the extent possible under law, 唐鳳 has waived all copyright and related
1226 or neighboring rights to L<Test-use-ok>.
1228 This work is published from Taiwan.
1230 L<http://creativecommons.org/publicdomain/zero/1.0>
1234 This module is copyright 2005 Fergal Daly <fergal@esatclear.ie>, some parts
1235 are based on other people's work.
1237 Under the same license as Perl itself
1239 See http://www.perl.com/perl/misc/Artistic.html
1241 =item Test::Builder::Tester
1243 Copyright Mark Fowler E<lt>mark@twoshortplanks.comE<gt> 2002, 2004.
1245 This program is free software; you can redistribute it
1246 and/or modify it under the same terms as Perl itself.