1 #--- podExamples.t ------------------------------------------------------------
2 # function: Test HTML::ToC. In particular test the examples as described in
3 # the POD documentation.
8 BEGIN { plan tests => 13; }
11 use HTML::TocGenerator;
12 use HTML::TocInsertor;
15 my ($filename, $filename2);
20 $filename = "tmp.htm";
21 die "$filename is already there" if -e $filename;
23 $filename2 = "tmp2.htm";
24 die "$filename2 is already there" if -e $filename2;
30 unlink($filename) or warn "Can't unlink $filename: $!";
32 unlink($filename2) or warn "Can't unlink $filename2: $!";
36 #--- TestExtendFromFile() --------------------------------------------------
37 # function: Test 'HTML::TocGenerator->extendFromFile()
39 sub TestExtendFromFile {
41 open(FILE, ">$filename") || die "Can't create $filename: $!";
42 print FILE <<'EOT'; close(FILE);
44 <h1>Chapter of document 1</h1>
48 # Assemble test file 2
49 open(FILE, ">$filename2") || die "Can't create $filename2: $!";
50 print FILE <<'EOT'; close(FILE);
53 <h1>Chapter of document 2</h1>
59 my $toc = HTML::Toc->new();
60 my $tocGenerator = HTML::TocGenerator->new();
63 $toc->setOptions({'doLinkToFile' => 1});
65 $tocGenerator->generateFromFile($toc, $filename);
66 $tocGenerator->extendFromFile($toc, $filename2);
68 ok($toc->format(), <<EOT);
70 <!-- Table of Contents generated by Perl - HTML::Toc -->
72 <li><a href=tmp.htm#h-1>Chapter of document 1</a>
73 <li><a href=tmp2.htm#h-2>Chapter of document 2</a>
75 <!-- End of generated Table of Contents -->
77 } # TestExtendFromFile()
80 #--- TestGenerateFromFiles() --------------------------------------------------
81 # function: Test 'HTML::TocGenerator->generateFromFile()
83 sub TestGenerateFromFiles {
85 open(FILE, ">$filename") || die "Can't create $filename: $!";
86 print FILE <<'EOT'; close(FILE);
88 <h1>Chapter of document 1</h1>
92 # Assemble test file 2
93 open(FILE, ">$filename2") || die "Can't create $filename2: $!";
94 print FILE <<'EOT'; close(FILE);
97 <h1>Chapter of document 2</h1>
103 my $toc = HTML::Toc->new();
104 my $tocGenerator = HTML::TocGenerator->new();
107 $toc->setOptions({'doLinkToFile' => 1});
109 $tocGenerator->generateFromFile($toc, [$filename, $filename2]);
111 ok($toc->format(), <<EOT);
113 <!-- Table of Contents generated by Perl - HTML::Toc -->
115 <li><a href=tmp.htm#h-1>Chapter of document 1</a>
116 <li><a href=tmp2.htm#h-2>Chapter of document 2</a>
118 <!-- End of generated Table of Contents -->
120 } # TestGenerateFromFiles()
123 #--- TestGenerateFromFiles() --------------------------------------------------
124 # function: Test 'HTML::TocGenerator->generateFromFile() using multiple files.
126 sub TestGenerateFromFile {
127 # Assemble test file 1
128 open(FILE, ">$filename") || die "Can't create $filename: $!";
129 print FILE <<'EOT'; close(FILE);
138 my $toc = HTML::Toc->new();
139 my $tocGenerator = HTML::TocGenerator->new();
142 $tocGenerator->generateFromFile($toc, $filename);
144 ok($toc->format(), <<EOT);
146 <!-- Table of Contents generated by Perl - HTML::Toc -->
148 <li><a href=#h-1>Chapter</a>
150 <!-- End of generated Table of Contents -->
152 } # TestGenerateFromFile()
155 #--- TestInsertIntoFile() -----------------------------------------------------
156 # function: Test 'HTML::TocInsertor->insertIntoFile()
158 sub TestInsertIntoFile {
160 open(FILE, ">$filename") || die "Can't create $filename: $!";
161 print FILE <<'EOT'; close(FILE);
170 my $toc = HTML::Toc->new();
171 my $tocInsertor = HTML::TocInsertor->new();
175 $tocInsertor->insertIntoFile($toc, $filename, {'output' => \$output});
180 <!-- Table of Contents generated by Perl - HTML::Toc -->
182 <li><a href=#h-1>Chapter</a>
184 <!-- End of generated Table of Contents -->
186 <a name=h-1><h1>Chapter</h1></a>
190 } # TestInsertIntoFile()
193 #--- TestInsertIntoFileUsingTocUpdator() --------------------------------------
194 # function: Test 'HTML::TocUpdator->insertIntoFile()
196 sub TestInsertIntoFileUsingTocUpdator {
198 open(FILE, ">$filename") || die "Can't create $filename: $!";
199 print FILE <<'EOT'; close(FILE);
210 my $toc = HTML::Toc->new();
211 my $tocUpdator = HTML::TocUpdator->new();
215 $tocUpdator->insertIntoFile($toc, $filename, {'output' => \$output});
219 <body><!-- #BeginToc -->
220 <!-- Table of Contents generated by Perl - HTML::Toc -->
222 <li><a href=#h-1> Chapter </a>
224 <!-- End of generated Table of Contents -->
226 <!-- #BeginTocAnchorNameBegin --><a name=h-1><!-- #EndTocAnchorNameBegin --><h1>
228 </h1><!-- #BeginTocAnchorNameEnd --></a><!-- #EndTocAnchorNameEnd -->
232 } # TestInsertIntoFileUsingTocUpdator()
235 #--- TestGlobalGroups0() ------------------------------------------------------
236 # function: Test 'HTML::TocGenerator' option 'doUseGroupsGlobal = 0'.
238 sub TestGlobalGroups0 {
240 open(FILE, ">$filename") || die "Can't create $filename: $!";
241 print FILE <<'EOT'; close(FILE);
247 my $toc1 = HTML::Toc->new();
248 my $toc2 = HTML::Toc->new();
249 my $tocGenerator = HTML::TocGenerator->new();
255 'tokenToToc' => [{'tokenBegin' => '<h1>'}]
260 'tokenToToc' => [{'tokenBegin' => '<h2>'}]
263 $tocGenerator->generateFromFile([$toc1, $toc2], $filename);
265 ok($toc1->format() . $toc2->format() . "\n", <<'EOT');
267 <li><a href=#h-1>Chapter</a>
269 <li><a href=#h-1>Paragraph</a>
272 } # TestGlobalGroups0()
275 #--- TestGlobalGroups1() ------------------------------------------------------
276 # function: Test 'HTML::TocGenerator' option 'doUseGroupsGlobal = 0'.
278 sub TestGlobalGroups1 {
280 open(FILE, ">$filename") || die "Can't create $filename: $!";
288 my $toc1 = HTML::Toc->new();
289 my $toc2 = HTML::Toc->new();
290 my $tocGenerator = HTML::TocGenerator->new();
296 'tokenToToc' => [{'tokenBegin' => '<h1>'}]
301 'tokenToToc' => [{'tokenBegin' => '<h2>'}]
304 $tocGenerator->generateFromFile(
305 [$toc1, $toc2], $filename, {'doUseGroupsGlobal' => 1}
308 ok($toc1->format() . $toc2->format() . "\n", <<'EOT');
310 <li><a href=#h-1>Chapter</a>
312 <li><a href=#h-2>Paragraph</a>
315 } # TestGlobalGroups1()
318 #--- TestMultipleGroupsAppendix() ---------------------------------------------
319 # function: Test multiple ToCs
321 sub TestMultipleGroupsAppendix() {
323 my $toc = HTML::Toc->new();
324 my $tocInsertor = HTML::TocInsertor->new();
330 'tokenBegin' => '<h1 class=-appendix>'
332 'tokenBegin' => '<h2 class=-appendix>',
335 'groupId' => 'appendix',
336 'tokenBegin' => '<h1 class=appendix>',
338 'groupId' => 'appendix',
339 'tokenBegin' => '<h2 class=appendix>',
344 $tocInsertor->insert($toc, <<EOT, {'output' => \$output});
348 <h3>Subparagraph</h3>
350 <h1 class=appendix>Appendix Chapter</h1>
351 <h2 class=appendix>Appendix Paragraph</h2>
357 <!-- Table of Contents generated by Perl - HTML::Toc -->
359 <li><a href=#h-1>Chapter</a>
361 <li><a href=#h-1.1>Paragraph</a>
363 <li><a href=#h-2>Chapter</a>
366 <li><a href=#appendix-1>Appendix Chapter</a>
368 <li><a href=#appendix-1.1>Appendix Paragraph</a>
371 <!-- End of generated Table of Contents -->
373 <a name=h-1><h1>Chapter</h1></a>
374 <a name=h-1.1><h2>Paragraph</h2></a>
375 <h3>Subparagraph</h3>
376 <a name=h-2><h1>Chapter</h1></a>
377 <a name=appendix-1><h1 class=appendix>Appendix Chapter</h1></a>
378 <a name=appendix-1.1><h2 class=appendix>Appendix Paragraph</h2></a>
381 } # TestMultipleGroupsAppendix()
384 #--- TestMultipleGroupsPart() -------------------------------------------------
385 # function: Test multiple ToCs
387 sub TestMultipleGroupsPart() {
389 my $toc = HTML::Toc->new();
390 my $tocInsertor = HTML::TocInsertor->new();
396 'tokenBegin' => '<h1 class=-part>'
398 'tokenBegin' => '<h2 class=-part>',
402 'tokenBegin' => '<h1 class=part>',
404 'doNumberToken' => 1,
405 'numberingStyle' => 'upper-alpha'
409 $tocInsertor->insert($toc, <<EOT, {'output' => \$output});
411 <h1 class=part>First Part</h1>
414 <h1 class=part>Second Part</h1>
422 <!-- Table of Contents generated by Perl - HTML::Toc -->
424 <li><a href=#part-A>First Part</a>
427 <li><a href=#h-1>Chapter</a>
429 <li><a href=#h-1.1>Paragraph</a>
433 <li><a href=#part-B>Second Part</a>
436 <li><a href=#h-2>Chapter</a>
438 <li><a href=#h-2.1>Paragraph</a>
441 <!-- End of generated Table of Contents -->
443 <a name=part-A><h1 class=part>A First Part</h1></a>
444 <a name=h-1><h1>Chapter</h1></a>
445 <a name=h-1.1><h2>Paragraph</h2></a>
446 <a name=part-B><h1 class=part>B Second Part</h1></a>
447 <a name=h-2><h1>Chapter</h1></a>
448 <a name=h-2.1><h2>Paragraph</h2></a>
451 } # TestMultipleGroupsPart()
454 #--- TestMultipleTocs() -------------------------------------------------------
455 # function: Test multiple ToCs
457 sub TestMultipleTocs() {
459 open(FILE, ">$filename") || die "Can't create $filename: $!";
460 print FILE <<'EOT'; close(FILE);
463 <img src=test1.gif alt="First picture">
464 <h2>Paragraph One</h2>
465 <img src=test2.gif alt="Second picture">
470 my $toc1 = HTML::Toc->new();
471 my $toc2 = HTML::Toc->new();
472 my $tocInsertor = HTML::TocInsertor->new();
478 'groupId' => 'image',
479 'tokenBegin' => '<img alt=@>'
483 $tocInsertor->insertIntoFile(
484 [$toc1, $toc2], $filename, {'output' => \$output}
489 <!-- Table of Contents generated by Perl - HTML::Toc -->
491 <li><a href=#h-1>Header One</a>
493 <li><a href=#h-1.1>Paragraph One</a>
496 <!-- End of generated Table of Contents -->
498 <!-- Table of Contents generated by Perl - HTML::Toc -->
500 <li><a href=#image-1>First picture</a>
501 <li><a href=#image-2>Second picture</a>
503 <!-- End of generated Table of Contents -->
505 <a name=h-1><h1>Header One</h1></a>
506 <a name=image-1><img src=test1.gif alt="First picture"></a>
507 <a name=h-1.1><h2>Paragraph One</h2></a>
508 <a name=image-2><img src=test2.gif alt="Second picture"></a>
511 } # TestMultipleTocs()
514 #--- TestSpecifyNumberedList() ------------------------------------------------
515 # function: Test specifying numbered list.
517 sub TestSpecifyNumberedList {
519 open(FILE, ">$filename") || die "Can't create $filename: $!";
520 print FILE <<'EOT'; close(FILE);
528 my $toc = HTML::Toc->new();
529 my $tocGenerator = HTML::TocGenerator->new();
533 'templateLevelBegin' => '"<ol>\n"',
534 'templateLevelEnd' => '"</ol>\n"',
537 $tocGenerator->generateFromFile($toc, $filename);
539 ok($toc->format(), <<EOT);
541 <!-- Table of Contents generated by Perl - HTML::Toc -->
543 <li><a href=#h-1>Chapter</a>
545 <li><a href=#h-1.1>Paragraph</a>
548 <!-- End of generated Table of Contents -->
550 } # TestSpecifyNumberedList()
553 #--- TestUpdateFile() ---------------------------------------------------------
554 # function: Test 'HTML::TocUpdator->updateFile()'
558 open(FILE, ">$filename") || die "Can't create $filename: $!";
559 print FILE <<'EOT'; close(FILE);
561 <body><!-- #BeginToc -->
564 <!-- #BeginTocAnchorNameBegin -->bar<!-- #EndTocAnchorNameBegin --><h1>
566 </h1><!-- #BeginTocAnchorNameEnd -->foo<!-- #EndTocAnchorNameEnd -->
572 my $toc = HTML::Toc->new();
573 my $tocUpdator = HTML::TocUpdator->new();
577 $tocUpdator->updateFile($toc, $filename, {'output' => \$output});
581 <body><!-- #BeginToc -->
582 <!-- Table of Contents generated by Perl - HTML::Toc -->
584 <li><a href=#h-1> Chapter </a>
586 <!-- End of generated Table of Contents -->
588 <!-- #BeginTocAnchorNameBegin --><a name=h-1><!-- #EndTocAnchorNameBegin --><h1>
590 </h1><!-- #BeginTocAnchorNameEnd --></a><!-- #EndTocAnchorNameEnd -->
597 #--- TestUsingCSS() -----------------------------------------------------------
598 # function: Test multiple ToCs
603 my $toc = new HTML::Toc;
604 my $tocInsertor = new HTML::TocInsertor;
608 'templateLevelBegin' => '"<ol class=toc_$groupId$level>\n"',
609 'templateLevelEnd' => '"</ol>\n"',
610 'doNumberToken' => 1,
612 'groupId' => 'appendix',
613 'tokenBegin' => '<h1>',
614 'numberingStyle' => 'upper-alpha'
616 'groupId' => 'appendix',
617 'tokenBegin' => '<h2>',
621 $tocInsertor->insert($toc, <<EOT);
624 <style type="text/css">
625 ol.toc_appendix1 { list-style-type: upper-alpha }
630 <h2>Appendix Paragraph</h2>
632 <h2>Appendix Paragraph</h2>
637 $tocInsertor->insert($toc, <<EOT, {'output' => \$output});
640 <style type="text/css">
641 ol.toc_appendix1 { list-style-type: upper-alpha }
646 <h2>Appendix Paragraph</h2>
648 <h2>Appendix Paragraph</h2>
656 <style type="text/css">
657 ol.toc_appendix1 { list-style-type: upper-alpha }
661 <!-- Table of Contents generated by Perl - HTML::Toc -->
662 <ol class=toc_appendix1>
663 <li><a href=#appendix-A>Appendix</a>
664 <ol class=toc_appendix2>
665 <li><a href=#appendix-A.1>Appendix Paragraph</a>
667 <li><a href=#appendix-B>Appendix</a>
668 <ol class=toc_appendix2>
669 <li><a href=#appendix-B.1>Appendix Paragraph</a>
672 <!-- End of generated Table of Contents -->
674 <a name=appendix-A><h1>A Appendix</h1></a>
675 <a name=appendix-A.1><h2>A.1 Appendix Paragraph</h2></a>
676 <a name=appendix-B><h1>B Appendix</h1></a>
677 <a name=appendix-B.1><h2>B.1 Appendix Paragraph</h2></a>
684 # Test 'extendFromFile()'
685 TestExtendFromFile();
686 # Test 'generateFromFile()'
687 TestGenerateFromFile();
688 # Test 'generateFromFiles()'
689 TestGenerateFromFiles();
690 # Test 'doUseGroupsGlobal = 0'
692 # Test 'doUseGroupsGlobal = 1'
694 # Test 'tocInsertor->insertIntoFile'
695 TestInsertIntoFile();
696 # Test 'tocUpdator->insertIntoFile'
697 TestInsertIntoFileUsingTocUpdator();
698 # Test additional 'appendix' group
699 TestMultipleGroupsAppendix();
700 # Test additional 'part' group
701 TestMultipleGroupsPart();
704 # Test specifying numbered list
705 TestSpecifyNumberedList();
706 # Test 'updateFile()'