Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / Test::Tutorial.3pm
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  | will give a
29 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
32 .tr \(*W-|\(bv\*(Tr
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD.  Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
58 ..
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "Test::Tutorial 3"
132 .TH Test::Tutorial 3 "2009-09-02" "perl v5.8.7" "User Contributed Perl Documentation"
133 .SH "NAME"
134 Test::Tutorial \- A tutorial about writing really basic tests
135 .SH "DESCRIPTION"
136 .IX Header "DESCRIPTION"
137 \&\fI\s-1AHHHHHHH\s0!!!!  \s-1NOT\s0 \s-1TESTING\s0!  Anything but testing!  
138 Beat me, whip me, send me to Detroit, but don't make 
139 me write tests!\fR
140 .PP
141 \&\fI*sob*\fR
142 .PP
143 \&\fIBesides, I don't know how to write the damned things.\fR
144 .PP
145 Is this you?  Is writing tests right up there with writing
146 documentation and having your fingernails pulled out?  Did you open up
147 a test and read 
148 .PP
149 .Vb 1
150 \&    ######## We start with some black magic
151 .Ve
152 .PP
153 and decide that's quite enough for you?
154 .PP
155 It's ok.  That's all gone now.  We've done all the black magic for
156 you.  And here are the tricks...
157 .Sh "Nuts and bolts of testing."
158 .IX Subsection "Nuts and bolts of testing."
159 Here's the most basic test program.
160 .PP
161 .Vb 1
162 \&    #!/usr/bin/perl \-w
163 .Ve
164 .PP
165 .Vb 1
166 \&    print "1..1\en";
167 .Ve
168 .PP
169 .Vb 1
170 \&    print 1 + 1 == 2 ? "ok 1\en" : "not ok 1\en";
171 .Ve
172 .PP
173 since 1 + 1 is 2, it prints:
174 .PP
175 .Vb 2
176 \&    1..1
177 \&    ok 1
178 .Ve
179 .PP
180 What this says is: \f(CW1..1\fR \*(L"I'm going to run one test.\*(R" [1] \f(CW\*(C`ok 1\*(C'\fR
181 \&\*(L"The first test passed\*(R".  And that's about all magic there is to
182 testing.  Your basic unit of testing is the \fIok\fR.  For each thing you
183 test, an \f(CW\*(C`ok\*(C'\fR is printed.  Simple.  \fBTest::Harness\fR interprets your test
184 results to determine if you succeeded or failed (more on that later).
185 .PP
186 Writing all these print statements rapidly gets tedious.  Fortunately,
187 there's \fBTest::Simple\fR.  It has one function, \f(CW\*(C`ok()\*(C'\fR.
188 .PP
189 .Vb 1
190 \&    #!/usr/bin/perl \-w
191 .Ve
192 .PP
193 .Vb 1
194 \&    use Test::Simple tests => 1;
195 .Ve
196 .PP
197 .Vb 1
198 \&    ok( 1 + 1 == 2 );
199 .Ve
200 .PP
201 and that does the same thing as the code above.  \f(CW\*(C`ok()\*(C'\fR is the backbone
202 of Perl testing, and we'll be using it instead of roll-your-own from
203 here on.  If \f(CW\*(C`ok()\*(C'\fR gets a true value, the test passes.  False, it
204 fails.
205 .PP
206 .Vb 1
207 \&    #!/usr/bin/perl \-w
208 .Ve
209 .PP
210 .Vb 3
211 \&    use Test::Simple tests => 2;
212 \&    ok( 1 + 1 == 2 );
213 \&    ok( 2 + 2 == 5 );
214 .Ve
215 .PP
216 from that comes
217 .PP
218 .Vb 5
219 \&    1..2
220 \&    ok 1
221 \&    not ok 2
222 \&    #     Failed test (test.pl at line 5)
223 \&    # Looks like you failed 1 tests of 2.
224 .Ve
225 .PP
226 \&\f(CW1..2\fR \*(L"I'm going to run two tests.\*(R"  This number is used to ensure
227 your test program ran all the way through and didn't die or skip some
228 tests.  \f(CW\*(C`ok 1\*(C'\fR \*(L"The first test passed.\*(R"  \f(CW\*(C`not ok 2\*(C'\fR \*(L"The second test
229 failed\*(R".  Test::Simple helpfully prints out some extra commentary about
230 your tests.
231 .PP
232 It's not scary.  Come, hold my hand.  We're going to give an example
233 of testing a module.  For our example, we'll be testing a date
234 library, \fBDate::ICal\fR.  It's on \s-1CPAN\s0, so download a copy and follow
235 along. [2]
236 .Sh "Where to start?"
237 .IX Subsection "Where to start?"
238 This is the hardest part of testing, where do you start?  People often
239 get overwhelmed at the apparent enormity of the task of testing a
240 whole module.  Best place to start is at the beginning.  Date::ICal is
241 an object-oriented module, and that means you start by making an
242 object.  So we test \f(CW\*(C`new()\*(C'\fR.
243 .PP
244 .Vb 1
245 \&    #!/usr/bin/perl \-w
246 .Ve
247 .PP
248 .Vb 1
249 \&    use Test::Simple tests => 2;
250 .Ve
251 .PP
252 .Vb 1
253 \&    use Date::ICal;
254 .Ve
255 .PP
256 .Vb 3
257 \&    my $ical = Date::ICal\->new;         # create an object
258 \&    ok( defined $ical );                # check that we got something
259 \&    ok( $ical\->isa('Date::ICal') );     # and it's the right class
260 .Ve
261 .PP
262 run that and you should get:
263 .PP
264 .Vb 3
265 \&    1..2
266 \&    ok 1
267 \&    ok 2
268 .Ve
269 .PP
270 congratulations, you've written your first useful test.
271 .Sh "Names"
272 .IX Subsection "Names"
273 That output isn't terribly descriptive, is it?  When you have two
274 tests you can figure out which one is #2, but what if you have 102?
275 .PP
276 Each test can be given a little descriptive name as the second
277 argument to \f(CW\*(C`ok()\*(C'\fR.
278 .PP
279 .Vb 1
280 \&    use Test::Simple tests => 2;
281 .Ve
282 .PP
283 .Vb 2
284 \&    ok( defined $ical,              'new() returned something' );
285 \&    ok( $ical\->isa('Date::ICal'),   "  and it's the right class" );
286 .Ve
287 .PP
288 So now you'd see...
289 .PP
290 .Vb 3
291 \&    1..2
292 \&    ok 1 \- new() returned something
293 \&    ok 2 \-   and it's the right class
294 .Ve
295 .Sh "Test the manual"
296 .IX Subsection "Test the manual"
297 Simplest way to build up a decent testing suite is to just test what
298 the manual says it does. [3] Let's pull something out of the 
299 \&\*(L"\s-1SYNOPSIS\s0\*(R" in Date::ICal and test that all its bits work.
300 .PP
301 .Vb 1
302 \&    #!/usr/bin/perl \-w
303 .Ve
304 .PP
305 .Vb 1
306 \&    use Test::Simple tests => 8;
307 .Ve
308 .PP
309 .Vb 1
310 \&    use Date::ICal;
311 .Ve
312 .PP
313 .Vb 3
314 \&    $ical = Date::ICal\->new( year => 1964, month => 10, day => 16, 
315 \&                             hour => 16, min => 12, sec => 47, 
316 \&                             tz => '0530' );
317 .Ve
318 .PP
319 .Vb 8
320 \&    ok( defined $ical,            'new() returned something' );
321 \&    ok( $ical\->isa('Date::ICal'), "  and it's the right class" );
322 \&    ok( $ical\->sec   == 47,       '  sec()'   );
323 \&    ok( $ical\->min   == 12,       '  min()'   );    
324 \&    ok( $ical\->hour  == 16,       '  hour()'  );
325 \&    ok( $ical\->day   == 17,       '  day()'   );
326 \&    ok( $ical\->month == 10,       '  month()' );
327 \&    ok( $ical\->year  == 1964,     '  year()'  );
328 .Ve
329 .PP
330 run that and you get:
331 .PP
332 .Vb 11
333 \&    1..8
334 \&    ok 1 \- new() returned something
335 \&    ok 2 \-   and it's the right class
336 \&    ok 3 \-   sec()
337 \&    ok 4 \-   min()
338 \&    ok 5 \-   hour()
339 \&    not ok 6 \-   day()
340 \&    #     Failed test (\- at line 16)
341 \&    ok 7 \-   month()
342 \&    ok 8 \-   year()
343 \&    # Looks like you failed 1 tests of 8.
344 .Ve
345 .PP
346 Whoops, a failure! [4] Test::Simple helpfully lets us know on what line
347 the failure occurred, but not much else.  We were supposed to get 17,
348 but we didn't.  What did we get??  Dunno.  We'll have to re-run the
349 test in the debugger or throw in some print statements to find out.
350 .PP
351 Instead, we'll switch from \fBTest::Simple\fR to \fBTest::More\fR.  \fBTest::More\fR
352 does everything \fBTest::Simple\fR does, and more!  In fact, Test::More does
353 things \fIexactly\fR the way Test::Simple does.  You can literally swap
354 Test::Simple out and put Test::More in its place.  That's just what
355 we're going to do.
356 .PP
357 Test::More does more than Test::Simple.  The most important difference
358 at this point is it provides more informative ways to say \*(L"ok\*(R".
359 Although you can write almost any test with a generic \f(CW\*(C`ok()\*(C'\fR, it
360 can't tell you what went wrong.  Instead, we'll use the \f(CW\*(C`is()\*(C'\fR
361 function, which lets us declare that something is supposed to be the
362 same as something else:
363 .PP
364 .Vb 1
365 \&    #!/usr/bin/perl \-w
366 .Ve
367 .PP
368 .Vb 1
369 \&    use Test::More tests => 8;
370 .Ve
371 .PP
372 .Vb 1
373 \&    use Date::ICal;
374 .Ve
375 .PP
376 .Vb 3
377 \&    $ical = Date::ICal\->new( year => 1964, month => 10, day => 16, 
378 \&                             hour => 16, min => 12, sec => 47, 
379 \&                             tz => '0530' );
380 .Ve
381 .PP
382 .Vb 8
383 \&    ok( defined $ical,            'new() returned something' );
384 \&    ok( $ical\->isa('Date::ICal'), "  and it's the right class" );
385 \&    is( $ical\->sec,     47,       '  sec()'   );
386 \&    is( $ical\->min,     12,       '  min()'   );    
387 \&    is( $ical\->hour,    16,       '  hour()'  );
388 \&    is( $ical\->day,     17,       '  day()'   );
389 \&    is( $ical\->month,   10,       '  month()' );
390 \&    is( $ical\->year,    1964,     '  year()'  );
391 .Ve
392 .PP
393 "Is \f(CW\*(C`$ical\->sec\*(C'\fR 47?\*(L"  \*(R"Is \f(CW\*(C`$ical\->min\*(C'\fR 12?"  With \f(CW\*(C`is()\*(C'\fR in place,
394 you get some more information
395 .PP
396 .Vb 13
397 \&    1..8
398 \&    ok 1 \- new() returned something
399 \&    ok 2 \-   and it's the right class
400 \&    ok 3 \-   sec()
401 \&    ok 4 \-   min()
402 \&    ok 5 \-   hour()
403 \&    not ok 6 \-   day()
404 \&    #     Failed test (\- at line 16)
405 \&    #          got: '16'
406 \&    #     expected: '17'
407 \&    ok 7 \-   month()
408 \&    ok 8 \-   year()
409 \&    # Looks like you failed 1 tests of 8.
410 .Ve
411 .PP
412 letting us know that \f(CW\*(C`$ical\->day\*(C'\fR returned 16, but we expected 17.  A
413 quick check shows that the code is working fine, we made a mistake
414 when writing up the tests.  Just change it to:
415 .PP
416 .Vb 1
417 \&    is( $ical\->day,     16,       '  day()'   );
418 .Ve
419 .PP
420 and everything works.
421 .PP
422 So any time you're doing a \*(L"this equals that\*(R" sort of test, use \f(CW\*(C`is()\*(C'\fR.
423 It even works on arrays.  The test is always in scalar context, so you
424 can test how many elements are in a list this way. [5]
425 .PP
426 .Vb 1
427 \&    is( @foo, 5, 'foo has 5 elements' );
428 .Ve
429 .Sh "Sometimes the tests are wrong"
430 .IX Subsection "Sometimes the tests are wrong"
431 Which brings us to a very important lesson.  Code has bugs.  Tests are
432 code.  Ergo, tests have bugs.  A failing test could mean a bug in the
433 code, but don't discount the possibility that the test is wrong.
434 .PP
435 On the flip side, don't be tempted to prematurely declare a test
436 incorrect just because you're having trouble finding the bug.
437 Invalidating a test isn't something to be taken lightly, and don't use
438 it as a cop out to avoid work.
439 .Sh "Testing lots of values"
440 .IX Subsection "Testing lots of values"
441 We're going to be wanting to test a lot of dates here, trying to trick
442 the code with lots of different edge cases.  Does it work before 1970?
443 After 2038?  Before 1904?  Do years after 10,000 give it trouble?
444 Does it get leap years right?  We could keep repeating the code above,
445 or we could set up a little try/expect loop.
446 .PP
447 .Vb 2
448 \&    use Test::More tests => 32;
449 \&    use Date::ICal;
450 .Ve
451 .PP
452 .Vb 12
453 \&    my %ICal_Dates = (
454 \&            # An ICal string     And the year, month, day
455 \&            #                    hour, minute and second we expect.
456 \&            '19971024T120000' =>    # from the docs.
457 \&                                [ 1997, 10, 24, 12,  0,  0 ],
458 \&            '20390123T232832' =>    # after the Unix epoch
459 \&                                [ 2039,  1, 23, 23, 28, 32 ],
460 \&            '19671225T000000' =>    # before the Unix epoch
461 \&                                [ 1967, 12, 25,  0,  0,  0 ],
462 \&            '18990505T232323' =>    # before the MacOS epoch
463 \&                                [ 1899,  5,  5, 23, 23, 23 ],
464 \&    );
465 .Ve
466 .PP
467 .Vb 2
468 \&    while( my($ical_str, $expect) = each %ICal_Dates ) {
469 \&        my $ical = Date::ICal\->new( ical => $ical_str );
470 .Ve
471 .PP
472 .Vb 2
473 \&        ok( defined $ical,            "new(ical => '$ical_str')" );
474 \&        ok( $ical\->isa('Date::ICal'), "  and it's the right class" );
475 .Ve
476 .PP
477 .Vb 7
478 \&        is( $ical\->year,    $expect\->[0],     '  year()'  );
479 \&        is( $ical\->month,   $expect\->[1],     '  month()' );
480 \&        is( $ical\->day,     $expect\->[2],     '  day()'   );
481 \&        is( $ical\->hour,    $expect\->[3],     '  hour()'  );
482 \&        is( $ical\->min,     $expect\->[4],     '  min()'   );    
483 \&        is( $ical\->sec,     $expect\->[5],     '  sec()'   );
484 \&    }
485 .Ve
486 .PP
487 So now we can test bunches of dates by just adding them to
488 \&\f(CW%ICal_Dates\fR.  Now that it's less work to test with more dates, you'll
489 be inclined to just throw more in as you think of them.
490 Only problem is, every time we add to that we have to keep adjusting
491 the \f(CW\*(C`use Test::More tests => ##\*(C'\fR line.  That can rapidly get
492 annoying.  There's two ways to make this work better.
493 .PP
494 First, we can calculate the plan dynamically using the \f(CW\*(C`plan()\*(C'\fR
495 function.
496 .PP
497 .Vb 2
498 \&    use Test::More;
499 \&    use Date::ICal;
500 .Ve
501 .PP
502 .Vb 3
503 \&    my %ICal_Dates = (
504 \&        ...same as before...
505 \&    );
506 .Ve
507 .PP
508 .Vb 2
509 \&    # For each key in the hash we're running 8 tests.
510 \&    plan tests => keys(%ICal_Dates) * 8;
511 .Ve
512 .PP
513 .Vb 1
514 \&    ...and then your tests...
515 .Ve
516 .PP
517 Or to be even more flexible, we use \f(CW\*(C`no_plan\*(C'\fR.  This means we're just
518 running some tests, don't know how many. [6]
519 .PP
520 .Vb 1
521 \&    use Test::More 'no_plan';   # instead of tests => 32
522 .Ve
523 .PP
524 now we can just add tests and not have to do all sorts of math to
525 figure out how many we're running.
526 .Sh "Informative names"
527 .IX Subsection "Informative names"
528 Take a look at this line here
529 .PP
530 .Vb 1
531 \&    ok( defined $ical,            "new(ical => '$ical_str')" );
532 .Ve
533 .PP
534 we've added more detail about what we're testing and the ICal string
535 itself we're trying out to the name.  So you get results like:
536 .PP
537 .Vb 8
538 \&    ok 25 \- new(ical => '19971024T120000')
539 \&    ok 26 \-   and it's the right class
540 \&    ok 27 \-   year()
541 \&    ok 28 \-   month()
542 \&    ok 29 \-   day()
543 \&    ok 30 \-   hour()
544 \&    ok 31 \-   min()
545 \&    ok 32 \-   sec()
546 .Ve
547 .PP
548 if something in there fails, you'll know which one it was and that
549 will make tracking down the problem easier.  So try to put a bit of
550 debugging information into the test names.
551 .PP
552 Describe what the tests test, to make debugging a failed test easier
553 for you or for the next person who runs your test.
554 .Sh "Skipping tests"
555 .IX Subsection "Skipping tests"
556 Poking around in the existing Date::ICal tests, I found this in
557 \&\fIt/01sanity.t\fR [7]
558 .PP
559 .Vb 1
560 \&    #!/usr/bin/perl \-w
561 .Ve
562 .PP
563 .Vb 2
564 \&    use Test::More tests => 7;
565 \&    use Date::ICal;
566 .Ve
567 .PP
568 .Vb 3
569 \&    # Make sure epoch time is being handled sanely.
570 \&    my $t1 = Date::ICal\->new( epoch => 0 );
571 \&    is( $t1\->epoch, 0,          "Epoch time of 0" );
572 .Ve
573 .PP
574 .Vb 2
575 \&    # XXX This will only work on unix systems.
576 \&    is( $t1\->ical, '19700101Z', "  epoch to ical" );
577 .Ve
578 .PP
579 .Vb 3
580 \&    is( $t1\->year,  1970,       "  year()"  );
581 \&    is( $t1\->month, 1,          "  month()" );
582 \&    is( $t1\->day,   1,          "  day()"   );
583 .Ve
584 .PP
585 .Vb 3
586 \&    # like the tests above, but starting with ical instead of epoch
587 \&    my $t2 = Date::ICal\->new( ical => '19700101Z' );
588 \&    is( $t2\->ical, '19700101Z', "Start of epoch in ICal notation" );
589 .Ve
590 .PP
591 .Vb 1
592 \&    is( $t2\->epoch, 0,          "  and back to ICal" );
593 .Ve
594 .PP
595 The beginning of the epoch is different on most non-Unix operating
596 systems [8].  Even though Perl smooths out the differences for the
597 most part, certain ports do it differently.  MacPerl is one off the
598 top of my head. [9]  So rather than just putting a comment in the test,
599 we can explicitly say it's never going to work and skip the test.
600 .PP
601 .Vb 2
602 \&    use Test::More tests => 7;
603 \&    use Date::ICal;
604 .Ve
605 .PP
606 .Vb 3
607 \&    # Make sure epoch time is being handled sanely.
608 \&    my $t1 = Date::ICal\->new( epoch => 0 );
609 \&    is( $t1\->epoch, 0,          "Epoch time of 0" );
610 .Ve
611 .PP
612 .Vb 3
613 \&    SKIP: {
614 \&        skip('epoch to ICal not working on MacOS', 6) 
615 \&            if $^O eq 'MacOS';
616 .Ve
617 .PP
618 .Vb 1
619 \&        is( $t1\->ical, '19700101Z', "  epoch to ical" );
620 .Ve
621 .PP
622 .Vb 3
623 \&        is( $t1\->year,  1970,       "  year()"  );
624 \&        is( $t1\->month, 1,          "  month()" );
625 \&        is( $t1\->day,   1,          "  day()"   );
626 .Ve
627 .PP
628 .Vb 3
629 \&        # like the tests above, but starting with ical instead of epoch
630 \&        my $t2 = Date::ICal\->new( ical => '19700101Z' );
631 \&        is( $t2\->ical, '19700101Z', "Start of epoch in ICal notation" );
632 .Ve
633 .PP
634 .Vb 2
635 \&        is( $t2\->epoch, 0,          "  and back to ICal" );
636 \&    }
637 .Ve
638 .PP
639 A little bit of magic happens here.  When running on anything but
640 MacOS, all the tests run normally.  But when on MacOS, \f(CW\*(C`skip()\*(C'\fR causes
641 the entire contents of the \s-1SKIP\s0 block to be jumped over.  It's never
642 run.  Instead, it prints special output that tells Test::Harness that
643 the tests have been skipped.
644 .PP
645 .Vb 8
646 \&    1..7
647 \&    ok 1 \- Epoch time of 0
648 \&    ok 2 # skip epoch to ICal not working on MacOS
649 \&    ok 3 # skip epoch to ICal not working on MacOS
650 \&    ok 4 # skip epoch to ICal not working on MacOS
651 \&    ok 5 # skip epoch to ICal not working on MacOS
652 \&    ok 6 # skip epoch to ICal not working on MacOS
653 \&    ok 7 # skip epoch to ICal not working on MacOS
654 .Ve
655 .PP
656 This means your tests won't fail on MacOS.  This means less emails
657 from MacPerl users telling you about failing tests that you know will
658 never work.  You've got to be careful with skip tests.  These are for
659 tests which don't work and \fInever will\fR.  It is not for skipping
660 genuine bugs (we'll get to that in a moment).
661 .PP
662 The tests are wholly and completely skipped. [10]  This will work.
663 .PP
664 .Vb 2
665 \&    SKIP: {
666 \&        skip("I don't wanna die!");
667 .Ve
668 .PP
669 .Vb 2
670 \&        die, die, die, die, die;
671 \&    }
672 .Ve
673 .Sh "Todo tests"
674 .IX Subsection "Todo tests"
675 Thumbing through the Date::ICal man page, I came across this:
676 .PP
677 .Vb 1
678 \&   ical
679 .Ve
680 .PP
681 .Vb 1
682 \&       $ical_string = $ical\->ical;
683 .Ve
684 .PP
685 .Vb 2
686 \&   Retrieves, or sets, the date on the object, using any
687 \&   valid ICal date/time string.
688 .Ve
689 .PP
690 \&\*(L"Retrieves or sets\*(R".  Hmmm, didn't see a test for using \f(CW\*(C`ical()\*(C'\fR to set
691 the date in the Date::ICal test suite.  So I'll write one.
692 .PP
693 .Vb 2
694 \&    use Test::More tests => 1;
695 \&    use Date::ICal;
696 .Ve
697 .PP
698 .Vb 3
699 \&    my $ical = Date::ICal\->new;
700 \&    $ical\->ical('20201231Z');
701 \&    is( $ical\->ical, '20201231Z',   'Setting via ical()' );
702 .Ve
703 .PP
704 run that and I get
705 .PP
706 .Vb 6
707 \&    1..1
708 \&    not ok 1 \- Setting via ical()
709 \&    #     Failed test (\- at line 6)
710 \&    #          got: '20010814T233649Z'
711 \&    #     expected: '20201231Z'
712 \&    # Looks like you failed 1 tests of 1.
713 .Ve
714 .PP
715 Whoops!  Looks like it's unimplemented.  Let's assume we don't have
716 the time to fix this. [11] Normally, you'd just comment out the test
717 and put a note in a todo list somewhere.  Instead, we're going to
718 explicitly state \*(L"this test will fail\*(R" by wrapping it in a \f(CW\*(C`TODO\*(C'\fR block.
719 .PP
720 .Vb 1
721 \&    use Test::More tests => 1;
722 .Ve
723 .PP
724 .Vb 2
725 \&    TODO: {
726 \&        local $TODO = 'ical($ical) not yet implemented';
727 .Ve
728 .PP
729 .Vb 2
730 \&        my $ical = Date::ICal\->new;
731 \&        $ical\->ical('20201231Z');
732 .Ve
733 .PP
734 .Vb 2
735 \&        is( $ical\->ical, '20201231Z',   'Setting via ical()' );
736 \&    }
737 .Ve
738 .PP
739 Now when you run, it's a little different:
740 .PP
741 .Vb 4
742 \&    1..1
743 \&    not ok 1 \- Setting via ical() # TODO ical($ical) not yet implemented
744 \&    #          got: '20010822T201551Z'
745 \&    #     expected: '20201231Z'
746 .Ve
747 .PP
748 Test::More doesn't say \*(L"Looks like you failed 1 tests of 1\*(R".  That '#
749 \&\s-1TODO\s0' tells Test::Harness \*(L"this is supposed to fail\*(R" and it treats a
750 failure as a successful test.  So you can write tests even before
751 you've fixed the underlying code.
752 .PP
753 If a \s-1TODO\s0 test passes, Test::Harness will report it \*(L"\s-1UNEXPECTEDLY\s0
754 \&\s-1SUCCEEDED\s0\*(R".  When that happens, you simply remove the \s-1TODO\s0 block with
755 \&\f(CW\*(C`local $TODO\*(C'\fR and turn it into a real test.
756 .Sh "Testing with taint mode."
757 .IX Subsection "Testing with taint mode."
758 Taint mode is a funny thing.  It's the globalest of all global
759 features.  Once you turn it on, it affects \fIall\fR code in your program
760 and \fIall\fR modules used (and all the modules they use).  If a single
761 piece of code isn't taint clean, the whole thing explodes.  With that
762 in mind, it's very important to ensure your module works under taint
763 mode.
764 .PP
765 It's very simple to have your tests run under taint mode.  Just throw
766 a \f(CW\*(C`\-T\*(C'\fR into the \f(CW\*(C`#!\*(C'\fR line.  Test::Harness will read the switches
767 in \f(CW\*(C`#!\*(C'\fR and use them to run your tests.
768 .PP
769 .Vb 1
770 \&    #!/usr/bin/perl \-Tw
771 .Ve
772 .PP
773 .Vb 1
774 \&    ...test normally here...
775 .Ve
776 .PP
777 So when you say \f(CW\*(C`make test\*(C'\fR it will be run with taint mode and
778 warnings on.
779 .SH "FOOTNOTES"
780 .IX Header "FOOTNOTES"
781 .IP "1" 4
782 .IX Item "1"
783 The first number doesn't really mean anything, but it has to be 1.
784 It's the second number that's important.
785 .IP "2" 4
786 .IX Item "2"
787 For those following along at home, I'm using version 1.31.  It has
788 some bugs, which is good \*(-- we'll uncover them with our tests.
789 .IP "3" 4
790 .IX Item "3"
791 You can actually take this one step further and test the manual
792 itself.  Have a look at \fBTest::Inline\fR (formerly \fBPod::Tests\fR).
793 .IP "4" 4
794 .IX Item "4"
795 Yes, there's a mistake in the test suite.  What!  Me, contrived?
796 .IP "5" 4
797 .IX Item "5"
798 We'll get to testing the contents of lists later.
799 .IP "6" 4
800 .IX Item "6"
801 But what happens if your test program dies halfway through?!  Since we
802 didn't say how many tests we're going to run, how can we know it
803 failed?  No problem, Test::More employs some magic to catch that death
804 and turn the test into a failure, even if every test passed up to that
805 point.
806 .IP "7" 4
807 .IX Item "7"
808 I cleaned it up a little.
809 .IP "8" 4
810 .IX Item "8"
811 Most Operating Systems record time as the number of seconds since a
812 certain date.  This date is the beginning of the epoch.  Unix's starts
813 at midnight January 1st, 1970 \s-1GMT\s0.
814 .IP "9" 4
815 .IX Item "9"
816 MacOS's epoch is midnight January 1st, 1904.  \s-1VMS\s0's is midnight,
817 November 17th, 1858, but vmsperl emulates the Unix epoch so it's not a
818 problem.
819 .IP "10" 4
820 .IX Item "10"
821 As long as the code inside the \s-1SKIP\s0 block at least compiles.  Please
822 don't ask how.  No, it's not a filter.
823 .IP "11" 4
824 .IX Item "11"
825 Do \s-1NOT\s0 be tempted to use \s-1TODO\s0 tests as a way to avoid fixing simple
826 bugs!
827 .SH "AUTHORS"
828 .IX Header "AUTHORS"
829 Michael G Schwern <schwern@pobox.com> and the perl-qa dancers!
830 .SH "COPYRIGHT"
831 .IX Header "COPYRIGHT"
832 Copyright 2001 by Michael G Schwern <schwern@pobox.com>.
833 .PP
834 This documentation is free; you can redistribute it and/or modify it
835 under the same terms as Perl itself.
836 .PP
837 Irrespective of its distribution, all code examples in these files
838 are hereby placed into the public domain.  You are permitted and
839 encouraged to use this code in your own programs for fun
840 or for profit as you see fit.  A simple comment in the code giving
841 credit would be courteous but is not required.