📄 tester.pm
字号:
sub test_test{ # decode the arguements as described in the pod my $mess; my %args; if (@_ == 1) { $mess = shift } else { %args = @_; $mess = $args{name} if exists($args{name}); $mess = $args{title} if exists($args{title}); $mess = $args{label} if exists($args{label}); } # er, are we testing? croak "Not testing. You must declare output with a test function first." unless $testing; # okay, reconnect the test suite back to the saved handles $t->output($original_output_handle); $t->failure_output($original_failure_handle); $t->todo_output($original_todo_handle); # restore the test no, etc, back to the original point $t->current_test($testing_num); $testing = 0; # re-enable the original setting of the harness $ENV{HARNESS_ACTIVE} = $original_harness_env; # check the output we've stashed unless ($t->ok( ($args{skip_out} || $out->check) && ($args{skip_err} || $err->check), $mess)) { # print out the diagnostic information about why this # test failed local $_; $t->diag(map {"$_\n"} $out->complaint) unless $args{skip_out} || $out->check; $t->diag(map {"$_\n"} $err->complaint) unless $args{skip_err} || $err->check; }}=item line_numA utility function that returns the line number that the function wascalled on. You can pass it an offset which will be added to theresult. This is very useful for working out the correct text ofdiagnostic functions that contain line numbers.Essentially this is the same as the C<__LINE__> macro, but theC<line_num(+3)> idiom is arguably nicer.=cutsub line_num{ my ($package, $filename, $line) = caller; return $line + (shift() || 0); # prevent warnings}=backIn addition to the six exported functions there there exists onefunction that can only be accessed with a fully qualified functioncall.=over 4=item colorWhen C<test_test> is called and the output that your tests generatedoes not match that which you declared, C<test_test> will print outdebug information showing the two conflicting versions. As thisoutput itself is debug information it can be confusing which part ofthe output is from C<test_test> and which was the original output fromyour original tests. Also, it may be hard to spot things likeextraneous whitespace at the end of lines that may cause your test tofail even though the output looks similar.To assist you, if you have the B<Term::ANSIColor> module installed(which you should do by default from perl 5.005 onwards), C<test_test>can colour the background of the debug information to disambiguate thedifferent types of output. The debug output will have it's backgroundcoloured green and red. The green part represents the text which isthe same between the executed and actual output, the red shows whichpart differs.The C<color> function determines if colouring should occur or not.Passing it a true or false value will enable or disable colouringrespectively, and the function called with no argument will return thecurrent setting.To enable colouring from the command line, you can use theB<Text::Builder::Tester::Color> module like so: perl -Mlib=Text::Builder::Tester::Color test.tOr by including the B<Test::Builder::Tester::Color> module directly inthe PERL5LIB.=cutmy $color;sub color{ $color = shift if @_; $color;}=back=head1 BUGSCalls C<<Test::Builder->no_ending>> turning off the ending tests.This is needed as otherwise it will trip out because we've run moretests than we strictly should have and it'll register any failures wehad that we were testing for as real failures.The color function doesn't work unless B<Term::ANSIColor> is installedand is compatible with your terminal.Bugs (and requests for new features) can be reported to the authorthough the CPAN RT system:L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Builder-Tester>=head1 AUTHORCopyright Mark Fowler E<lt>mark@twoshortplanks.comE<gt> 2002, 2004.Some code taken from B<Test::More> and B<Test::Catch>, written by byMichael G Schwern E<lt>schwern@pobox.comE<gt>. Hence, those partsCopyright Micheal G Schwern 2001. Used and distributed withpermission.This program is free software; you can redistribute itand/or modify it under the same terms as Perl itself.=head1 NOTESThis code has been tested explicitly on the following versionsof perl: 5.7.3, 5.6.1, 5.6.0, 5.005_03, 5.004_05 and 5.004.Thanks to Richard Clamp E<lt>richardc@unixbeard.netE<gt> for lettingme use his testing system to try this module out on.=head1 SEE ALSOL<Test::Builder>, L<Test::Builder::Tester::Color>, L<Test::More>.=cut1;##################################################################### Helper class that is used to remember expected and received datapackage Test::Builder::Tester::Tie;### add line(s) to be expectedsub expect{ my $self = shift; my @checks = @_; foreach my $check (@checks) { $check = $self->_translate_Failed_check($check); push @{$self->{wanted}}, ref $check ? $check : "$check\n"; }}sub _translate_Failed_check{ my($self, $check) = @_; if( $check =~ /\A(.*)# (Failed .*test) \((.*?) at line (\d+)\)\Z(?!\n)/ ) { $check = "/\Q$1\E#\\s+\Q$2\E.*?\\n?.*?\Qat $3\E line \Q$4\E.*\\n?/"; } return $check;}### return true iff the expected data matches the got datasub check{ my $self = shift; # turn off warnings as these might be undef local $^W = 0; my @checks = @{$self->{wanted}}; my $got = $self->{got}; foreach my $check (@checks) { $check = "\Q$check\E" unless ($check =~ s,^/(.*)/$,$1, or ref $check); return 0 unless $got =~ s/^$check//; } return length $got == 0;}### a complaint message about the inputs not matching (to be# used for debugging messages)sub complaint{ my $self = shift; my $type = $self->type; my $got = $self->got; my $wanted = join "\n", @{$self->wanted}; # are we running in colour mode? if (Test::Builder::Tester::color) { # get color eval "require Term::ANSIColor"; unless ($@) { # colours my $green = Term::ANSIColor::color("black"). Term::ANSIColor::color("on_green"); my $red = Term::ANSIColor::color("black"). Term::ANSIColor::color("on_red"); my $reset = Term::ANSIColor::color("reset"); # work out where the two strings start to differ my $char = 0; $char++ while substr($got, $char, 1) eq substr($wanted, $char, 1); # get the start string and the two end strings my $start = $green . substr($wanted, 0, $char); my $gotend = $red . substr($got , $char) . $reset; my $wantedend = $red . substr($wanted, $char) . $reset; # make the start turn green on and off $start =~ s/\n/$reset\n$green/g; # make the ends turn red on and off $gotend =~ s/\n/$reset\n$red/g; $wantedend =~ s/\n/$reset\n$red/g; # rebuild the strings $got = $start . $gotend; $wanted = $start . $wantedend; } } return "$type is:\n" . "$got\nnot:\n$wanted\nas expected"}### forget all expected and got datasub reset{ my $self = shift; %$self = ( type => $self->{type}, got => '', wanted => [], );}sub got{ my $self = shift; return $self->{got};}sub wanted{ my $self = shift; return $self->{wanted};}sub type{ my $self = shift; return $self->{type};}#### tie interface###sub PRINT { my $self = shift; $self->{got} .= join '', @_;}sub TIEHANDLE { my($class, $type) = @_; my $self = bless { type => $type }, $class; $self->reset; return $self;}sub READ {}sub READLINE {}sub GETC {}sub FILENO {}1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -