📄 builder.pm
字号:
Like Test::More's isnt(). Checks if $got ne $dont_expect. This isthe string version.=item B<isnt_num> $Test->is_num($got, $dont_expect, $name);Like Test::More's isnt(). Checks if $got ne $dont_expect. This isthe numeric version.=cutsub isnt_eq { my($self, $got, $dont_expect, $name) = @_; local $Level = $Level + 1; if( !defined $got || !defined $dont_expect ) { # undef only matches undef and nothing else my $test = defined $got || defined $dont_expect; $self->ok($test, $name); $self->_cmp_diag($got, 'ne', $dont_expect) unless $test; return $test; } return $self->cmp_ok($got, 'ne', $dont_expect, $name);}sub isnt_num { my($self, $got, $dont_expect, $name) = @_; local $Level = $Level + 1; if( !defined $got || !defined $dont_expect ) { # undef only matches undef and nothing else my $test = defined $got || defined $dont_expect; $self->ok($test, $name); $self->_cmp_diag($got, '!=', $dont_expect) unless $test; return $test; } return $self->cmp_ok($got, '!=', $dont_expect, $name);}=item B<like> $Test->like($this, qr/$regex/, $name); $Test->like($this, '/$regex/', $name);Like Test::More's like(). Checks if $this matches the given $regex.You'll want to avoid qr// if you want your tests to work before 5.005.=item B<unlike> $Test->unlike($this, qr/$regex/, $name); $Test->unlike($this, '/$regex/', $name);Like Test::More's unlike(). Checks if $this B<does not match> thegiven $regex.=cutsub like { my($self, $this, $regex, $name) = @_; local $Level = $Level + 1; $self->_regex_ok($this, $regex, '=~', $name);}sub unlike { my($self, $this, $regex, $name) = @_; local $Level = $Level + 1; $self->_regex_ok($this, $regex, '!~', $name);}=item B<maybe_regex> $Test->maybe_regex(qr/$regex/); $Test->maybe_regex('/$regex/');Convenience method for building testing functions that take regularexpressions as arguments, but need to work before perl 5.005.Takes a quoted regular expression produced by qr//, or a stringrepresenting a regular expression.Returns a Perl value which may be used instead of the correspondingregular expression, or undef if it's argument is not recognised.For example, a version of like(), sans the useful diagnostic messages,could be written as: sub laconic_like { my ($self, $this, $regex, $name) = @_; my $usable_regex = $self->maybe_regex($regex); die "expecting regex, found '$regex'\n" unless $usable_regex; $self->ok($this =~ m/$usable_regex/, $name); }=cutsub maybe_regex { my ($self, $regex) = @_; my $usable_regex = undef; return $usable_regex unless defined $regex; my($re, $opts); # Check for qr/foo/ if( ref $regex eq 'Regexp' ) { $usable_regex = $regex; } # Check for '/foo/' or 'm,foo,' elsif( ($re, $opts) = $regex =~ m{^ /(.*)/ (\w*) $ }sx or (undef, $re, $opts) = $regex =~ m,^ m([^\w\s]) (.+) \1 (\w*) $,sx ) { $usable_regex = length $opts ? "(?$opts)$re" : $re; } return $usable_regex;};sub _regex_ok { my($self, $this, $regex, $cmp, $name) = @_; local $Level = $Level + 1; my $ok = 0; my $usable_regex = $self->maybe_regex($regex); unless (defined $usable_regex) { $ok = $self->ok( 0, $name ); $self->diag(" '$regex' doesn't look much like a regex to me."); return $ok; } { local $^W = 0; my $test = $this =~ /$usable_regex/ ? 1 : 0; $test = !$test if $cmp eq '!~'; $ok = $self->ok( $test, $name ); } unless( $ok ) { $this = defined $this ? "'$this'" : 'undef'; my $match = $cmp eq '=~' ? "doesn't match" : "matches"; $self->diag(sprintf <<DIAGNOSTIC, $this, $match, $regex); %s %13s '%s'DIAGNOSTIC } return $ok;}=item B<cmp_ok> $Test->cmp_ok($this, $type, $that, $name);Works just like Test::More's cmp_ok(). $Test->cmp_ok($big_num, '!=', $other_big_num);=cutsub cmp_ok { my($self, $got, $type, $expect, $name) = @_; my $test; { local $^W = 0; local($@,$!); # don't interfere with $@ # eval() sometimes resets $! $test = eval "\$got $type \$expect"; } local $Level = $Level + 1; my $ok = $self->ok($test, $name); unless( $ok ) { if( $type =~ /^(eq|==)$/ ) { $self->_is_diag($got, $type, $expect); } else { $self->_cmp_diag($got, $type, $expect); } } return $ok;}sub _cmp_diag { my($self, $got, $type, $expect) = @_; $got = defined $got ? "'$got'" : 'undef'; $expect = defined $expect ? "'$expect'" : 'undef'; return $self->diag(sprintf <<DIAGNOSTIC, $got, $type, $expect); %s %s %sDIAGNOSTIC}=item B<BAILOUT> $Test->BAILOUT($reason);Indicates to the Test::Harness that things are going so badly alltesting should terminate. This includes running any additional testscripts.It will exit with 255.=cutsub BAILOUT { my($self, $reason) = @_; $self->_print("Bail out! $reason"); exit 255;}=item B<skip> $Test->skip; $Test->skip($why);Skips the current test, reporting $why.=cutsub skip { my($self, $why) = @_; $why ||= ''; $self->_unoverload(\$why); unless( $Have_Plan ) { require Carp; Carp::croak("You tried to run tests without a plan! Gotta have a plan."); } lock($Curr_Test); $Curr_Test++; $Test_Results[$Curr_Test-1] = &share({ 'ok' => 1, actual_ok => 1, name => '', type => 'skip', reason => $why, }); my $out = "ok"; $out .= " $Curr_Test" if $self->use_numbers; $out .= " # skip"; $out .= " $why" if length $why; $out .= "\n"; $Test->_print($out); return 1;}=item B<todo_skip> $Test->todo_skip; $Test->todo_skip($why);Like skip(), only it will declare the test as failing and TODO. Similarto print "not ok $tnum # TODO $why\n";=cutsub todo_skip { my($self, $why) = @_; $why ||= ''; unless( $Have_Plan ) { require Carp; Carp::croak("You tried to run tests without a plan! Gotta have a plan."); } lock($Curr_Test); $Curr_Test++; $Test_Results[$Curr_Test-1] = &share({ 'ok' => 1, actual_ok => 0, name => '', type => 'todo_skip', reason => $why, }); my $out = "not ok"; $out .= " $Curr_Test" if $self->use_numbers; $out .= " # TODO & SKIP $why\n"; $Test->_print($out); return 1;}=begin _unimplemented=item B<skip_rest> $Test->skip_rest; $Test->skip_rest($reason);Like skip(), only it skips all the rest of the tests you plan to runand terminates the test.If you're running under no_plan, it skips once and terminates thetest.=end _unimplemented=back=head2 Test style=over 4=item B<level> $Test->level($how_high);How far up the call stack should $Test look when reporting where thetest failed.Defaults to 1.Setting $Test::Builder::Level overrides. This is typically usefullocalized: { local $Test::Builder::Level = 2; $Test->ok($test); }=cutsub level { my($self, $level) = @_; if( defined $level ) { $Level = $level; } return $Level;}=item B<use_numbers> $Test->use_numbers($on_or_off);Whether or not the test should output numbers. That is, this if true: ok 1 ok 2 ok 3or this if false ok ok okMost useful when you can't depend on the test output order, such aswhen threads or forking is involved.Test::Harness will accept either, but avoid mixing the two styles.Defaults to on.=cutsub use_numbers { my($self, $use_nums) = @_; if( defined $use_nums ) { $Use_Nums = $use_nums; } return $Use_Nums;}=item B<no_header> $Test->no_header($no_header);If set to true, no "1..N" header will be printed.=item B<no_ending> $Test->no_ending($no_ending);Normally, Test::Builder does some extra diagnostics when the testends. It also changes the exit code as described below.If this is true, none of that will be done.=cutsub no_header { my($self, $no_header) = @_; if( defined $no_header ) { $No_Header = $no_header; } return $No_Header;}sub no_ending { my($self, $no_ending) = @_; if( defined $no_ending ) { $No_Ending = $no_ending; } return $No_Ending;}=back=head2 OutputControlling where the test output goes.It's ok for your test to change where STDOUT and STDERR point to,Test::Builder's default output settings will not be affected.=over 4=item B<diag> $Test->diag(@msgs);Prints out the given @msgs. Like C<print>, arguments are simplyappended together.Normally, it uses the failure_output() handle, but if this is for aTODO test, the todo_output() handle is used.Output will be indented and marked with a # so as not to interferewith test output. A newline will be put on the end if there isn't onealready.We encourage using this rather than calling print directly.Returns false. Why? Because diag() is often used in conjunction witha failing test (C<ok() || diag()>) it "passes through" the failure. return ok(...) || diag(...);=for blame transferMark Fowler <mark@twoshortplanks.com>=cutsub diag { my($self, @msgs) = @_; return unless @msgs; # Prevent printing headers when compiling (i.e. -c) return if $^C; # Smash args together like print does. # Convert undef to 'undef' so its readable. my $msg = join '', map { defined($_) ? $_ : 'undef' } @msgs; # Escape each line with a #. $msg =~ s/^/# /gm; # Stick a newline on the end if it needs it. $msg .= "\n" unless $msg =~ /\n\Z/; local $Level = $Level + 1; $self->_print_diag($msg); return 0;}=begin _private=item B<_print> $Test->_print(@msgs);Prints to the output() filehandle.=end _private=cutsub _print { my($self, @msgs) = @_; # Prevent printing headers when only compiling. Mostly for when # tests are deparsed with B::Deparse return if $^C; my $msg = join '', @msgs; local($\, $", $,) = (undef, ' ', ''); my $fh = $self->output; # Escape each line after the first with a # so we don't # confuse Test::Harness. $msg =~ s/\n(.)/\n# $1/sg; # Stick a newline on the end if it needs it. $msg .= "\n" unless $msg =~ /\n\Z/; print $fh $msg;}=item B<_print_diag> $Test->_print_diag(@msg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -