⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 more.pm

📁 关于Berkelay数据库的共享源码
💻 PM
📖 第 1 页 / 共 3 页
字号:
package Test::More;use 5.004;use strict;use Test::Builder;# Can't use Carp because it might cause use_ok() to accidentally succeed# even though the module being used forgot to use Carp.  Yes, this# actually happened.sub _carp {    my($file, $line) = (caller(1))[1,2];    warn @_, " at $file line $line\n";}require Exporter;use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS $TODO);$VERSION = '0.54';$VERSION = eval $VERSION;    # make the alpha version come out as a number@ISA    = qw(Exporter);@EXPORT = qw(ok use_ok require_ok             is isnt like unlike is_deeply             cmp_ok             skip todo todo_skip             pass fail             eq_array eq_hash eq_set             $TODO             plan             can_ok  isa_ok             diag            );my $Test = Test::Builder->new;my $Show_Diag = 1;# 5.004's Exporter doesn't have export_to_level.sub _export_to_level{      my $pkg = shift;      my $level = shift;      (undef) = shift;                  # redundant arg      my $callpkg = caller($level);      $pkg->export($callpkg, @_);}=head1 NAMETest::More - yet another framework for writing test scripts=head1 SYNOPSIS  use Test::More tests => $Num_Tests;  # or  use Test::More qw(no_plan);  # or  use Test::More skip_all => $reason;  BEGIN { use_ok( 'Some::Module' ); }  require_ok( 'Some::Module' );  # Various ways to say "ok"  ok($this eq $that, $test_name);  is  ($this, $that,    $test_name);  isnt($this, $that,    $test_name);  # Rather than print STDERR "# here's what went wrong\n"  diag("here's what went wrong");  like  ($this, qr/that/, $test_name);  unlike($this, qr/that/, $test_name);  cmp_ok($this, '==', $that, $test_name);  is_deeply($complex_structure1, $complex_structure2, $test_name);  SKIP: {      skip $why, $how_many unless $have_some_feature;      ok( foo(),       $test_name );      is( foo(42), 23, $test_name );  };  TODO: {      local $TODO = $why;      ok( foo(),       $test_name );      is( foo(42), 23, $test_name );  };  can_ok($module, @methods);  isa_ok($object, $class);  pass($test_name);  fail($test_name);  # Utility comparison functions.  eq_array(\@this, \@that);  eq_hash(\%this, \%that);  eq_set(\@this, \@that);  # UNIMPLEMENTED!!!  my @status = Test::More::status;  # UNIMPLEMENTED!!!  BAIL_OUT($why);=head1 DESCRIPTIONB<STOP!> If you're just getting started writing tests, have a look atTest::Simple first.  This is a drop in replacement for Test::Simplewhich you can switch to once you get the hang of basic testing.The purpose of this module is to provide a wide range of testingutilities.  Various ways to say "ok" with better diagnostics,facilities to skip tests, test future features and compare complicateddata structures.  While you can do almost anything with a simpleC<ok()> function, it doesn't provide good diagnostic output.=head2 I love it when a plan comes togetherBefore anything else, you need a testing plan.  This basically declareshow many tests your script is going to run to protect against prematurefailure.The preferred way to do this is to declare a plan when you C<use Test::More>.  use Test::More tests => $Num_Tests;There are rare cases when you will not know beforehand how many testsyour script is going to run.  In this case, you can declare that youhave no plan.  (Try to avoid using this as it weakens your test.)  use Test::More qw(no_plan);B<NOTE>: using no_plan requires a Test::Harness upgrade else it willthink everything has failed.  See L<BUGS and CAVEATS>)In some cases, you'll want to completely skip an entire testing script.  use Test::More skip_all => $skip_reason;Your script will declare a skip with the reason why you skipped andexit immediately with a zero (success).  See L<Test::Harness> fordetails.If you want to control what functions Test::More will export, youhave to use the 'import' option.  For example, to import everythingbut 'fail', you'd do:  use Test::More tests => 23, import => ['!fail'];Alternatively, you can use the plan() function.  Useful for when youhave to calculate the number of tests.  use Test::More;  plan tests => keys %Stuff * 3;or for deciding between running the tests at all:  use Test::More;  if( $^O eq 'MacOS' ) {      plan skip_all => 'Test irrelevant on MacOS';  }  else {      plan tests => 42;  }=cutsub plan {    my(@plan) = @_;    my $idx = 0;    my @cleaned_plan;    while( $idx <= $#plan ) {        my $item = $plan[$idx];        if( $item eq 'no_diag' ) {            $Show_Diag = 0;        }        else {            push @cleaned_plan, $item;        }        $idx++;    }    $Test->plan(@cleaned_plan);}sub import {    my($class) = shift;    my $caller = caller;    $Test->exported_to($caller);    my $idx = 0;    my @plan;    my @imports;    while( $idx <= $#_ ) {        my $item = $_[$idx];        if( $item eq 'import' ) {            push @imports, @{$_[$idx+1]};            $idx++;        }        else {            push @plan, $item;        }        $idx++;    }    plan(@plan);    __PACKAGE__->_export_to_level(1, __PACKAGE__, @imports);}=head2 Test namesBy convention, each test is assigned a number in order.  This islargely done automatically for you.  However, it's often very useful toassign a name to each test.  Which would you rather see:  ok 4  not ok 5  ok 6or  ok 4 - basic multi-variable  not ok 5 - simple exponential  ok 6 - force == mass * accelerationThe later gives you some idea of what failed.  It also makes it easierto find the test in your script, simply search for "simpleexponential".All test functions take a name argument.  It's optional, but highlysuggested that you use it.=head2 I'm ok, you're not ok.The basic purpose of this module is to print out either "ok #" or "notok #" depending on if a given test succeeded or failed.  Everythingelse is just gravy.All of the following print "ok" or "not ok" depending on if the testsucceeded or failed.  They all also return true or false,respectively.=over 4=item B<ok>  ok($this eq $that, $test_name);This simply evaluates any expression (C<$this eq $that> is just asimple example) and uses that to determine if the test succeeded orfailed.  A true expression passes, a false one fails.  Very simple.For example:    ok( $exp{9} == 81,                   'simple exponential' );    ok( Film->can('db_Main'),            'set_db()' );    ok( $p->tests == 4,                  'saw tests' );    ok( !grep !defined $_, @items,       'items populated' );(Mnemonic:  "This is ok.")$test_name is a very short description of the test that will be printedout.  It makes it very easy to find a test in your script when it failsand gives others an idea of your intentions.  $test_name is optional,but we B<very> strongly encourage its use.Should an ok() fail, it will produce some diagnostics:    not ok 18 - sufficient mucus    #     Failed test 18 (foo.t at line 42)This is actually Test::Simple's ok() routine.=cutsub ok ($;$) {    my($test, $name) = @_;    $Test->ok($test, $name);}=item B<is>=item B<isnt>  is  ( $this, $that, $test_name );  isnt( $this, $that, $test_name );Similar to ok(), is() and isnt() compare their two argumentswith C<eq> and C<ne> respectively and use the result of that todetermine if the test succeeded or failed.  So these:    # Is the ultimate answer 42?    is( ultimate_answer(), 42,          "Meaning of Life" );    # $foo isn't empty    isnt( $foo, '',     "Got some foo" );are similar to these:    ok( ultimate_answer() eq 42,        "Meaning of Life" );    ok( $foo ne '',     "Got some foo" );(Mnemonic:  "This is that."  "This isn't that.")So why use these?  They produce better diagnostics on failure.  ok()cannot know what you are testing for (beyond the name), but is() andisnt() know what the test was and why it failed.  For example thistest:    my $foo = 'waffle';  my $bar = 'yarblokos';    is( $foo, $bar,   'Is foo the same as bar?' );Will produce something like this:    not ok 17 - Is foo the same as bar?    #     Failed test (foo.t at line 139)    #          got: 'waffle'    #     expected: 'yarblokos'So you can figure out what went wrong without rerunning the test.You are encouraged to use is() and isnt() over ok() where possible,however do not be tempted to use them to find out if something istrue or false!  # XXX BAD!  is( exists $brooklyn{tree}, 1, 'A tree grows in Brooklyn' );This does not check if C<exists $brooklyn{tree}> is true, it checks ifit returns 1.  Very different.  Similar caveats exist for false and 0.In these cases, use ok().  ok( exists $brooklyn{tree},    'A tree grows in Brooklyn' );For those grammatical pedants out there, there's an C<isn't()>function which is an alias of isnt().=cutsub is ($$;$) {    $Test->is_eq(@_);}sub isnt ($$;$) {    $Test->isnt_eq(@_);}*isn't = \&isnt;=item B<like>  like( $this, qr/that/, $test_name );Similar to ok(), like() matches $this against the regex C<qr/that/>.So this:    like($this, qr/that/, 'this is like that');is similar to:    ok( $this =~ /that/, 'this is like that');(Mnemonic "This is like that".)The second argument is a regular expression.  It may be given as aregex reference (i.e. C<qr//>) or (for better compatibility with olderperls) as a string that looks like a regex (alternative delimiters arecurrently not supported):    like( $this, '/that/', 'this is like that' );Regex options may be placed on the end (C<'/that/i'>).Its advantages over ok() are similar to that of is() and isnt().  Betterdiagnostics on failure.=cutsub like ($$;$) {    $Test->like(@_);}=item B<unlike>  unlike( $this, qr/that/, $test_name );Works exactly as like(), only it checks if $this B<does not> match thegiven pattern.=cutsub unlike ($$;$) {    $Test->unlike(@_);}=item B<cmp_ok>  cmp_ok( $this, $op, $that, $test_name );Halfway between ok() and is() lies cmp_ok().  This allows you tocompare two arguments using any binary perl operator.    # ok( $this eq $that );    cmp_ok( $this, 'eq', $that, 'this eq that' );    # ok( $this == $that );    cmp_ok( $this, '==', $that, 'this == that' );    # ok( $this && $that );    cmp_ok( $this, '&&', $that, 'this && that' );    ...etc...Its advantage over ok() is when the test fails you'll know what $thisand $that were:    not ok 1    #     Failed test (foo.t at line 12)    #     '23'    #         &&    #     undefIt's also useful in those cases where you are comparing numbers andis()'s use of C<eq> will interfere:    cmp_ok( $big_hairy_number, '==', $another_big_hairy_number );=cutsub cmp_ok($$$;$) {    $Test->cmp_ok(@_);}=item B<can_ok>  can_ok($module, @methods);  can_ok($object, @methods);Checks to make sure the $module or $object can do these @methods(works with functions, too).    can_ok('Foo', qw(this that whatever));is almost exactly like saying:    ok( Foo->can('this') &&         Foo->can('that') &&         Foo->can('whatever')       );only without all the typing and with a better interface.  Handy forquickly testing an interface.No matter how many @methods you check, a single can_ok() call countsas one test.  If you desire otherwise, use:    foreach my $meth (@methods) {        can_ok('Foo', $meth);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -