tutorial.html

来自「perl教程」· HTML 代码 · 共 648 行 · 第 1/4 页

HTML
648 行
字号
        <span class="variable">skip</span><span class="operator">(</span><span class="string">"I don't wanna die!"</span><span class="operator">);</span>
</pre>
<pre>
        <span class="keyword">die</span><span class="operator">,</span> <span class="keyword">die</span><span class="operator">,</span> <span class="keyword">die</span><span class="operator">,</span> <span class="keyword">die</span><span class="operator">,</span> <span class="keyword">die</span><span class="operator">;</span>
            <span class="operator">}</span>
</pre>
<p>
</p>
<h2><a name="todo_tests">Todo tests</a></h2>
<p>Thumbing through the Date::ICal man page, I came across this:</p>
<pre>
   ical</pre>
<pre>
       <span class="variable">$ical_string</span> <span class="operator">=</span> <span class="variable">$ical</span><span class="operator">-&gt;</span><span class="variable">ical</span><span class="operator">;</span>
</pre>
<pre>
   Retrieves, or sets, the date on the object, using any
   valid ICal date/time string.</pre>
<p>&quot;Retrieves or sets&quot;.  Hmmm, didn't see a test for using <code>ical()</code> to set
the date in the Date::ICal test suite.  So I'll write one.</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Test::More</span> <span class="string">tests</span> <span class="operator">=&gt;</span> <span class="number">1</span><span class="operator">;</span>
    <span class="keyword">use</span> <span class="variable">Date::ICal</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">my</span> <span class="variable">$ical</span> <span class="operator">=</span> <span class="variable">Date::ICal</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">;</span>
    <span class="variable">$ical</span><span class="operator">-&gt;</span><span class="variable">ical</span><span class="operator">(</span><span class="string">'20201231Z'</span><span class="operator">);</span>
    <span class="variable">is</span><span class="operator">(</span> <span class="variable">$ical</span><span class="operator">-&gt;</span><span class="variable">ical</span><span class="operator">,</span> <span class="string">'20201231Z'</span><span class="operator">,</span>   <span class="string">'Setting via ical()'</span> <span class="operator">);</span>
</pre>
<p>run that and I get</p>
<pre>
    1..1
    not ok 1 - Setting via ical()
    #     Failed test (- at line 6)
    #          got: '20010814T233649Z'
    #     expected: '20201231Z'
    # Looks like you failed 1 tests of 1.</pre>
<p>Whoops!  Looks like it's unimplemented.  Let's assume we don't have
the time to fix this. [11] Normally, you'd just comment out the test
and put a note in a todo list somewhere.  Instead, we're going to
explicitly state &quot;this test will fail&quot; by wrapping it in a <code>TODO</code> block.</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Test::More</span> <span class="string">tests</span> <span class="operator">=&gt;</span> <span class="number">1</span><span class="operator">;</span>
</pre>
<pre>
    <span class="variable">TODO</span><span class="operator">:</span> <span class="operator">{</span>
        <span class="keyword">local</span> <span class="variable">$TODO</span> <span class="operator">=</span> <span class="string">'ical($ical) not yet implemented'</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">$ical</span> <span class="operator">=</span> <span class="variable">Date::ICal</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">;</span>
        <span class="variable">$ical</span><span class="operator">-&gt;</span><span class="variable">ical</span><span class="operator">(</span><span class="string">'20201231Z'</span><span class="operator">);</span>
</pre>
<pre>
        <span class="variable">is</span><span class="operator">(</span> <span class="variable">$ical</span><span class="operator">-&gt;</span><span class="variable">ical</span><span class="operator">,</span> <span class="string">'20201231Z'</span><span class="operator">,</span>   <span class="string">'Setting via ical()'</span> <span class="operator">);</span>
            <span class="operator">}</span>
