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 / xhtml01.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 => 62;
12 #use Test::More 'no_plan';
13
14 use_ok('Pod::Simple::XHTML') or exit;
15
16 my $parser = Pod::Simple::XHTML->new ();
17 isa_ok ($parser, 'Pod::Simple::XHTML');
18
19 my $results;
20
21 my $PERLDOC = "http://search.cpan.org/perldoc";
22 my $MANURL = "http://man.he.net/man";
23
24 initialize($parser, $results);
25 $parser->parse_string_document( "=head1 Poit!" );
26 is($results, qq{<h1 id="Poit">Poit!</h1>\n\n}, "head1 level output");
27
28 initialize($parser, $results);
29 $parser->parse_string_document( "=head2 Yada Yada Operator
30 X<...> X<... operator> X<yada yada operator>" );
31 is($results, qq{<h2 id="Yada-Yada-Operator">Yada Yada Operator   </h2>\n\n}, "head ID with X<>");
32
33 initialize($parser, $results);
34 $parser->parse_string_document( "=head2 Platforms with no supporting programmers:");
35 is($results, qq{<h2 id="Platforms-with-no-supporting-programmers">Platforms with no supporting programmers:</h2>\n\n}, "head ID ending in colon");
36
37 initialize($parser, $results);
38 $parser->html_h_level(2);
39 $parser->parse_string_document( "=head1 Poit!" );
40 is($results, qq{<h2 id="Poit">Poit!</h2>\n\n}, "head1 level output h_level 2");
41
42 initialize($parser, $results);
43 $parser->parse_string_document( "=head2 I think so Brain." );
44 is($results, qq{<h2 id="I-think-so-Brain">I think so Brain.</h2>\n\n}, "head2 level output");
45
46 initialize($parser, $results);
47 $parser->parse_string_document( "=head3 I say, Brain..." );
48 is($results, qq{<h3 id="I-say-Brain">I say, Brain...</h3>\n\n}, "head3 level output");
49
50 initialize($parser, $results);
51 $parser->parse_string_document( "=head4 Zort & Zog!" );
52 is($results, qq{<h4 id="Zort-Zog">Zort &amp; Zog!</h4>\n\n}, "head4 level output");
53
54 sub x ($;&) {
55   my $code = $_[1];
56   Pod::Simple::XHTML->_out(
57   sub { $code->($_[0]) if $code },
58   "=pod\n\n$_[0]",
59 ) }
60
61 like(
62   x("=head1 Header\n\n=for html <div>RAW<span>!</span></div>\n\nDone."),
63   qr/.+<\/h1>\s+<div>RAW<span>!<\/span><\/div>\s+.*/sm,
64   "heading building"
65 ) or exit;
66
67 initialize($parser, $results);
68 $parser->parse_string_document(<<'EOPOD');
69 =pod
70
71 Gee, Brain, what do you want to do tonight?
72 EOPOD
73
74 is($results, <<'EOHTML', "simple paragraph");
75 <p>Gee, Brain, what do you want to do tonight?</p>
76
77 EOHTML
78
79
80 initialize($parser, $results);
81 $parser->parse_string_document(<<'EOPOD');
82 =pod
83
84 B: Now, Pinky, if by any chance you are captured during this mission,
85 remember you are Gunther Heindriksen from Appenzell. You moved to
86 Grindelwald to drive the cog train to Murren. Can you repeat that?
87
88 P: Mmmm, no, Brain, dont think I can.
89 EOPOD
90
91 is($results, <<'EOHTML', "multiple paragraphs");
92 <p>B: Now, Pinky, if by any chance you are captured during this mission, remember you are Gunther Heindriksen from Appenzell. You moved to Grindelwald to drive the cog train to Murren. Can you repeat that?</p>
93
94 <p>P: Mmmm, no, Brain, dont think I can.</p>
95
96 EOHTML
97
98 initialize($parser, $results);
99 $parser->parse_string_document(<<'EOPOD');
100 =over
101
102 =item *
103
104 P: Gee, Brain, what do you want to do tonight?
105
106 =item *
107
108 B: The same thing we do every night, Pinky. Try to take over the world!
109
110 =back
111
112 EOPOD
113
114 is($results, <<'EOHTML', "simple bulleted list");
115 <ul>
116
117 <li><p>P: Gee, Brain, what do you want to do tonight?</p>
118
119 </li>
120 <li><p>B: The same thing we do every night, Pinky. Try to take over the world!</p>
121
122 </li>
123 </ul>
124
125 EOHTML
126
127
128 initialize($parser, $results);
129 $parser->parse_string_document(<<'EOPOD');
130 =over
131
132 =item *
133
134 P: Gee, Brain, what do you want to do tonight?
135
136 =item *
137
138 B: The same thing we do every night, Pinky. Try to take over the world!
139
140 =over
141
142 =item *
143
144 Take over world
145
146 =item *
147
148 Do laundry
149
150 =back
151
152 =back
153
154 EOPOD
155
156 is($results, <<'EOHTML', "nested bulleted list");
157 <ul>
158
159 <li><p>P: Gee, Brain, what do you want to do tonight?</p>
160
161 </li>
162 <li><p>B: The same thing we do every night, Pinky. Try to take over the world!</p>
163
164 <ul>
165
166 <li><p>Take over world</p>
167
168 </li>
169 <li><p>Do laundry</p>
170
171 </li>
172 </ul>
173
174 </li>
175 </ul>
176
177 EOHTML
178
179
180
181 initialize($parser, $results);
182 $parser->parse_string_document(<<'EOPOD');
183 =over
184
185 =item 1
186
187 P: Gee, Brain, what do you want to do tonight?
188
189 =item 2
190
191 B: The same thing we do every night, Pinky. Try to take over the world!
192
193 =back
194
195 EOPOD
196
197 is($results, <<'EOHTML', "numbered list");
198 <ol>
199
200 <li><p>P: Gee, Brain, what do you want to do tonight?</p>
201
202 </li>
203 <li><p>B: The same thing we do every night, Pinky. Try to take over the world!</p>
204
205 </li>
206 </ol>
207
208 EOHTML
209
210
211 initialize($parser, $results);
212 $parser->parse_string_document(<<'EOPOD');
213 =over
214
215 =item 1
216
217 P: Gee, Brain, what do you want to do tonight?
218
219 =item 2
220
221 B: The same thing we do every night, Pinky. Try to take over the world!
222
223 =over
224
225 =item 1
226
227 Take over world
228
229 =item 2
230
231 Do laundry
232
233 =back
234
235 =back
236
237 EOPOD
238
239 is($results, <<'EOHTML', "nested numbered list");
240 <ol>
241
242 <li><p>P: Gee, Brain, what do you want to do tonight?</p>
243
244 </li>
245 <li><p>B: The same thing we do every night, Pinky. Try to take over the world!</p>
246
247 <ol>
248
249 <li><p>Take over world</p>
250
251 </li>
252 <li><p>Do laundry</p>
253
254 </li>
255 </ol>
256
257 </li>
258 </ol>
259
260 EOHTML
261
262
263 initialize($parser, $results);
264 $parser->parse_string_document(<<'EOPOD');
265 =over
266
267 =item Pinky
268
269 Gee, Brain, what do you want to do tonight?
270
271 =item Brain
272
273 The same thing we do every night, Pinky. Try to take over the world!
274
275 =back
276
277 EOPOD
278
279 is($results, <<'EOHTML', "list with text headings");
280 <dl>
281
282 <dt>Pinky</dt>
283 <dd>
284
285 <p>Gee, Brain, what do you want to do tonight?</p>
286
287 </dd>
288 <dt>Brain</dt>
289 <dd>
290
291 <p>The same thing we do every night, Pinky. Try to take over the world!</p>
292
293 </dd>
294 </dl>
295
296 EOHTML
297
298 initialize($parser, $results);
299 $parser->parse_string_document(<<'EOPOD');
300 =over
301
302 =item * Pinky
303
304 Gee, Brain, what do you want to do tonight?
305
306 =item * Brain
307
308 The same thing we do every night, Pinky. Try to take over the world!
309
310 =back
311
312 EOPOD
313
314 is($results, <<'EOHTML', "list with bullet and text headings");
315 <ul>
316
317 <li><p>Pinky</p>
318
319 <p>Gee, Brain, what do you want to do tonight?</p>
320
321 </li>
322 <li><p>Brain</p>
323
324 <p>The same thing we do every night, Pinky. Try to take over the world!</p>
325
326 </li>
327 </ul>
328
329 EOHTML
330
331 initialize($parser, $results);
332 $parser->parse_string_document(<<'EOPOD');
333 =over
334
335 =item * Brain <brain@binkyandthebrain.com>
336
337 =item * Pinky <pinky@binkyandthebrain.com>
338
339 =back
340
341 EOPOD
342
343 is($results, <<'EOHTML', "bulleted author list");
344 <ul>
345
346 <li><p>Brain &lt;brain@binkyandthebrain.com&gt;</p>
347
348 </li>
349 <li><p>Pinky &lt;pinky@binkyandthebrain.com&gt;</p>
350
351 </li>
352 </ul>
353
354 EOHTML
355
356 initialize($parser, $results);
357 $parser->parse_string_document(<<'EOPOD');
358 =over
359
360 =item Pinky
361
362 =over
363
364 =item World Domination
365
366 =back
367
368 =item Brain
369
370 =back
371
372 EOPOD
373
374 is($results, <<'EOHTML', 'nested lists');
375 <dl>
376
377 <dt>Pinky</dt>
378 <dd>
379
380 <dl>
381
382 <dt>World Domination</dt>
383 <dd>
384
385 </dd>
386 </dl>
387
388 </dd>
389 <dt>Brain</dt>
390 <dd>
391
392 </dd>
393 </dl>
394
395 EOHTML
396
397 initialize($parser, $results);
398 $parser->parse_string_document(<<'EOPOD');
399 =over
400
401 =item Pinky
402
403 On the list:
404
405 =over
406
407 =item World Domination
408
409 Fight the good fight
410
411 =item Go to Europe
412
413 (Steve Martin joke)
414
415 =back
416
417 =item Brain
418
419 Not so much
420
421 =back
422
423 EOPOD
424
425 is($results, <<'EOHTML', 'multiparagraph nested lists');
426 <dl>
427
428 <dt>Pinky</dt>
429 <dd>
430
431 <p>On the list:</p>
432
433 <dl>
434
435 <dt>World Domination</dt>
436 <dd>
437
438 <p>Fight the good fight</p>
439
440 </dd>
441 <dt>Go to Europe</dt>
442 <dd>
443
444 <p>(Steve Martin joke)</p>
445
446 </dd>
447 </dl>
448
449 </dd>
450 <dt>Brain</dt>
451 <dd>
452
453 <p>Not so much</p>
454
455 </dd>
456 </dl>
457
458 EOHTML
459
460 initialize($parser, $results);
461 $parser->parse_string_document(<<'EOPOD');
462 =pod
463
464   1 + 1 = 2;
465   2 + 2 = 4;
466
467 EOPOD
468
469 is($results, <<'EOHTML', "code block");
470 <pre><code>  1 + 1 = 2;
471   2 + 2 = 4;</code></pre>
472
473 EOHTML
474
475
476 initialize($parser, $results);
477 $parser->parse_string_document(<<'EOPOD');
478 =pod
479
480 A plain paragraph with a C<functionname>.
481
482 C<< This code is B<important> to E<lt>me>! >>
483
484 EOPOD
485 is($results, <<"EOHTML", "code entity in a paragraph");
486 <p>A plain paragraph with a <code>functionname</code>.</p>
487
488 <p><code>This code is <b>important</b> to &lt;me&gt;!</code></p>
489
490 EOHTML
491
492
493 initialize($parser, $results);
494 $parser->html_header("<html>\n<body>");
495 $parser->html_footer("</body>\n</html>");
496 $parser->parse_string_document(<<'EOPOD');
497 =pod
498
499 A plain paragraph with body tags turned on.
500 EOPOD
501 is($results, <<"EOHTML", "adding html body tags");
502 <html>
503 <body>
504
505 <p>A plain paragraph with body tags turned on.</p>
506
507 </body>
508 </html>
509
510 EOHTML
511
512
513 initialize($parser, $results);
514 $parser->html_css('style.css');
515 $parser->html_header(undef);
516 $parser->html_footer(undef);
517 $parser->parse_string_document(<<'EOPOD');
518 =pod
519
520 A plain paragraph with body tags and css tags turned on.
521 EOPOD
522 like($results, qr/<link rel="stylesheet" href="style.css" type="text\/css" \/>/,
523 "adding html body tags and css tags");
524
525
526 initialize($parser, $results);
527 $parser->parse_string_document(<<'EOPOD');
528 =pod
529
530 A plain paragraph with S<non breaking text>.
531 EOPOD
532 is($results, <<"EOHTML", "Non breaking text in a paragraph");
533 <p>A plain paragraph with <span style="white-space: nowrap;">non breaking text</span>.</p>
534
535 EOHTML
536
537 initialize($parser, $results);
538 $parser->parse_string_document(<<'EOPOD');
539 =pod
540
541 A plain paragraph with a L<Newlines>.
542 EOPOD
543 is($results, <<"EOHTML", "Link entity in a paragraph");
544 <p>A plain paragraph with a <a href="$PERLDOC?Newlines">Newlines</a>.</p>
545
546 EOHTML
547
548 initialize($parser, $results);
549 $parser->parse_string_document(<<'EOPOD');
550 =pod
551
552 A plain paragraph with a L<perlport/Newlines>.
553 EOPOD
554 is($results, <<"EOHTML", "Link entity in a paragraph");
555 <p>A plain paragraph with a <a href="$PERLDOC?perlport#Newlines">&quot;Newlines&quot; in perlport</a>.</p>
556
557 EOHTML
558
559 initialize($parser, $results);
560 $parser->parse_string_document(<<'EOPOD');
561 =pod
562
563 A plain paragraph with a L<Boo|http://link.included.here>.
564 EOPOD
565 is($results, <<"EOHTML", "A link in a paragraph");
566 <p>A plain paragraph with a <a href="http://link.included.here">Boo</a>.</p>
567
568 EOHTML
569
570 initialize($parser, $results);
571 $parser->parse_string_document(<<'EOPOD');
572 =pod
573
574 A plain paragraph with a L<http://link.included.here>.
575 EOPOD
576 is($results, <<"EOHTML", "A link in a paragraph");
577 <p>A plain paragraph with a <a href="http://link.included.here">http://link.included.here</a>.</p>
578
579 EOHTML
580
581 initialize($parser, $results);
582 $parser->parse_string_document(<<'EOPOD');
583 =pod
584
585 A plain paragraph with a L<http://link.included.here?o=1&p=2>.
586 EOPOD
587 is($results, <<"EOHTML", "A link in a paragraph");
588 <p>A plain paragraph with a <a href="http://link.included.here?o=1&amp;p=2">http://link.included.here?o=1&amp;p=2</a>.</p>
589
590 EOHTML
591
592 initialize($parser, $results);
593 $parser->parse_string_document(<<'EOPOD');
594 =pod
595
596 A plain paragraph with B<bold text>.
597 EOPOD
598 is($results, <<"EOHTML", "Bold text in a paragraph");
599 <p>A plain paragraph with <b>bold text</b>.</p>
600
601 EOHTML
602
603 initialize($parser, $results);
604 $parser->parse_string_document(<<'EOPOD');
605 =pod
606
607 A plain paragraph with I<italic text>.
608 EOPOD
609 is($results, <<"EOHTML", "Italic text in a paragraph");
610 <p>A plain paragraph with <i>italic text</i>.</p>
611
612 EOHTML
613
614 initialize($parser, $results);
615 $parser->parse_string_document(<<'EOPOD');
616 =pod
617
618 A plain paragraph with a F<filename>.
619 EOPOD
620 is($results, <<"EOHTML", "File name in a paragraph");
621 <p>A plain paragraph with a <i>filename</i>.</p>
622
623 EOHTML
624
625 # It's not important that 's (apostrophes) be encoded for XHTML output.
626 initialize($parser, $results);
627 $parser->parse_string_document(<<'EOPOD');
628 =pod
629
630   # this header is very important & dont you forget it
631   my $text = "File is: " . <FILE>;
632 EOPOD
633 is($results, <<"EOHTML", "Verbatim text with encodable entities");
634 <pre><code>  # this header is very important &amp; dont you forget it
635   my \$text = &quot;File is: &quot; . &lt;FILE&gt;;</code></pre>
636
637 EOHTML
638
639 initialize($parser, $results);
640 $parser->parse_string_document(<<'EOPOD');
641 =pod
642
643 A text paragraph using E<sol> and E<verbar> special POD entities.
644
645 EOPOD
646 is($results, <<"EOHTML", "Text with decodable entities");
647 <p>A text paragraph using / and | special POD entities.</p>
648
649 EOHTML
650
651 initialize($parser, $results);
652 $parser->parse_string_document(<<'EOPOD');
653 =pod
654
655 A text paragraph using numeric POD entities: E<60>, E<62>.
656
657 EOPOD
658 is($results, <<"EOHTML", "Text with numeric entities");
659 <p>A text paragraph using numeric POD entities: &lt;, &gt;.</p>
660
661 EOHTML
662
663 my $html = q{<tt>
664 <pre>
665 #include &lt;stdio.h&gt;
666
667 int main(int argc,char *argv[]) {
668
669         printf("Hellow World\n");
670         return 0;
671
672 }
673 </pre>
674 </tt>};
675 initialize($parser, $results);
676 $parser->parse_string_document("=begin html\n\n$html\n\n=end html\n");
677 is($results, "$html\n\n", "Text with =begin html");
678
679 SKIP: for my $use_html_entities (0, 1) {
680   if ($use_html_entities and not $Pod::Simple::XHTML::HAS_HTML_ENTITIES) {
681     skip("HTML::Entities not installed", 3);
682   }
683   local $Pod::Simple::XHTML::HAS_HTML_ENTITIES = $use_html_entities;
684   initialize($parser, $results);
685   $parser->codes_in_verbatim(1);
686   $parser->parse_string_document(<<'EOPOD');
687 =pod
688
689   # this header is very important & dont you forget it
690   B<my $file = <FILEE<gt> || Blank!;>
691   my $text = "File is: " . <FILE>;
692 EOPOD
693 is($results, <<"EOHTML", "Verbatim text with markup and embedded formatting");
694 <pre><code>  # this header is very important &amp; dont you forget it
695   <b>my \$file = &lt;FILE&gt; || Blank!;</b>
696   my \$text = &quot;File is: &quot; . &lt;FILE&gt;;</code></pre>
697
698 EOHTML
699
700   # Specify characters to encode.
701   initialize($parser, $results);
702   $parser->html_encode_chars('><"&T');
703   $parser->parse_string_document(<<'EOPOD');
704 =pod
705
706 This is Anna's "Answer" to the <q>Question</q>.
707
708 =cut
709
710 EOPOD
711 my $T = $use_html_entities ? 84 : 'x54';
712 is($results, <<"EOHTML", 'HTML Entities should be only for specified characters');
713 <p>&#$T;his is Anna's &quot;Answer&quot; to the &lt;q&gt;Question&lt;/q&gt;.</p>
714
715 EOHTML
716
717   # Keep =encoding out of content.
718   initialize($parser, $results);
719   $parser->parse_string_document("=encoding ascii\n\n=head1 NAME\n");
720   is($results, <<"EOHTML", 'Encoding should not be in content')
721 <h1 id="NAME">NAME</h1>
722
723 EOHTML
724
725 }
726
727
728 ok $parser = Pod::Simple::XHTML->new, 'Construct a new parser';
729 $results = '';
730 $parser->output_string( \$results ); # Send the resulting output to a string
731 ok $parser->parse_string_document( "=head1 Poit!" ), 'Parse with headers';
732 like $results, qr{\Q<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />},
733     'Should have proper http-equiv meta tag';
734
735 ok $parser = Pod::Simple::XHTML->new, 'Construct a new parser again';
736 ok $parser->html_charset('UTF-8'), 'Set the html charset to UTF-8';
737 $results = '';
738 $parser->output_string( \$results ); # Send the resulting output to a string
739 ok $parser->parse_string_document( "=head1 Poit!" ), 'Parse with headers';
740 like $results, qr{\Q<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />},
741     'Should have http-equiv meta tag with UTF-8';
742
743 # Test the link generation methods.
744 is $parser->resolve_pod_page_link('Net::Ping', 'INSTALL'),
745     "$PERLDOC?Net::Ping#INSTALL",
746     'POD link with fragment';
747 is $parser->resolve_pod_page_link('perlpodspec'),
748     "$PERLDOC?perlpodspec", 'Simple POD link';
749 is $parser->resolve_pod_page_link(undef, 'SYNOPSIS'), '#SYNOPSIS',
750     'Simple fragment link';
751 is $parser->resolve_pod_page_link(undef, 'this that'), '#this-that',
752     'Fragment link with space';
753 is $parser->resolve_pod_page_link('perlpod', 'this that'),
754     "$PERLDOC?perlpod#this-that",
755     'POD link with fragment with space';
756
757 is $parser->resolve_man_page_link('crontab(5)', 'EXAMPLE CRON FILE'),
758     "${MANURL}5/crontab", 'Man link with fragment';
759 is $parser->resolve_man_page_link('crontab(5)'),
760     "${MANURL}5/crontab", 'Man link without fragment';
761 is $parser->resolve_man_page_link('crontab'),
762     "${MANURL}1/crontab", 'Man link without section';
763
764 # Make sure that batch_mode_page_object_init() works.
765 ok $parser->batch_mode_page_object_init(0, 0, 0, 0, 6),
766     'Call batch_mode_page_object_init()';
767 ok $parser->batch_mode, 'We should be in batch mode';
768 is $parser->batch_mode_current_level, 6,
769     'The level should have been set';
770
771 ######################################
772
773 sub initialize {
774         $_[0] = Pod::Simple::XHTML->new ();
775         $_[0]->html_header("");
776         $_[0]->html_footer("");
777         $_[0]->output_string( \$results ); # Send the resulting output to a string
778         $_[1] = '';
779         return;
780 }