#--- insert.t ----------------------------------------------------------------- # function: Test ToC insertion. use strict; use Test; BEGIN { plan tests => 10; } use HTML::Toc; use HTML::TocGenerator; use HTML::TocInsertor; my ($output, $content, $filename); my $toc = HTML::Toc->new; my $tocGenerator = HTML::TocGenerator->new; my $tocInsertor = HTML::TocInsertor->new; $toc->setOptions({ 'doLinkToToken' => 0, 'levelIndent' => 0, 'header' => "", 'footer' => "", }); BEGIN { # Create test file $filename = "file$$.htm"; die "$filename is already there" if -e $filename; open(FILE, ">$filename") || die "Can't create $filename: $!"; print FILE <<'EOT'; close(FILE);

Header

EOT } END { # Remove test file unlink($filename) or warn "Can't unlink $filename: $!"; } #--- 1. insert before start --------------------------------------------------- $toc->setOptions({'insertionPoint' => 'before

'}); # Generate ToC $tocGenerator->generate($toc, "

Header

"); $tocInsertor->insert($toc, "

Header

", { 'output' => \$output, 'doGenerateToc' => 0 }); # Test ToC ok($output, "

Header

"); #--- 2. insert after start ---------------------------------------------------- $toc->setOptions({'insertionPoint' => 'after

'}); # Generate ToC $tocGenerator->generate($toc, "

Header

"); $tocInsertor->insert($toc, "

Header

", { 'output' => \$output, 'doGenerateToc' => 0 }); # Test ToC ok($output, "

Header

"); #--- 3. insert before end ----------------------------------------------------- $toc->setOptions({'insertionPoint' => 'before '}); # Generate ToC $tocGenerator->generate($toc, "

Header

"); $tocInsertor->insert($toc, "

Header

", { 'output' => \$output, 'doGenerateToc' => 0 }); # Test ToC ok($output, "

Header

"); #--- 4. insert after end ------------------------------------------------------ $toc->setOptions({'insertionPoint' => 'after '}); # Generate ToC $tocGenerator->generate($toc, "

Header

"); $tocInsertor->insert($toc, "

Header

", { 'output' => \$output, 'doGenerateToc' => 0 }); # Test ToC ok($output, "

Header

"); #--- 5. outputFile ------------------------------------------------------------ $toc->setOptions({'insertionPoint' => 'before

'}); # Generate ToC $tocGenerator->generate($toc, "

Header

"); # Insert ToC, output to file $tocInsertor->insert($toc, "

Header

", { 'outputFile' => $filename, 'doGenerateToc' => 0 }); # Read outputfile open(FILE, "<$filename") || die "Can't open $filename: $!"; $content = join('', ); close(FILE); # Test ToC ok($output, "

Header

"); #--- 6. empty toc ------------------------------------------------------------- $tocGenerator->generate($toc, ""); $tocInsertor->insert($toc, "", { 'output' => \$output, 'doGenerateToc' => 0 }); ok($output, ""); #--- TestAfterDeclaration() --------------------------------------------------- # function: Test putting HTML comment after declaration. sub TestAfterDeclaration { # Create objects my $toc = HTML::Toc->new(); my $tocInsertor = HTML::TocInsertor->new(); my $output; # Set ToC options $toc->setOptions({ 'insertionPoint' => "after ", }); # Generate ToC $tocInsertor->insert($toc, < \$output});

Appendix

Appendix Paragraph

Appendix

Appendix Paragraph

EOT # Test ToC ok($output, <

Appendix

Appendix Paragraph

Appendix

Appendix Paragraph

EOT } # TestAfterDeclaration() #--- TestNumberingStyle() ----------------------------------------------------- # function: Test numberingstyle. sub TestNumberingStyle { # Create objects my $toc = HTML::Toc->new(); my $tocInsertor = HTML::TocInsertor->new(); my $output; # Set ToC options $toc->setOptions({ 'numberingStyle' => 'lower-alpha', 'doNumberToken' => 1, 'tokenToToc' => [{ 'tokenBegin' => '

', }, { 'tokenBegin' => '

', 'level' => 2, 'numberingStyle' => 'upper-alpha' }, { 'tokenBegin' => '

', 'level' => 3, 'numberingStyle' => 'decimal' }] }); # Generate ToC $tocInsertor->insert($toc, < \$output});

Chapter

Paragraph

Paragraph

Paragraph

Paragraph

EOT # Test ToC ok($output, <

a  Chapter

a.A  Paragraph

a.A.1  Paragraph

a.A.2  Paragraph

a.A.3  Paragraph

EOT } # TestNumberingStyle() #--- TestReplaceComment() ----------------------------------------------------- # function: Test replacing HTML comment with ToC. sub TestReplaceComment { # Create objects my $toc = HTML::Toc->new(); my $tocInsertor = HTML::TocInsertor->new(); my $output; # Set ToC options $toc->setOptions({ 'insertionPoint' => "replace " }); # Generate ToC $tocInsertor->insert($toc, < \$output});

Appendix

Appendix Paragraph

Appendix

Appendix Paragraph

EOT # Test ToC ok($output, <

Appendix

Appendix Paragraph

Appendix

Appendix Paragraph

EOT } # TestReplaceComment() #--- TestReplaceText() ----------------------------------------------------- # function: Test replacing HTML comment with ToC. sub TestReplaceText { # Create objects my $toc = HTML::Toc->new(); my $tocInsertor = HTML::TocInsertor->new(); my $output; # Set ToC options $toc->setOptions({ 'insertionPoint' => "replace ToC will be placed here[,]" }); # Generate ToC $tocInsertor->insert($toc, < \$output}); The ToC will be placed here, overnight.

Appendix

Appendix Paragraph

Appendix

Appendix Paragraph

EOT # Test ToC ok($output, <

Appendix

Appendix Paragraph

Appendix

Appendix Paragraph

EOT } # TestReplaceText() # 7. Test 'numberingStyle' TestNumberingStyle(); # 8. Test replace comment TestReplaceComment(); # 9. Test after declaration TestAfterDeclaration(); # 10. Test replace text TestReplaceText();