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