</pre>
<p>Now when you run, it's a little different:</p>
<pre>
    1..1
    not ok 1 - Setting via ical() # TODO ical($ical) not yet implemented
    #          got: '20010822T201551Z'
    #     expected: '20201231Z'</pre>
<p>Test::More doesn't say &quot;Looks like you failed 1 tests of 1&quot;.  That '#
TODO' tells Test::Harness &quot;this is supposed to fail&quot; and it treats a
failure as a successful test.  So you can write tests even before
you've fixed the underlying code.</p>
<p>If a TODO test passes, Test::Harness will report it &quot;UNEXPECTEDLY
SUCCEEDED&quot;.  When that happens, you simply remove the TODO block with
<code>local $TODO</code> and turn it into a real test.</p>
<p>
</p>
<h2><a name="testing_with_taint_mode_">Testing with taint mode.</a></h2>
<p>Taint mode is a funny thing.  It's the globalest of all global
features.  Once you turn it on, it affects <em>all</em> code in your program
and <em>all</em> modules used (and all the modules they use).  If a single
piece of code isn't taint clean, the whole thing explodes.  With that
in mind, it's very important to ensure your module works under taint
mode.</p>
<p>It's very simple to have your tests run under taint mode.  Just throw
a <a href="../../lib/Pod/perlrun.html#item__2dt"><code>-T</code></a> into the <code>#!</code> line.  Test::Harness will read the switches
in <code>#!</code> and use them to run your tests.</p>
<pre>
    <span class="comment">#!/usr/bin/perl -Tw</span>
</pre>
<pre>
    ...test normally here...</pre>
<p>So when you say <code>make test</code> it will be run with taint mode and
warnings on.</p>
<p>
</p>
<hr />
<h1><a name="footnotes">FOOTNOTES</a></h1>
<ol>
<li>
<p>The first number doesn't really mean anything, but it has to be 1.
It's the second number that's important.</p>
</li>
<li>
<p>For those following along at home, I'm using version 1.31.  It has
some bugs, which is good -- we'll uncover them with our tests.</p>
</li>
<li>
<p>You can actually take this one step further and test the manual
itself.  Have a look at <strong>Test::Inline</strong> (formerly <strong>Pod::Tests</strong>).</p>
</li>
<li>
<p>Yes, there's a mistake in the test suite.  What!  Me, contrived?</p>
</li>
<li>
<p>We'll get to testing the contents of lists later.</p>
</li>
<li>
<p>But what happens if your test program dies halfway through?!  Since we
didn't say how many tests we're going to run, how can we know it
failed?  No problem, Test::More employs some magic to catch that death
and turn the test into a failure, even if every test passed up to that
point.</p>
</li>
<li>
<p>I cleaned it up a little.</p>
</li>
<li>
<p>Most Operating Systems record time as the number of seconds since a
certain date.  This date is the beginning of the epoch.  Unix's starts
at midnight January 1st, 1970 GMT.</p>
</li>
<li>
<p>MacOS's epoch is midnight January 1st, 1904.  VMS's is midnight,
November 17th, 1858, but vmsperl emulates the Unix epoch so it's not a
problem.</p>
</li>
<li>
<p>As long as the code inside the SKIP block at least compiles.  Please
don't ask how.  No, it's not a filter.</p>
</li>
<li>
<p>Do NOT be tempted to use TODO tests as a way to avoid fixing simple
bugs!</p>
</li>
</ol>
<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; and the perl-qa dancers!</p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<p>Copyright 2001 by Michael G Schwern &lt;<a href="mailto:schwern@pobox.com">schwern@pobox.com</a>&gt;.</p>
<p>This documentation is free; you can redistribute it and/or modify it
under the same terms as Perl itself.</p>
<p>Irrespective of its distribution, all code examples in these files
are hereby placed into the public domain.  You are permitted and
encouraged to use this code in your own programs for fun
or for profit as you see fit.  A simple comment in the code giving
credit would be courteous but is not required.</p>

</body>

</html>

⌨️ 快捷键说明

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