1 #--- format.t ----------------------------------------------------------------- 2 # function: Test ToC formatting. 3 4 use strict; 5 use Test; 6 7 BEGIN { plan tests => 6; } 8 9 use HTML::Toc; 10 use HTML::TocGenerator; 11 use HTML::TocInsertor; 12 13 my ($output, $content, $filename); 14 my $toc = HTML::Toc->new; 15 my $tocGenerator = HTML::TocGenerator->new; 16 my $tocInsertor = HTML::TocInsertor->new; 17 18 $toc->setOptions({ 19 'doLinkToToken' => 0, 20 'levelIndent' => 0, 21 'insertionPoint' => 'before <h1>', 22 'header' => '', 23 'footer' => '', 24 }); 25 26 27 BEGIN { 28 # Create test file 29 $filename = "file$$.htm"; 30 die "$filename is already there" if -e $filename; 31 open(FILE, ">$filename") || die "Can't create $filename: $!"; 32 print FILE <<'EOT'; close(FILE); 33 <h1>Header</h1> 34 EOT 35 } 36 37 38 END { 39 # Remove test file 40 unlink($filename) or warn "Can't unlink $filename: $!"; 41 } 42 43 44 #--- 1. templateLevelBegin ---------------------------------------------------- 45 46 $toc->setOptions({ 47 'templateLevelBegin' => '"<ul class=toc_$groupId$level>\n"' 48 }); 49 $tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 50 ok($output, "<ul class=toc_h1>\n<li>Header\n</ul><h1>Header</h1>"); 51 $toc->setOptions({'templateLevelBegin' => undef}); 52 53 54 #--- 2. levelToToc ----------------------------------------------------------- 55 56 $tocGenerator->generate($toc, "<h1>Header1</h1>\n<h2>Header2</h2>"); 57 $toc->setOptions({'levelToToc' => '1'}); 58 ok($toc->format(), "<ul>\n<li>Header1\n</ul>"); 59 $toc->setOptions({'levelToToc' => '.*'}); 60 61 62 #--- 3. groupToToc ----------------------------------------------------------- 63 64 $toc->setOptions({ 65 'tokenToToc' => [{ 66 'level' => 1, 67 'tokenBegin' => '<h1 class=-foo>' 68 }, { 69 'groupId' => 'foo', 70 'level' => 1, 71 'tokenBegin' => '<h1 class=foo>' 72 }] 73 }); 74 $tocGenerator->generate($toc, "<h1>Header1</h1>\n<h1 class=foo>Foo</h1>"); 75 $toc->setOptions({'groupToToc' => 'foo'}); 76 ok($toc->format(), "<ul>\n<li>Foo\n</ul>"); 77 $toc->setOptions({'groupToToc' => '.*'}); 78 79 80 #--- 4. header & footer ------------------------------------------------------- 81 82 $toc->setOptions({ 83 'tokenToToc' => [{ 84 'level' => 1, 85 'tokenBegin' => '<h1>' 86 }], 87 'header' => '<!-- TocHeader -->', 88 'footer' => '<!-- TocFooter -->', 89 }); 90 $tocInsertor->insert($toc, "<h1>Header1</h1>", {'output' => \$output}); 91 ok("$output\n", <<EOT); 92 <!-- TocHeader --><ul> 93 <li>Header1 94 </ul><!-- TocFooter --><h1>Header1</h1> 95 EOT 96 97 98 # Test 'doSingleStepLevel' => 1 99 TestSingleStepLevel1(); 100 # Test 'doSingleStepLevel' => 0 101 TestSingleStepLevel0(); 102 103 104 #--- 5. TestSingleStepLevel1 -------------------------------------------------- 105 106 sub TestSingleStepLevel1 { 107 my $toc = new HTML::Toc; 108 my $tocGenerator = new HTML::TocGenerator; 109 110 # Generate ToC 111 $tocGenerator->generate($toc, <<EOT); 112 <h1>Header 1</h1> 113 <h3>Header 3</h3> 114 EOT 115 # Compare output 116 ok($toc->format(), <<EOT); 117 118 <!-- Table of Contents generated by Perl - HTML::Toc --> 119 <ul> 120 <li><a href=#h-1>Header 1</a> 121 <ul> 122 <ul> 123 <li><a href=#h-1.0.1>Header 3</a> 124 </ul> 125 </ul> 126 </ul> 127 <!-- End of generated Table of Contents --> 128 EOT 129 } # TestSingleStepLevel1() 130 131 132 #--- 6. TestSingleStepLevel0 -------------------------------------------------- 133 134 sub TestSingleStepLevel0 { 135 my $toc = new HTML::Toc; 136 my $tocGenerator = new HTML::TocGenerator; 137 138 # Set ToC options 139 $toc->setOptions({'doSingleStepLevel' => 0}); 140 # Generate ToC 141 $tocGenerator->generate($toc, <<EOT); 142 <h1>Header 1</h1> 143 <h3>Header 3</h3> 144 EOT 145 # Compare output 146 ok($toc->format(), <<EOT); 147 148 <!-- Table of Contents generated by Perl - HTML::Toc --> 149 <ul> 150 <li><a href=#h-1>Header 1</a> 151 <ul> 152 <li><a href=#h-1.0.1>Header 3</a> 153 </ul> 154 </ul> 155 <!-- End of generated Table of Contents --> 156 EOT 157 } # TestSingleStepLevel0() 158