1 #--- insert.t -----------------------------------------------------------------
2 # function: Test ToC insertion.
7 BEGIN { plan tests => 10; }
10 use HTML::TocGenerator;
11 use HTML::TocInsertor;
13 my ($output, $content, $filename);
14 my $toc = HTML::Toc->new;
15 my $tocGenerator = HTML::TocGenerator->new;
16 my $tocInsertor = HTML::TocInsertor->new;
28 $filename = "file$$.htm";
29 die "$filename is already there" if -e $filename;
30 open(FILE, ">$filename") || die "Can't create $filename: $!";
31 print FILE <<'EOT'; close(FILE);
39 unlink($filename) or warn "Can't unlink $filename: $!";
43 #--- 1. insert before start ---------------------------------------------------
45 $toc->setOptions({'insertionPoint' => 'before <h1>'});
47 $tocGenerator->generate($toc, "<h1>Header</h1>");
48 $tocInsertor->insert($toc, "<h1>Header</h1>", {
53 ok($output, "<ul>\n<li>Header\n</ul><h1>Header</h1>");
56 #--- 2. insert after start ----------------------------------------------------
58 $toc->setOptions({'insertionPoint' => 'after <h1>'});
60 $tocGenerator->generate($toc, "<h1>Header</h1>");
61 $tocInsertor->insert($toc, "<h1>Header</h1>", {
66 ok($output, "<h1><ul>\n<li>Header\n</ul>Header</h1>");
69 #--- 3. insert before end -----------------------------------------------------
71 $toc->setOptions({'insertionPoint' => 'before </h1>'});
73 $tocGenerator->generate($toc, "<h1>Header</h1>");
74 $tocInsertor->insert($toc, "<h1>Header</h1>", {
79 ok($output, "<h1>Header<ul>\n<li>Header\n</ul></h1>");
82 #--- 4. insert after end ------------------------------------------------------
84 $toc->setOptions({'insertionPoint' => 'after </h1>'});
86 $tocGenerator->generate($toc, "<h1>Header</h1>");
87 $tocInsertor->insert($toc, "<h1>Header</h1>", {
92 ok($output, "<h1>Header</h1><ul>\n<li>Header\n</ul>");
95 #--- 5. outputFile ------------------------------------------------------------
97 $toc->setOptions({'insertionPoint' => 'before <h1>'});
99 $tocGenerator->generate($toc, "<h1>Header</h1>");
100 # Insert ToC, output to file
101 $tocInsertor->insert($toc, "<h1>Header</h1>", {
102 'outputFile' => $filename,
106 open(FILE, "<$filename") || die "Can't open $filename: $!";
107 $content = join('', <FILE>);
110 ok($output, "<ul>\n<li>Header\n</ul><h1>Header</h1>");
113 #--- 6. empty toc -------------------------------------------------------------
115 $tocGenerator->generate($toc, "");
116 $tocInsertor->insert($toc, "", {
117 'output' => \$output,
123 #--- TestAfterDeclaration() ---------------------------------------------------
124 # function: Test putting HTML comment after declaration.
126 sub TestAfterDeclaration {
128 my $toc = HTML::Toc->new();
129 my $tocInsertor = HTML::TocInsertor->new();
134 'insertionPoint' => "after <!ToC>",
137 $tocInsertor->insert($toc, <<EOT, {'output' => \$output});
140 <h2>Appendix Paragraph</h2>
142 <h2>Appendix Paragraph</h2>
148 <!-- Table of Contents generated by Perl - HTML::Toc -->
150 <li><a href=#h-1>Appendix</a>
152 <li><a href=#h-1.1>Appendix Paragraph</a>
154 <li><a href=#h-2>Appendix</a>
156 <li><a href=#h-2.1>Appendix Paragraph</a>
159 <!-- End of generated Table of Contents -->
161 <a name=h-1><h1>Appendix</h1></a>
162 <a name=h-1.1><h2>Appendix Paragraph</h2></a>
163 <a name=h-2><h1>Appendix</h1></a>
164 <a name=h-2.1><h2>Appendix Paragraph</h2></a>
167 } # TestAfterDeclaration()
170 #--- TestNumberingStyle() -----------------------------------------------------
171 # function: Test numberingstyle.
173 sub TestNumberingStyle {
175 my $toc = HTML::Toc->new();
176 my $tocInsertor = HTML::TocInsertor->new();
181 'numberingStyle' => 'lower-alpha',
182 'doNumberToken' => 1,
184 'tokenBegin' => '<h1>',
186 'tokenBegin' => '<h2>',
188 'numberingStyle' => 'upper-alpha'
190 'tokenBegin' => '<h3>',
192 'numberingStyle' => 'decimal'
196 $tocInsertor->insert($toc, <<EOT, {'output' => \$output});
208 <!-- Table of Contents generated by Perl - HTML::Toc -->
210 <li><a href=#h-a>Chapter</a>
212 <li><a href=#h-a.A>Paragraph</a>
214 <li><a href=#h-a.A.1>Paragraph</a>
215 <li><a href=#h-a.A.2>Paragraph</a>
216 <li><a href=#h-a.A.3>Paragraph</a>
220 <!-- End of generated Table of Contents -->
222 <a name=h-a><h1>a Chapter</h1></a>
223 <a name=h-a.A><h2>a.A Paragraph</h2></a>
224 <a name=h-a.A.1><h3>a.A.1 Paragraph</h3></a>
225 <a name=h-a.A.2><h3>a.A.2 Paragraph</h3></a>
226 <a name=h-a.A.3><h3>a.A.3 Paragraph</h3></a>
229 } # TestNumberingStyle()
232 #--- TestReplaceComment() -----------------------------------------------------
233 # function: Test replacing HTML comment with ToC.
235 sub TestReplaceComment {
237 my $toc = HTML::Toc->new();
238 my $tocInsertor = HTML::TocInsertor->new();
243 'insertionPoint' => "replace <!-- ToC -->"
246 $tocInsertor->insert($toc, <<EOT, {'output' => \$output});
250 <h2>Appendix Paragraph</h2>
252 <h2>Appendix Paragraph</h2>
258 <!-- Table of Contents generated by Perl - HTML::Toc -->
260 <li><a href=#h-1>Appendix</a>
262 <li><a href=#h-1.1>Appendix Paragraph</a>
264 <li><a href=#h-2>Appendix</a>
266 <li><a href=#h-2.1>Appendix Paragraph</a>
269 <!-- End of generated Table of Contents -->
272 <a name=h-1><h1>Appendix</h1></a>
273 <a name=h-1.1><h2>Appendix Paragraph</h2></a>
274 <a name=h-2><h1>Appendix</h1></a>
275 <a name=h-2.1><h2>Appendix Paragraph</h2></a>
278 } # TestReplaceComment()
281 #--- TestReplaceText() -----------------------------------------------------
282 # function: Test replacing HTML comment with ToC.
284 sub TestReplaceText {
286 my $toc = HTML::Toc->new();
287 my $tocInsertor = HTML::TocInsertor->new();
292 'insertionPoint' => "replace ToC will be placed here[,]"
295 $tocInsertor->insert($toc, <<EOT, {'output' => \$output});
296 The ToC will be placed here, overnight.
299 <h2>Appendix Paragraph</h2>
301 <h2>Appendix Paragraph</h2>
307 <!-- Table of Contents generated by Perl - HTML::Toc -->
309 <li><a href=#h-1>Appendix</a>
311 <li><a href=#h-1.1>Appendix Paragraph</a>
313 <li><a href=#h-2>Appendix</a>
315 <li><a href=#h-2.1>Appendix Paragraph</a>
318 <!-- End of generated Table of Contents -->
320 <a name=h-1><h1>Appendix</h1></a>
321 <a name=h-1.1><h2>Appendix Paragraph</h2></a>
322 <a name=h-2><h1>Appendix</h1></a>
323 <a name=h-2.1><h2>Appendix Paragraph</h2></a>
326 } # TestReplaceText()
329 # 7. Test 'numberingStyle'
330 TestNumberingStyle();
331 # 8. Test replace comment
332 TestReplaceComment();
333 # 9. Test after declaration
334 TestAfterDeclaration();
335 # 10. Test replace text