1 #--- generate.t --------------------------------------------------------------- 2 # function: Test ToC generation. 3 4 use strict; 5 use Test; 6 7 BEGIN { plan tests => 13; } 8 9 use HTML::Toc; 10 use HTML::TocGenerator; 11 12 my ($filename); 13 my $toc = HTML::Toc->new; 14 my $tocGenerator = HTML::TocGenerator->new; 15 16 $toc->setOptions({ 17 'doLinkToToken' => 0, 18 'levelIndent' => 0, 19 'header' => '', 20 'footer' => '', 21 }); 22 23 24 BEGIN { 25 # Create test file 26 $filename = "file$$.htm"; 27 die "$filename is already there" if -e $filename; 28 open(FILE, ">$filename") || die "Can't create $filename: $!"; 29 print FILE <<'EOT'; 30 <h1>Header</h1> 31 EOT 32 close(FILE); 33 } 34 35 36 END { 37 # Remove test file 38 unlink($filename) or warn "Can't unlink $filename: $!"; 39 } 40 41 42 #--- 1. generate -------------------------------------------------------------- 43 44 $tocGenerator->generate($toc, "<h1>Header</h1>"); 45 ok($toc->format(), "<ul>\n<li>Header\n</ul>"); 46 47 48 #--- 2. generateFromFile ------------------------------------------------------ 49 50 $tocGenerator->generateFromFile($toc, $filename); 51 ok($toc->format(), "<ul>\n<li>Header\n</ul>"); 52 53 54 #--- 3. generateFromFiles ----------------------------------------------------- 55 56 $tocGenerator->generateFromFile($toc, [$filename, $filename]); 57 ok($toc->format(), "<ul>\n<li>Header\n<li>Header\n</ul>"); 58 59 60 #--- 4. doLinkToToken ----------------------------------------------------- 61 62 $toc->setOptions({'doLinkToToken' => 1}); 63 $tocGenerator->generateFromFile($toc, $filename, {'globalGroups' => 1}); 64 ok($toc->format(), "<ul>\n<li><a href=#h-1>Header</a>\n</ul>"); 65 66 67 #--- 5. doLinkToFile ------------------------------------------------------- 68 69 $toc->setOptions({'doLinkToFile' => 1}); 70 $tocGenerator->generateFromFile($toc, $filename); 71 ok($toc->format(), "<ul>\n<li><a href=$filename#h-1>Header</a>\n</ul>"); 72 73 74 #--- 6. templateAnchorHrefBegin ----------------------------------------------- 75 76 # Set options 77 $toc->setOptions({'templateAnchorHrefBegin' => '"test-$file"'}); 78 # Generate ToC 79 $tocGenerator->generateFromFile($toc, $filename); 80 # Test ToC 81 ok($toc->format(), "<ul>\n<li>test-".$filename."Header</a>\n</ul>"); 82 # Reset options 83 $toc->setOptions({'templateAnchorHrefBegin' => undef}); 84 85 86 #--- 7. templateAnchorHrefBegin function -------------------------------------- 87 88 sub AssembleAnchorHrefBegin { 89 # Get arguments 90 my ($aFile, $aGroupId, $aLevel, $aNode) = @_; 91 # Return value 92 return $aFile . $aGroupId . $aLevel . $aNode; 93 } # AssembleAnchorHrefBegin() 94 95 96 # Set options 97 $toc->setOptions({'templateAnchorHrefBegin' => \&AssembleAnchorHrefBegin}); 98 # Generate ToC 99 $tocGenerator->generateFromFile($toc, $filename); 100 # Test ToC 101 ok($toc->format(), "<ul>\n<li>".$filename."h11Header</a>\n</ul>"); 102 # Reset options 103 $toc->setOptions({'templateAnchorHrefBegin' => undef}); 104 105 106 #--- 8. levelToToc no levels available --------------------------------------- 107 108 $toc->setOptions({'levelToToc' => '2'}); 109 $tocGenerator->generate($toc, "<h1>Header</h1>"); 110 ok($toc->format(), ""); 111 112 113 #--- 9. levelToToc level 1 --------------------------------------------------- 114 115 # Set options 116 $toc->setOptions({ 117 'levelToToc' => '1', 118 'doLinkToToken' => 0, 119 }); 120 $tocGenerator->generate($toc, "<h1>Header1</h1>\n<h2>Header2</h2>"); 121 ok($toc->format(), "<ul>\n<li>Header1\n</ul>"); 122 123 124 #--- 10. levelToToc level 2 -------------------------------------------------- 125 126 # Set options 127 $toc->setOptions({ 128 'levelToToc' => '2', 129 'doLinkToToken' => 0, 130 }); 131 $tocGenerator->generate($toc, "<h1>Header1</h1>\n<h2>Header2</h2>"); 132 ok($toc->format(), "<ul>\n<li>Header2\n</ul>"); 133 # Restore options 134 $toc->setOptions({ 135 'levelToToc' => '.*', 136 }); 137 138 139 #--- 11. tokenToToc empty array ---------------------------------------------- 140 141 # Set options 142 $toc->setOptions({'tokenToToc' => []}); 143 $tocGenerator->generate($toc, "<h1>Header</h1>"); 144 ok($toc->format(), ""); 145 146 147 #--- 12. groups nested -------------------------------------------------------- 148 149 $toc->setOptions({ 150 'doNestGroup' => 1, 151 'tokenToToc' => [ 152 { 153 'level' => 1, 154 'tokenBegin' => '<h1 class=-appendix>' 155 }, { 156 'groupId' => 'appendix', 157 'level' => 1, 158 'tokenBegin' => '<h1 class=appendix>' 159 } 160 ] 161 }); 162 $tocGenerator->generate( 163 $toc, "<h1>Header1</h1>\n<h1 class=appendix>Appendix</h1>" 164 ); 165 ok($toc->format() . "\n", <<'EOT'); 166 <ul> 167 <li>Header1 168 <ul> 169 <li>Appendix 170 </ul> 171 </ul> 172 EOT 173 174 175 #--- 13. groups not nested ---------------------------------------------------- 176 177 $toc->setOptions({ 178 'doNestGroup' => 0, 179 'tokenToToc' => [ 180 { 181 'level' => 1, 182 'tokenBegin' => '<h1 class=-appendix>' 183 }, { 184 'groupId' => 'appendix', 185 'level' => 1, 186 'tokenBegin' => '<h1 class=appendix>' 187 } 188 ] 189 }); 190 $tocGenerator->generate( 191 $toc, "<h1>Header1</h1>\n<h1 class=appendix>Appendix</h1>" 192 ); 193 ok($toc->format() . "\n", <<'EOT'); 194 <ul> 195 <li>Header1 196 </ul> 197 <ul> 198 <li>Appendix 199 </ul> 200 EOT 201