1 #--- generate.t --------------------------------------------------------------- 2 # function: Test ToC generation. 3 4 use strict; 5 use Test; 6 7 BEGIN { plan tests => 4; } 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. extend ---------------------------------------------------------------- 43 44 # Generate ToC 45 $tocGenerator->generate($toc, "<h1>Header</h1>"); 46 # Extend ToC 47 $tocGenerator->extend($toc, "<h1>Header</h1>"); 48 # Test ToC 49 ok($toc->format(), "<ul>\n<li>Header\n<li>Header\n</ul>"); 50 51 52 #--- 2. extendFromFile -------------------------------------------------------- 53 54 # Generate ToC 55 $tocGenerator->generateFromFile($toc, $filename); 56 # Extend ToC 57 $tocGenerator->extendFromFile($toc, $filename); 58 # Test ToC 59 ok($toc->format(), "<ul>\n<li>Header\n<li>Header\n</ul>"); 60 61 62 #--- 3. extendFromFiles ------------------------------------------------------- 63 64 # Generate ToC 65 $tocGenerator->generateFromFile($toc, $filename); 66 # Extend ToC 67 $tocGenerator->extendFromFile($toc, [$filename, $filename]); 68 # Test ToC 69 ok($toc->format(), "<ul>\n<li>Header\n<li>Header\n<li>Header\n</ul>"); 70 71 72 #--- 4. linkTocToToken -------------------------------------------------------- 73 74 $toc->setOptions({ 75 'doLinkToToken' => 1, 76 }); 77 # Generate ToC 78 $tocGenerator->generate($toc, "<h1>Header</h1>"); 79 # Extend ToC 80 $tocGenerator->extend($toc, "<h1>Header</h1>"); 81 # Test ToC 82 ok($toc->format() . "\n", <<'EOT'); 83 <ul> 84 <li><a href=#h-1>Header</a> 85 <li><a href=#h-2>Header</a> 86 </ul> 87 EOT 88