#--- format.t -----------------------------------------------------------------
# function: Test ToC formatting.
use strict;
use Test;
BEGIN { plan tests => 6; }
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,
'insertionPoint' => 'before
',
'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. templateLevelBegin ----------------------------------------------------
$toc->setOptions({
'templateLevelBegin' => '"\n"'
});
$tocInsertor->insert($toc, "Header
", {'output' => \$output});
ok($output, "Header
");
$toc->setOptions({'templateLevelBegin' => undef});
#--- 2. levelToToc -----------------------------------------------------------
$tocGenerator->generate($toc, "Header1
\nHeader2
");
$toc->setOptions({'levelToToc' => '1'});
ok($toc->format(), "");
$toc->setOptions({'levelToToc' => '.*'});
#--- 3. groupToToc -----------------------------------------------------------
$toc->setOptions({
'tokenToToc' => [{
'level' => 1,
'tokenBegin' => ''
}, {
'groupId' => 'foo',
'level' => 1,
'tokenBegin' => ''
}]
});
$tocGenerator->generate($toc, "Header1
\nFoo
");
$toc->setOptions({'groupToToc' => 'foo'});
ok($toc->format(), "");
$toc->setOptions({'groupToToc' => '.*'});
#--- 4. header & footer -------------------------------------------------------
$toc->setOptions({
'tokenToToc' => [{
'level' => 1,
'tokenBegin' => ''
}],
'header' => '',
'footer' => '',
});
$tocInsertor->insert($toc, "Header1
", {'output' => \$output});
ok("$output\n", <Header1
EOT
# Test 'doSingleStepLevel' => 1
TestSingleStepLevel1();
# Test 'doSingleStepLevel' => 0
TestSingleStepLevel0();
#--- 5. TestSingleStepLevel1 --------------------------------------------------
sub TestSingleStepLevel1 {
my $toc = new HTML::Toc;
my $tocGenerator = new HTML::TocGenerator;
# Generate ToC
$tocGenerator->generate($toc, <Header 1
Header 3
EOT
# Compare output
ok($toc->format(), <
EOT
} # TestSingleStepLevel1()
#--- 6. TestSingleStepLevel0 --------------------------------------------------
sub TestSingleStepLevel0 {
my $toc = new HTML::Toc;
my $tocGenerator = new HTML::TocGenerator;
# Set ToC options
$toc->setOptions({'doSingleStepLevel' => 0});
# Generate ToC
$tocGenerator->generate($toc, <Header 1
Header 3
EOT
# Compare output
ok($toc->format(), <
EOT
} # TestSingleStepLevel0()