1 #--- options.t ---------------------------------------------------------------- 2 # function: Test HTML::ToC. In particular test the available options. 3 4 use strict; 5 use Test; 6 7 BEGIN { plan tests => 5; } 8 9 use HTML::Toc; 10 use HTML::TocGenerator; 11 use HTML::TocInsertor; 12 use HTML::TocUpdator; 13 14 my ($filename); 15 16 BEGIN { 17 # Create test file 18 $filename = "file$$.htm"; 19 die "$filename is already there" if -e $filename; 20 } 21 22 23 END { 24 # Remove test file 25 unlink($filename) or warn "Can't unlink $filename: $!"; 26 } 27 28 29 #--- TestAttributeToExcludeToken() -------------------------------------------- 30 # function: Test 'HTML::Toc' option 'attributeToExcludeToken' 31 32 sub TestAttributeToExcludeToken { 33 # Assemble test file 34 open(FILE, ">$filename") || die "Can't create $filename: $!"; 35 print FILE <<'EOT'; close(FILE); 36 <body> 37 <h1>Chapter 1</h1> 38 <h1 class=appendix>Appendix</h1> 39 </body> 40 EOT 41 42 # Create objects 43 my $toc = HTML::Toc->new(); 44 my $tocGenerator = HTML::TocGenerator->new(); 45 46 $toc->setOptions({ 47 'attributeToExcludeToken' => 'foo', 48 'tokenToToc' => [{ 49 'tokenBegin' => '<h1 class=foodix>' 50 }] 51 }); 52 # Generate ToC 53 $tocGenerator->generateFromFile($toc, $filename); 54 # Test ToC 55 ok($toc->format(), <<EOT); 56 57 <!-- Table of Contents generated by Perl - HTML::Toc --> 58 <ul> 59 <li><a href=#h-1>Chapter 1</a> 60 </ul> 61 <!-- End of generated Table of Contents --> 62 EOT 63 } # TestAttributeToExcludeToken() 64 65 66 #--- TestAttributeToTocToken() ------------------------------------------------ 67 # function: Test 'HTML::Toc' option 'attributeToTocToken' 68 69 sub TestAttributeToTocToken { 70 # Assemble test file 71 open(FILE, ">$filename") || die "Can't create $filename: $!"; 72 print FILE <<'EOT'; close(FILE); 73 <body> 74 <img src=test.gif alt=Picture> 75 </body> 76 </html> 77 EOT 78 79 # Create objects 80 my $toc = HTML::Toc->new(); 81 my $tocGenerator = HTML::TocGenerator->new(); 82 83 $toc->setOptions({ 84 'attributeToTocToken' => 'foo', 85 'tokenToToc' => [{ 86 'groupId' => 'image', 87 'tokenBegin' => '<img alt=foo>' 88 }], 89 }); 90 # Generate ToC 91 $tocGenerator->generateFromFile($toc, $filename); 92 # Test ToC 93 ok($toc->format(), <<EOT); 94 95 <!-- Table of Contents generated by Perl - HTML::Toc --> 96 <ul> 97 <li><a href=#image-1>Picture</a> 98 </ul> 99 <!-- End of generated Table of Contents --> 100 EOT 101 } # TestAttributeToTocToken() 102 103 104 #--- TestNumberingStyleDecimal ------------------------------------------------ 105 # function: Test 'decimal' numbering style. 106 107 sub TestNumberingStyleDecimal { 108 # Local variables 109 my $output; 110 # Create objects 111 my $toc = HTML::Toc->new(); 112 my $tocInsertor = HTML::TocInsertor->new(); 113 114 $toc->setOptions({ 115 'doNumberToken' => 1, 116 'tokenToToc' => [{ 117 'level' => 1, 118 'tokenBegin' => '<h1>', 119 'numberingStyle' => 'decimal' 120 }], 121 }); 122 # Generate ToC 123 $tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 124 # Test ToC 125 ok("$output\n", <<EOT); 126 <a name=h-1><h1>1 Header</h1></a> 127 EOT 128 } # TestNumberingStyleDecimal() 129 130 131 #--- TestNumberingStyleLowerAlpha --------------------------------------------- 132 # function: Test 'lower-alpha' numbering style. 133 134 sub TestNumberingStyleLowerAlpha { 135 # Local variables 136 my $output; 137 # Create objects 138 my $toc = HTML::Toc->new(); 139 my $tocInsertor = HTML::TocInsertor->new(); 140 141 $toc->setOptions({ 142 'doNumberToken' => 1, 143 'tokenToToc' => [{ 144 'level' => 1, 145 'tokenBegin' => '<h1>', 146 'numberingStyle' => 'lower-alpha' 147 }], 148 }); 149 # Generate ToC 150 $tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 151 # Test ToC 152 ok("$output\n", <<EOT); 153 <a name=h-a><h1>a Header</h1></a> 154 EOT 155 } # TestNumberingStyleLowerAlpha() 156 157 158 #--- TestNumberingStyleUpperAlpha --------------------------------------------- 159 # function: Test 'upper-alpha' numbering style. 160 161 sub TestNumberingStyleUpperAlpha { 162 # Local variables 163 my $output; 164 # Create objects 165 my $toc = HTML::Toc->new(); 166 my $tocInsertor = HTML::TocInsertor->new(); 167 168 $toc->setOptions({ 169 'doNumberToken' => 1, 170 'tokenToToc' => [{ 171 'level' => 1, 172 'tokenBegin' => '<h1>', 173 'numberingStyle' => 'upper-alpha' 174 }], 175 }); 176 # Generate ToC 177 $tocInsertor->insert($toc, "<h1>Header</h1>", {'output' => \$output}); 178 # Test ToC 179 ok("$output\n", <<EOT); 180 <a name=h-A><h1>A Header</h1></a> 181 EOT 182 } # TestNumberingStyleUpperAlpha() 183 184 185 # Test 'attributeToTocToken' 186 TestAttributeToTocToken(); 187 # Test 'attributeToExcludeToken' 188 TestAttributeToExcludeToken(); 189 # Test 'numberingStyleDecimal' 190 TestNumberingStyleDecimal(); 191 # Test 'numberingStyleLowerAlpha' 192 TestNumberingStyleLowerAlpha(); 193 # Test 'numberingStyleUpperAlpha' 194 TestNumberingStyleUpperAlpha(); 195