more.html

来自「perl教程」· HTML 代码 · 共 1,178 行 · 第 1/5 页

HTML
1,178 行
字号
</pre>
<p><a href="#item_is_deeply"><code>is_deeply()</code></a> can do that better and with diagnostics.</p>
<pre>
    <span class="variable">is_deeply</span><span class="operator">(</span> <span class="operator">\</span><span class="variable">@this</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">@that</span> <span class="operator">);</span>
</pre>
<p>They may be deprecated in future versions.</p>
<dl>
<dt><strong><a name="item_eq_array"><strong>eq_array</strong></a></strong>

<dd>
<pre>
  <span class="keyword">my</span> <span class="variable">$is_eq</span> <span class="operator">=</span> <span class="variable">eq_array</span><span class="operator">(\</span><span class="variable">@this</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">@that</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Checks if two arrays are equivalent.  This is a deep check, so
multi-level structures are handled correctly.</p>
</dd>
<dt><strong><a name="item_eq_hash"><strong>eq_hash</strong></a></strong>

<dd>
<pre>
  <span class="keyword">my</span> <span class="variable">$is_eq</span> <span class="operator">=</span> <span class="variable">eq_hash</span><span class="operator">(\</span><span class="variable">%this</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">%that</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Determines if the two hashes contain the same keys and values.  This
is a deep check.</p>
</dd>
<dt><strong><a name="item_eq_set"><strong>eq_set</strong></a></strong>

<dd>
<pre>
  <span class="keyword">my</span> <span class="variable">$is_eq</span> <span class="operator">=</span> <span class="variable">eq_set</span><span class="operator">(\</span><span class="variable">@this</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">@that</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Similar to eq_array(), except the order of the elements is <strong>not</strong>
important.  This is a deep check, but the irrelevancy of order only
applies to the top level.</p>
</dd>
<dd>
<pre>
    <span class="variable">ok</span><span class="operator">(</span> <span class="variable">eq_set</span><span class="operator">(\</span><span class="variable">@this</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">@that</span><span class="operator">)</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Is better written:</p>
</dd>
<dd>
<pre>
    <span class="variable">is_deeply</span><span class="operator">(</span> <span class="operator">[</span><span class="keyword">sort</span> <span class="variable">@this</span><span class="operator">]</span><span class="operator">,</span> <span class="operator">[</span><span class="keyword">sort</span> <span class="variable">@that</span><span class="operator">]</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p><strong>NOTE</strong> By historical accident, this is not a true set comparison.
While the order of elements does not matter, duplicate elements do.</p>
</dd>
<dd>
<p><strong>NOTE</strong> <a href="#item_eq_set"><code>eq_set()</code></a> does not know how to deal with references at the top
level.  The following is an example of a comparison which might not work:</p>
</dd>
<dd>
<pre>
    <span class="variable">eq_set</span><span class="operator">(</span><span class="operator">[</span><span class="operator">\</span><span class="number">1</span><span class="operator">,</span> <span class="operator">\</span><span class="number">2</span><span class="operator">]</span><span class="operator">,</span> <span class="operator">[</span><span class="operator">\</span><span class="number">2</span><span class="operator">,</span> <span class="operator">\</span><span class="number">1</span><span class="operator">]</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Test::Deep contains much better set comparison functions.</p>
</dd>
</dl>
<p>
</p>
<h2><a name="extending_and_embedding_test__more">Extending and Embedding Test::More</a></h2>
<p>Sometimes the Test::More interface isn't quite enough.  Fortunately,
Test::More is built on top of Test::Builder which provides a single,
unified backend for any test library to use.  This means two test
libraries which both use Test::Builder <strong>can be used together in the
same program</strong>.</p>
<p>If you simply want to do a little tweaking of how the tests behave,
you can access the underlying Test::Builder object like so:</p>
<dl>
<dt><strong><a name="item_builder"><strong>builder</strong></a></strong>

<dd>
<pre>
    <span class="keyword">my</span> <span class="variable">$test_builder</span> <span class="operator">=</span> <span class="variable">Test::More</span><span class="operator">-&gt;</span><span class="variable">builder</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Returns the Test::Builder object underlying Test::More for you to play
with.</p>
</dd>
</dl>
<p>
</p>
<hr />
<h1><a name="exit_codes">EXIT CODES</a></h1>
<p>If all your tests passed, Test::Builder will exit with zero (which is
normal).  If anything failed it will exit with how many failed.  If
you run less (or more) tests than you planned, the missing (or extras)
will be considered failures.  If no tests were ever run Test::Builder
will throw a warning and exit with 255.  If the test died, even after
having successfully completed all its tests, it will still be
considered a failure and will exit with 255.</p>
<p>So the exit codes are...</p>
<pre>
    0                   all tests successful
    255                 test died or all passed but wrong # of tests run
    any other number    how many failed (including missing or extras)</pre>
<p>If you fail more than 254 tests, it will be reported as 254.</p>
<p><strong>NOTE</strong>  This behavior may go away in future versions.</p>
<p>
</p>
<hr />
<h1><a name="caveats_and_notes">CAVEATS and NOTES</a></h1>
<dl>
<dt><strong><a name="item_backwards_compatibility">Backwards compatibility</a></strong>

<dd>
<p>Test::More works with Perls as old as 5.004_05.</p>
</dd>
</li>
<dt><strong><a name="item_overloaded_objects">Overloaded objects</a></strong>

<dd>
<p>String overloaded objects are compared <strong>as strings</strong> (or in cmp_ok()'s
case, strings or numbers as appropriate to the comparison op).  This
prevents Test::More from piercing an object's interface allowing
better blackbox testing.  So if a function starts returning overloaded
objects instead of bare strings your tests won't notice the
difference.  This is good.</p>
</dd>
<dd>
<p>However, it does mean that functions like <a href="#item_is_deeply"><code>is_deeply()</code></a> cannot be used to
test the internals of string overloaded objects.  In this case I would
suggest Test::Deep which contains more flexible testing functions for
complex data structures.</p>
</dd>
</li>
<dt><strong><a name="item_threads">Threads</a></strong>

<dd>
<p>Test::More will only be aware of threads if &quot;use threads&quot; has been done
<em>before</em> Test::More is loaded.  This is ok:</p>
</dd>
<dd>
<pre>
    <span class="keyword">use</span> <span class="variable">threads</span><span class="operator">;</span>
    <span class="keyword">use</span> <span class="variable">Test::More</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>This may cause problems:</p>
</dd>
<dd>
<pre>
    <span class="keyword">use</span> <span class="variable">Test::More</span>
    <span class="keyword">use</span> <span class="variable">threads</span><span class="operator">;</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_test_3a_3aharness_upgrade">Test::Harness upgrade</a></strong>

<dd>
<p>no_plan and todo depend on new Test::Harness features and fixes.  If
you're going to distribute tests that use no_plan or todo your
end-users will have to upgrade Test::Harness to the latest one on
CPAN.  If you avoid no_plan and TODO tests, the stock Test::Harness
will work fine.</p>
</dd>
<dd>
<p>Installing Test::More should also upgrade Test::Harness.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="history">HISTORY</a></h1>
<p>This is a case of convergent evolution with Joshua Pritikin's Test
module.  I was largely unaware of its existence when I'd first
written my own <a href="#item_ok"><code>ok()</code></a> routines.  This module exists because I can't
figure out how to easily wedge test names into Test's interface (along
with a few other problems).</p>
<p>The goal here is to have a testing utility that's simple to learn,
quick to use and difficult to trip yourself up with while still
providing more flexibility than the existing Test.pm.  As such, the
names of the most common routines are kept tiny, special cases and
magic side-effects are kept to a minimum.  WYSIWYG.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../../lib/Test/Simple.html">the Test::Simple manpage</a> if all this confuses you and you just want to write
some tests.  You can upgrade to Test::More later (it's forward
compatible).</p>
<p><a href="../../lib/Test.html">the Test manpage</a> is the old testing module.  Its main benefit is that it has
been distributed with Perl since 5.004_05.</p>
<p><a href="../../lib/Test/Harness.html">the Test::Harness manpage</a> for details on how your test results are interpreted
by Perl.</p>
<p><a href="../../Test/Differences.html">the Test::Differences manpage</a> for more ways to test complex data structures.
And it plays well with Test::More.</p>
<p><a href="../../Test/Class.html">the Test::Class manpage</a> is like XUnit but more perlish.</p>
<p><a href="../../Test/Deep.html">the Test::Deep manpage</a> gives you more powerful complex data structure testing.</p>
<p><a href="../../Test/Unit.html">the Test::Unit manpage</a> is XUnit style testing.</p>
<p><a href="../../Test/Inline.html">the Test::Inline manpage</a> shows the idea of embedded testing.</p>
<p><a href="../../Bundle/Test.html">the Bundle::Test manpage</a> installs a whole bunch of useful test modules.</p>
<p>
</p>
<hr />
<h1><a name="authors">AUTHORS</a></h1>
<p>Michael G Schwern &lt;<a href="mailto:schwern@pobox.com">schwern@pobox.com</a>&gt; with much inspiration
from Joshua Pritikin's Test module and lots of help from Barrie
Slaymaker, Tony Bowden, blackstar.co.uk, chromatic, Fergal Daly and
the perl-qa gang.</p>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>See <em><a href="http://rt.cpan.org">http://rt.cpan.org</a></em> to report and view bugs.</p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<p>Copyright 2001, 2002, 2004 by Michael G Schwern &lt;<a href="mailto:schwern@pobox.com">schwern@pobox.com</a>&gt;.</p>
<p>This program is free software; you can redistribute it and/or 
modify it under the same terms as Perl itself.</p>
<p>See <em><a href="http://www.perl.com/perl/misc/Artistic.html">http://www.perl.com/perl/misc/Artistic.html</a></em></p>

</body>

</html>

⌨️ 快捷键说明

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