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

📄 test::more.3

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 3
📖 第 1 页 / 共 3 页
字号:
\&    is( ultimate_answer(), 42,          "Meaning of Life" );\&\&    # $foo isn\*(Aqt empty\&    isnt( $foo, \*(Aq\*(Aq,     "Got some foo" );.Ve.Spare similar to these:.Sp.Vb 2\&    ok( ultimate_answer() eq 42,        "Meaning of Life" );\&    ok( $foo ne \*(Aq\*(Aq,     "Got some foo" );.Ve.Sp(Mnemonic:  \*(L"This is that.\*(R"  \*(L"This isn't that.\*(R").SpSo why use these?  They produce better diagnostics on failure.  \fIok()\fRcannot know what you are testing for (beyond the name), but \fIis()\fR and\&\fIisnt()\fR know what the test was and why it failed.  For example thistest:.Sp.Vb 2\&    my $foo = \*(Aqwaffle\*(Aq;  my $bar = \*(Aqyarblokos\*(Aq;\&    is( $foo, $bar,   \*(AqIs foo the same as bar?\*(Aq );.Ve.SpWill produce something like this:.Sp.Vb 5\&    not ok 17 \- Is foo the same as bar?\&    #   Failed test \*(AqIs foo the same as bar?\*(Aq\&    #   in foo.t at line 139.\&    #          got: \*(Aqwaffle\*(Aq\&    #     expected: \*(Aqyarblokos\*(Aq.Ve.SpSo you can figure out what went wrong without rerunning the test..SpYou are encouraged to use \fIis()\fR and \fIisnt()\fR over \fIok()\fR where possible,however do not be tempted to use them to find out if something istrue or false!.Sp.Vb 2\&  # XXX BAD!\&  is( exists $brooklyn{tree}, 1, \*(AqA tree grows in Brooklyn\*(Aq );.Ve.SpThis does not check if \f(CW\*(C`exists $brooklyn{tree}\*(C'\fR is true, it checks ifit returns 1.  Very different.  Similar caveats exist for false and 0.In these cases, use \fIok()\fR..Sp.Vb 1\&  ok( exists $brooklyn{tree},    \*(AqA tree grows in Brooklyn\*(Aq );.Ve.SpFor those grammatical pedants out there, there's an \f(CW\*(C`isn\*(Aqt()\*(C'\fRfunction which is an alias of \fIisnt()\fR..IP "\fBlike\fR" 4.IX Item "like".Vb 1\&  like( $got, qr/expected/, $test_name );.Ve.SpSimilar to \fIok()\fR, \fIlike()\fR matches \f(CW$got\fR against the regex \f(CW\*(C`qr/expected/\*(C'\fR..SpSo this:.Sp.Vb 1\&    like($got, qr/expected/, \*(Aqthis is like that\*(Aq);.Ve.Spis similar to:.Sp.Vb 1\&    ok( $got =~ /expected/, \*(Aqthis is like that\*(Aq);.Ve.Sp(Mnemonic \*(L"This is like that\*(R".).SpThe second argument is a regular expression.  It may be given as aregex reference (i.e. \f(CW\*(C`qr//\*(C'\fR) or (for better compatibility with olderperls) as a string that looks like a regex (alternative delimiters arecurrently not supported):.Sp.Vb 1\&    like( $got, \*(Aq/expected/\*(Aq, \*(Aqthis is like that\*(Aq );.Ve.SpRegex options may be placed on the end (\f(CW\*(Aq/expected/i\*(Aq\fR)..SpIts advantages over \fIok()\fR are similar to that of \fIis()\fR and \fIisnt()\fR.  Betterdiagnostics on failure..IP "\fBunlike\fR" 4.IX Item "unlike".Vb 1\&  unlike( $got, qr/expected/, $test_name );.Ve.SpWorks exactly as \fIlike()\fR, only it checks if \f(CW$got\fR \fBdoes not\fR match thegiven pattern..IP "\fBcmp_ok\fR" 4.IX Item "cmp_ok".Vb 1\&  cmp_ok( $got, $op, $expected, $test_name );.Ve.SpHalfway between \fIok()\fR and \fIis()\fR lies \fIcmp_ok()\fR.  This allows you tocompare two arguments using any binary perl operator..Sp.Vb 2\&    # ok( $got eq $expected );\&    cmp_ok( $got, \*(Aqeq\*(Aq, $expected, \*(Aqthis eq that\*(Aq );\&\&    # ok( $got == $expected );\&    cmp_ok( $got, \*(Aq==\*(Aq, $expected, \*(Aqthis == that\*(Aq );\&\&    # ok( $got && $expected );\&    cmp_ok( $got, \*(Aq&&\*(Aq, $expected, \*(Aqthis && that\*(Aq );\&    ...etc....Ve.SpIts advantage over \fIok()\fR is when the test fails you'll know what \f(CW$got\fRand \f(CW$expected\fR were:.Sp.Vb 5\&    not ok 1\&    #   Failed test in foo.t at line 12.\&    #     \*(Aq23\*(Aq\&    #         &&\&    #     undef.Ve.SpIt's also useful in those cases where you are comparing numbers and\&\fIis()\fR's use of \f(CW\*(C`eq\*(C'\fR will interfere:.Sp.Vb 1\&    cmp_ok( $big_hairy_number, \*(Aq==\*(Aq, $another_big_hairy_number );.Ve.IP "\fBcan_ok\fR" 4.IX Item "can_ok".Vb 2\&  can_ok($module, @methods);\&  can_ok($object, @methods);.Ve.SpChecks to make sure the \f(CW$module\fR or \f(CW$object\fR can do these \f(CW@methods\fR(works with functions, too)..Sp.Vb 1\&    can_ok(\*(AqFoo\*(Aq, qw(this that whatever));.Ve.Spis almost exactly like saying:.Sp.Vb 4\&    ok( Foo\->can(\*(Aqthis\*(Aq) && \&        Foo\->can(\*(Aqthat\*(Aq) && \&        Foo\->can(\*(Aqwhatever\*(Aq) \&      );.Ve.Sponly without all the typing and with a better interface.  Handy forquickly testing an interface..SpNo matter how many \f(CW@methods\fR you check, a single \fIcan_ok()\fR call countsas one test.  If you desire otherwise, use:.Sp.Vb 3\&    foreach my $meth (@methods) {\&        can_ok(\*(AqFoo\*(Aq, $meth);\&    }.Ve.IP "\fBisa_ok\fR" 4.IX Item "isa_ok".Vb 2\&  isa_ok($object, $class, $object_name);\&  isa_ok($ref,    $type,  $ref_name);.Ve.SpChecks to see if the given \f(CW\*(C`$object\->isa($class)\*(C'\fR.  Also checks to makesure the object was defined in the first place.  Handy for this sortof thing:.Sp.Vb 2\&    my $obj = Some::Module\->new;\&    isa_ok( $obj, \*(AqSome::Module\*(Aq );.Ve.Spwhere you'd otherwise have to write.Sp.Vb 2\&    my $obj = Some::Module\->new;\&    ok( defined $obj && $obj\->isa(\*(AqSome::Module\*(Aq) );.Ve.Spto safeguard against your test script blowing up..SpIt works on references, too:.Sp.Vb 1\&    isa_ok( $array_ref, \*(AqARRAY\*(Aq );.Ve.SpThe diagnostics of this test normally just refer to 'the object'.  Ifyou'd like them to be more specific, you can supply an \f(CW$object_name\fR(for example 'Test customer')..IP "\fBpass\fR" 4.IX Item "pass".PD 0.IP "\fBfail\fR" 4.IX Item "fail".PD.Vb 2\&  pass($test_name);\&  fail($test_name);.Ve.SpSometimes you just want to say that the tests have passed.  Usuallythe case is you've got some complicated condition that is difficult towedge into an \fIok()\fR.  In this case, you can simply use \fIpass()\fR (todeclare the test ok) or fail (for not ok).  They are synonyms for\&\fIok\fR\|(1) and \fIok\fR\|(0)..SpUse these very, very, very sparingly..Sh "Module tests".IX Subsection "Module tests"You usually want to test if the module you're testing loads ok, ratherthan just vomiting if its load fails.  For such purposes we have\&\f(CW\*(C`use_ok\*(C'\fR and \f(CW\*(C`require_ok\*(C'\fR..IP "\fBuse_ok\fR" 4.IX Item "use_ok".Vb 2\&   BEGIN { use_ok($module); }\&   BEGIN { use_ok($module, @imports); }.Ve.SpThese simply use the given \f(CW$module\fR and test to make sure the loadhappened ok.  It's recommended that you run \fIuse_ok()\fR inside a \s-1BEGIN\s0block so its functions are exported at compile-time and prototypes areproperly honored..SpIf \f(CW@imports\fR are given, they are passed through to the use.  So this:.Sp.Vb 1\&   BEGIN { use_ok(\*(AqSome::Module\*(Aq, qw(foo bar)) }.Ve.Spis like doing this:.Sp.Vb 1\&   use Some::Module qw(foo bar);.Ve.SpVersion numbers can be checked like so:.Sp.Vb 2\&   # Just like "use Some::Module 1.02"\&   BEGIN { use_ok(\*(AqSome::Module\*(Aq, 1.02) }.Ve.SpDon't try to do this:.Sp.Vb 2\&   BEGIN {\&       use_ok(\*(AqSome::Module\*(Aq);\&\&       ...some code that depends on the use...\&       ...happening at compile time...\&   }.Ve.Spbecause the notion of \*(L"compile-time\*(R" is relative.  Instead, you want:.Sp.Vb 2\&  BEGIN { use_ok(\*(AqSome::Module\*(Aq) }\&  BEGIN { ...some code that depends on the use... }.Ve.IP "\fBrequire_ok\fR" 4.IX Item "require_ok".Vb 2\&   require_ok($module);\&   require_ok($file);.Ve.SpLike \fIuse_ok()\fR, except it requires the \f(CW$module\fR or \f(CW$file\fR..Sh "Complex data structures".IX Subsection "Complex data structures"Not everything is a simple eq check or regex.  There are times youneed to see if two data structures are equivalent.  For theseinstances Test::More provides a handful of useful functions..PP\&\fB\s-1NOTE\s0\fR I'm not quite sure what will happen with filehandles..IP "\fBis_deeply\fR" 4.IX Item "is_deeply".Vb 1\&  is_deeply( $got, $expected, $test_name );.Ve.SpSimilar to \fIis()\fR, except that if \f(CW$got\fR and \f(CW$expected\fR are references, itdoes a deep comparison walking each data structure to see if they areequivalent.  If the two structures are different, it will display theplace where they start differing..Sp\&\fIis_deeply()\fR compares the dereferenced values of references, thereferences themselves (except for their type) are ignored.  This meansaspects such as blessing and ties are not considered \*(L"different\*(R"..Sp\&\fIis_deeply()\fR current has very limited handling of function referenceand globs.  It merely checks if they have the same referent.  This mayimprove in the future..SpTest::Differences and Test::Deep provide more in-depth functionalityalong these lines..Sh "Diagnostics".IX Subsection "Diagnostics"If you pick the right test function, you'll usually get a good idea ofwhat went wrong when it failed.  But sometimes it doesn't work outthat way.  So here we have ways for you to write your own diagnosticmessages which are safer than just \f(CW\*(C`print STDERR\*(C'\fR..IP "\fBdiag\fR" 4.IX Item "diag".Vb 1\&  diag(@diagnostic_message);.Ve.SpPrints a diagnostic message which is guaranteed not to interfere withtest output.  Like \f(CW\*(C`print\*(C'\fR \f(CW@diagnostic_message\fR is simply concatenatedtogether..SpHandy for this sort of thing:.Sp.Vb 2\&    ok( grep(/foo/, @users), "There\*(Aqs a foo user" ) or\&        diag("Since there\*(Aqs no foo, check that /etc/bar is set up right");.Ve.Spwhich would produce:.Sp.Vb 4\&    not ok 42 \- There\*(Aqs a foo user\&    #   Failed test \*(AqThere\*(Aqs a foo user\*(Aq\&    #   in foo.t at line 52.\&    # Since there\*(Aqs no foo, check that /etc/bar is set up right..Ve.SpYou might remember \f(CW\*(C`ok() or diag()\*(C'\fR with the mnemonic \f(CW\*(C`open() ordie()\*(C'\fR..Sp\&\fB\s-1NOTE\s0\fR The exact formatting of the diagnostic output is stillchanging, but it is guaranteed that whatever you throw at it it won'tinterfere with the test..Sh "Conditional tests".IX Subsection "Conditional tests"

⌨️ 快捷键说明

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