This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.c: savepv() of getenv()
[perl5.git] / cpan / Pod-Simple / t / xhtml10.t
1 #!/usr/bin/perl -w
2
3 # t/xhtml01.t - check basic output from Pod::Simple::XHTML
4
5 BEGIN {
6     chdir 't' if -d 't';
7 }
8
9 use strict;
10 use lib '../lib';
11 use Test::More tests => 60;
12 #use Test::More 'no_plan';
13
14 use_ok('Pod::Simple::XHTML') or exit;
15
16 isa_ok my $parser = Pod::Simple::XHTML->new, 'Pod::Simple::XHTML';
17 my $header = $parser->html_header;
18 my $footer = $parser->html_footer;
19
20 for my $spec (
21     [ 'foo'    => 'foo',   'foo'     ],
22     [ '12foo'  => 'foo1',  'foo'     ],
23     [ 'fo$bar' => 'fo-bar', 'fo-bar' ],
24     [ 'f12'    => 'f12',    'f12'    ],
25     [ '13'     => 'pod13',  'pod13'  ],
26     [ '**.:'   => 'pod', 'pod' ],
27 ) {
28     is $parser->idify( $spec->[0] ), $spec->[1],
29         qq{ID for "$spec->[0]" should be "$spec->[1]"};
30     is $parser->idify( $spec->[0], 1 ), $spec->[2],
31         qq{Non-unique ID for "$spec->[0]" should be "$spec->[2]"};
32 }
33
34 my $results;
35
36 initialize($parser, $results);
37 $parser->html_header($header);
38 $parser->html_footer($footer);
39 ok $parser->parse_string_document( '=head1 Foo' ), 'Parse one header';
40 is $results, <<'EOF', 'Should have the index';
41
42 <html>
43 <head>
44 <title></title>
45 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
46 </head>
47 <body>
48
49
50 <ul id="index">
51   <li><a href="#Foo">Foo</a></li>
52 </ul>
53
54 <h1 id="Foo">Foo</h1>
55
56 </body>
57 </html>
58
59 EOF
60
61 initialize($parser, $results);
62 ok $parser->parse_string_document( '=head1 Foo Bar' ), 'Parse multiword header';
63 is $results, <<'EOF', 'Should have the index';
64 <ul id="index">
65   <li><a href="#Foo-Bar">Foo Bar</a></li>
66 </ul>
67
68 <h1 id="Foo-Bar">Foo Bar</h1>
69
70 EOF
71
72 initialize($parser, $results);
73 ok $parser->parse_string_document( "=head1 Foo B<Bar>\n\n=head1 Foo B<Baz>" ),
74     'Parse two multiword headers';
75 is $results, <<'EOF', 'Should have the index';
76 <ul id="index">
77   <li><a href="#Foo-Bar">Foo Bar</a></li>
78   <li><a href="#Foo-Baz">Foo Baz</a></li>
79 </ul>
80
81 <h1 id="Foo-Bar">Foo <b>Bar</b></h1>
82
83 <h1 id="Foo-Baz">Foo <b>Baz</b></h1>
84
85 EOF
86
87 initialize($parser, $results);
88 ok $parser->parse_string_document( "=head1 Foo\n\n=head1 Bar" ), 'Parse two headers';
89 is $results, <<'EOF', 'Should have both and the index';
90 <ul id="index">
91   <li><a href="#Foo">Foo</a></li>
92   <li><a href="#Bar">Bar</a></li>
93 </ul>
94
95 <h1 id="Foo">Foo</h1>
96
97 <h1 id="Bar">Bar</h1>
98
99 EOF
100
101 initialize($parser, $results);
102 ok $parser->parse_string_document( "=head1 Foo C<Bar>\n\n=head1 C<Baz>" ),
103     'Parse two headers with C<> formatting';
104 is $results, <<'EOF', 'Should have the index';
105 <ul id="index">
106   <li><a href="#Foo-Bar">Foo Bar</a></li>
107   <li><a href="#Baz">Baz</a></li>
108 </ul>
109
110 <h1 id="Foo-Bar">Foo <code>Bar</code></h1>
111
112 <h1 id="Baz"><code>Baz</code></h1>
113
114 EOF
115
116 initialize($parser, $results);
117 ok $parser->parse_string_document( "=head1 Foo\n\n=head1 Bar\n\n=head1 Baz" ),
118     'Parse three headers';
119 is $results, <<'EOF', 'Should have all three and the index';
120 <ul id="index">
121   <li><a href="#Foo">Foo</a></li>
122   <li><a href="#Bar">Bar</a></li>
123   <li><a href="#Baz">Baz</a></li>
124 </ul>
125
126 <h1 id="Foo">Foo</h1>
127
128 <h1 id="Bar">Bar</h1>
129
130 <h1 id="Baz">Baz</h1>
131
132 EOF
133
134 initialize($parser, $results);
135 ok $parser->parse_string_document( "=head1 Foo\n\n=head2 Bar" ), 'Parse two levels';
136 is $results, <<'EOF', 'Should have the dual-level index';
137 <ul id="index">
138   <li><a href="#Foo">Foo</a>
139     <ul>
140       <li><a href="#Bar">Bar</a></li>
141     </ul>
142   </li>
143 </ul>
144
145 <h1 id="Foo">Foo</h1>
146
147 <h2 id="Bar">Bar</h2>
148
149 EOF
150
151 initialize($parser, $results);
152 ok $parser->parse_string_document( "=head1 Foo\n\n=head2 Bar\n\n=head3 Baz" ),
153     'Parse three levels';
154 is $results, <<'EOF', 'Should have the three-level index';
155 <ul id="index">
156   <li><a href="#Foo">Foo</a>
157     <ul>
158       <li><a href="#Bar">Bar</a>
159         <ul>
160           <li><a href="#Baz">Baz</a></li>
161         </ul>
162       </li>
163     </ul>
164   </li>
165 </ul>
166
167 <h1 id="Foo">Foo</h1>
168
169 <h2 id="Bar">Bar</h2>
170
171 <h3 id="Baz">Baz</h3>
172
173 EOF
174
175 initialize($parser, $results);
176 ok $parser->parse_string_document( "=head1 Foo\n\n=head2 Bar\n\n=head3 Baz\n\n=head4 Howdy" ),
177     'Parse four levels';
178 is $results, <<'EOF', 'Should have the four-level index';
179 <ul id="index">
180   <li><a href="#Foo">Foo</a>
181     <ul>
182       <li><a href="#Bar">Bar</a>
183         <ul>
184           <li><a href="#Baz">Baz</a>
185             <ul>
186               <li><a href="#Howdy">Howdy</a></li>
187             </ul>
188           </li>
189         </ul>
190       </li>
191     </ul>
192   </li>
193 </ul>
194
195 <h1 id="Foo">Foo</h1>
196
197 <h2 id="Bar">Bar</h2>
198
199 <h3 id="Baz">Baz</h3>
200
201 <h4 id="Howdy">Howdy</h4>
202
203 EOF
204
205 initialize($parser, $results);
206 ok $parser->parse_string_document( "=head1 Foo\n\n=head2 Bar\n\n=head2 Baz" ),
207     'Parse 1/2';
208 is $results, <<'EOF', 'Should have the 1/s index';
209 <ul id="index">
210   <li><a href="#Foo">Foo</a>
211     <ul>
212       <li><a href="#Bar">Bar</a></li>
213       <li><a href="#Baz">Baz</a></li>
214     </ul>
215   </li>
216 </ul>
217
218 <h1 id="Foo">Foo</h1>
219
220 <h2 id="Bar">Bar</h2>
221
222 <h2 id="Baz">Baz</h2>
223
224 EOF
225
226 initialize($parser, $results);
227 ok $parser->parse_string_document( "=head1 Foo\n\n=head3 Bar" ), 'Parse jump from one to three';
228 is $results, <<'EOF', 'Should have the 1-3 index';
229 <ul id="index">
230   <li><a href="#Foo">Foo</a>
231     <ul>
232       <li>
233         <ul>
234           <li><a href="#Bar">Bar</a></li>
235         </ul>
236       </li>
237     </ul>
238   </li>
239 </ul>
240
241 <h1 id="Foo">Foo</h1>
242
243 <h3 id="Bar">Bar</h3>
244
245 EOF
246
247 initialize($parser, $results);
248 ok $parser->parse_string_document( "=head1 Foo\n\n=head4 Bar" ), 'Parse jump from one to four';
249 is $results, <<'EOF', 'Should have the 1-4 index';
250 <ul id="index">
251   <li><a href="#Foo">Foo</a>
252     <ul>
253       <li>
254         <ul>
255           <li>
256             <ul>
257               <li><a href="#Bar">Bar</a></li>
258             </ul>
259           </li>
260         </ul>
261       </li>
262     </ul>
263   </li>
264 </ul>
265
266 <h1 id="Foo">Foo</h1>
267
268 <h4 id="Bar">Bar</h4>
269
270 EOF
271
272 initialize($parser, $results);
273 ok $parser->parse_string_document( "=head2 Foo\n\n=head1 Bar" ),
274     'Parse two down to 1';
275 is $results, <<'EOF', 'Should have the 2-1 index';
276 <ul id="index">
277   <li>
278     <ul>
279       <li><a href="#Foo">Foo</a></li>
280     </ul>
281   </li>
282   <li><a href="#Bar">Bar</a></li>
283 </ul>
284
285 <h2 id="Foo">Foo</h2>
286
287 <h1 id="Bar">Bar</h1>
288
289 EOF
290
291 initialize($parser, $results);
292 ok $parser->parse_string_document( "=head2 Foo\n\n=head1 Bar\n\n=head4 Four\n\n=head4 Four2" ),
293     'Parse two down to 1';
294 is $results, <<'EOF', 'Should have the 2-1 index';
295 <ul id="index">
296   <li>
297     <ul>
298       <li><a href="#Foo">Foo</a></li>
299     </ul>
300   </li>
301   <li><a href="#Bar">Bar</a>
302     <ul>
303       <li>
304         <ul>
305           <li>
306             <ul>
307               <li><a href="#Four">Four</a></li>
308               <li><a href="#Four2">Four2</a></li>
309             </ul>
310           </li>
311         </ul>
312       </li>
313     </ul>
314   </li>
315 </ul>
316
317 <h2 id="Foo">Foo</h2>
318
319 <h1 id="Bar">Bar</h1>
320
321 <h4 id="Four">Four</h4>
322
323 <h4 id="Four2">Four2</h4>
324
325 EOF
326
327 initialize($parser, $results);
328 ok $parser->parse_string_document( "=head4 Foo" ),
329     'Parse just a four';
330 is $results, <<'EOF', 'Should have the 2-1 index';
331 <ul id="index">
332   <li>
333     <ul>
334       <li>
335         <ul>
336           <li>
337             <ul>
338               <li><a href="#Foo">Foo</a></li>
339             </ul>
340           </li>
341         </ul>
342       </li>
343     </ul>
344   </li>
345 </ul>
346
347 <h4 id="Foo">Foo</h4>
348
349 EOF
350
351 initialize($parser, $results);
352 ok $parser->parse_string_document( <<'EOF' ), 'Parse a mixture';
353 =head2 Foo
354
355 =head3 Bar
356
357 =head1 Baz
358
359 =head4 Drink
360
361 =head3 Sip
362
363 =head4 Ouch
364
365 =head1 Drip
366 EOF
367
368 is $results, <<'EOF', 'And it should work!';
369 <ul id="index">
370   <li>
371     <ul>
372       <li><a href="#Foo">Foo</a>
373         <ul>
374           <li><a href="#Bar">Bar</a></li>
375         </ul>
376       </li>
377     </ul>
378   </li>
379   <li><a href="#Baz">Baz</a>
380     <ul>
381       <li>
382         <ul>
383           <li>
384             <ul>
385               <li><a href="#Drink">Drink</a></li>
386             </ul>
387           </li>
388           <li><a href="#Sip">Sip</a>
389             <ul>
390               <li><a href="#Ouch">Ouch</a></li>
391             </ul>
392           </li>
393         </ul>
394       </li>
395     </ul>
396   </li>
397   <li><a href="#Drip">Drip</a></li>
398 </ul>
399
400 <h2 id="Foo">Foo</h2>
401
402 <h3 id="Bar">Bar</h3>
403
404 <h1 id="Baz">Baz</h1>
405
406 <h4 id="Drink">Drink</h4>
407
408 <h3 id="Sip">Sip</h3>
409
410 <h4 id="Ouch">Ouch</h4>
411
412 <h1 id="Drip">Drip</h1>
413
414 EOF
415
416 initialize($parser, $results);
417 $parser->html_header($header);
418 $parser->html_footer($footer);
419 $parser->backlink(1);
420 ok $parser->parse_string_document( '=head1 Foo' ), 'Parse a header';
421 is $results, <<'EOF', 'Should have the index and a backlink';
422
423 <html>
424 <head>
425 <title></title>
426 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
427 </head>
428 <body id="_podtop_">
429
430
431 <ul id="index">
432   <li><a href="#Foo">Foo</a></li>
433 </ul>
434
435 <a href="#_podtop_"><h1 id="Foo">Foo</h1></a>
436
437 </body>
438 </html>
439
440 EOF
441
442 initialize($parser, $results);
443 $parser->html_header($header);
444 $parser->html_footer($footer);
445 $parser->backlink(1);
446 ok $parser->parse_string_document( "=head1 Foo \n\n=head2 Bar \n\n=head1 Baz" ), 'Parse headers';
447 is $results, <<'EOF', 'Should have the index and backlinks';
448
449 <html>
450 <head>
451 <title></title>
452 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
453 </head>
454 <body id="_podtop_">
455
456
457 <ul id="index">
458   <li><a href="#Foo">Foo</a>
459     <ul>
460       <li><a href="#Bar">Bar</a></li>
461     </ul>
462   </li>
463   <li><a href="#Baz">Baz</a></li>
464 </ul>
465
466 <a href="#_podtop_"><h1 id="Foo">Foo</h1></a>
467
468 <h2 id="Bar">Bar</h2>
469
470 <a href="#_podtop_"><h1 id="Baz">Baz</h1></a>
471
472 </body>
473 </html>
474
475 EOF
476
477 initialize($parser, $results);
478 $parser->html_header($header);
479 $parser->html_footer($footer);
480 $parser->index(0);
481 $parser->backlink(1);
482 ok $parser->parse_string_document( "=head1 Foo \n\n=head1 Bar" ), 'Parse headers';
483 is $results, <<'EOF', 'Should have backlinks but no index';
484
485 <html>
486 <head>
487 <title></title>
488 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
489 </head>
490 <body id="_podtop_">
491
492
493 <a href="#_podtop_"><h1 id="Foo">Foo</h1></a>
494
495 <a href="#_podtop_"><h1 id="Bar">Bar</h1></a>
496
497 </body>
498 </html>
499
500 EOF
501
502 initialize($parser, $results);
503 $parser->html_header($header);
504 $parser->html_footer($footer);
505 $parser->backlink(1);
506 $parser->html_h_level(2);
507 ok $parser->parse_string_document( "=head1 Foo \n\n=head1 Bar" ), 'Parse headers';
508 is $results, <<'EOF', 'Should have index and backlinks around h2 elements';
509
510 <html>
511 <head>
512 <title></title>
513 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
514 </head>
515 <body id="_podtop_">
516
517
518 <ul id="index">
519   <li>
520     <ul>
521       <li><a href="#Foo">Foo</a></li>
522       <li><a href="#Bar">Bar</a></li>
523     </ul>
524   </li>
525 </ul>
526
527 <a href="#_podtop_"><h2 id="Foo">Foo</h2></a>
528
529 <a href="#_podtop_"><h2 id="Bar">Bar</h2></a>
530
531 </body>
532 </html>
533
534 EOF
535
536 initialize($parser, $results);
537 $parser->anchor_items(1);
538 ok $parser->parse_string_document( <<'EOPOD' ), 'Parse POD';
539 =head1 Foo
540
541 =over
542
543 =item test
544
545 =item Test 2
546
547 body of item
548
549 =back
550
551 =over
552
553 =item *
554
555 not anchored
556
557 =back
558
559 =over
560
561 =item 1
562
563 still not anchored
564
565 =back
566 EOPOD
567
568 is $results, <<'EOF', 'Anchor =item directives';
569 <ul id="index">
570   <li><a href="#Foo">Foo</a></li>
571 </ul>
572
573 <h1 id="Foo">Foo</h1>
574
575 <dl>
576
577 <dt id="test">test</dt>
578 <dd>
579
580 </dd>
581 <dt id="Test-2">Test 2</dt>
582 <dd>
583
584 <p>body of item</p>
585
586 </dd>
587 </dl>
588
589 <ul>
590
591 <li><p>not anchored</p>
592
593 </li>
594 </ul>
595
596 <ol>
597
598 <li><p>still not anchored</p>
599
600 </li>
601 </ol>
602
603 EOF
604
605 initialize($parser, $results);
606 $parser->anchor_items(0);
607 ok $parser->parse_string_document( <<'EOPOD' ), 'Parse POD';
608 =head1 Foo
609
610 =over
611
612 =item test
613
614 =item Test 2
615
616 body of item
617
618 =back
619
620 =over
621
622 =item *
623
624 not anchored
625
626 =back
627
628 =over
629
630 =item 1
631
632 still not anchored
633
634 =back
635 EOPOD
636 is $results, <<'EOF', 'Do not anchor =item directives';
637 <ul id="index">
638   <li><a href="#Foo">Foo</a></li>
639 </ul>
640
641 <h1 id="Foo">Foo</h1>
642
643 <dl>
644
645 <dt>test</dt>
646 <dd>
647
648 </dd>
649 <dt>Test 2</dt>
650 <dd>
651
652 <p>body of item</p>
653
654 </dd>
655 </dl>
656
657 <ul>
658
659 <li><p>not anchored</p>
660
661 </li>
662 </ul>
663
664 <ol>
665
666 <li><p>still not anchored</p>
667
668 </li>
669 </ol>
670
671 EOF
672
673 $ENV{FOO}= 1;
674
675 initialize($parser, $results);
676 ok $parser->parse_string_document( <<'EOPOD' ), 'Parse POD';
677 =head1 Foo
678
679 Test links from perlpodspec: L</"About LE<lt>...E<gt> Codes">
680
681 =head1 About LE<lt>...E<gt> Codes
682
683 Here it is
684 EOPOD
685
686 my $id = 'About-L...-Codes'; # what should this be?
687
688 is $results, <<EOF, 'anchor and h1 use same section id for complex sections';
689 <ul id="index">
690   <li><a href="#Foo">Foo</a></li>
691   <li><a href="#$id">About L&lt;...&gt; Codes</a></li>
692 </ul>
693
694 <h1 id="Foo">Foo</h1>
695
696 <p>Test links from perlpodspec: <a href="#$id">&quot;About L&lt;...&gt; Codes&quot;</a></p>
697
698 <h1 id="$id">About L&lt;...&gt; Codes</h1>
699
700 <p>Here it is</p>
701
702 EOF
703
704 sub initialize {
705         $_[0] = Pod::Simple::XHTML->new;
706         $_[0]->html_header('');
707         $_[0]->html_footer('');
708         $_[0]->index(1);
709         $_[0]->output_string( \$results ); # Send the resulting output to a string
710         $_[1] = '';
711         return;
712 }