• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#--- podExamples.t ------------------------------------------------------------
2# function: Test HTML::ToC.  In particular test the examples as described in
3#           the POD documentation.
4
5use strict;
6use Test;
7
8BEGIN { plan tests => 13; }
9
10use HTML::Toc;
11use HTML::TocGenerator;
12use HTML::TocInsertor;
13use HTML::TocUpdator;
14
15my ($filename, $filename2);
16
17
18BEGIN {
19		# Create test file
20	$filename = "tmp.htm";
21	die "$filename is already there" if -e $filename;
22		# Create test file 2
23	$filename2 = "tmp2.htm";
24	die "$filename2 is already there" if -e $filename2;
25}
26
27
28END {
29		# Remove test file
30	unlink($filename) or warn "Can't unlink $filename: $!";
31		# Remove test file 2
32	unlink($filename2) or warn "Can't unlink $filename2: $!";
33}
34
35
36#--- TestExtendFromFile() --------------------------------------------------
37# function: Test 'HTML::TocGenerator->extendFromFile()
38
39sub TestExtendFromFile {
40		# Assemble test file
41	open(FILE, ">$filename") || die "Can't create $filename: $!";
42	print FILE <<'EOT'; close(FILE);
43<body>
44   <h1>Chapter of document 1</h1>
45</body>
46EOT
47
48		# Assemble test file 2
49	open(FILE, ">$filename2") || die "Can't create $filename2: $!";
50	print FILE <<'EOT'; close(FILE);
51<html>
52<body>
53   <h1>Chapter of document 2</h1>
54</body>
55</html>
56EOT
57
58		# Create objects
59	my $toc          = HTML::Toc->new();
60	my $tocGenerator = HTML::TocGenerator->new();
61
62		# Set ToC options
63   $toc->setOptions({'doLinkToFile' => 1});
64		# Generate ToC
65	$tocGenerator->generateFromFile($toc, $filename);
66	$tocGenerator->extendFromFile($toc, $filename2);
67		# Test ToC
68	ok($toc->format(), <<EOT);
69
70<!-- Table of Contents generated by Perl - HTML::Toc -->
71<ul>
72   <li><a href=tmp.htm#h-1>Chapter of document 1</a>
73   <li><a href=tmp2.htm#h-2>Chapter of document 2</a>
74</ul>
75<!-- End of generated Table of Contents -->
76EOT
77}  # TestExtendFromFile()
78
79
80#--- TestGenerateFromFiles() --------------------------------------------------
81# function: Test 'HTML::TocGenerator->generateFromFile()
82
83sub TestGenerateFromFiles {
84		# Assemble test file
85	open(FILE, ">$filename") || die "Can't create $filename: $!";
86	print FILE <<'EOT'; close(FILE);
87<body>
88   <h1>Chapter of document 1</h1>
89</body>
90EOT
91
92		# Assemble test file 2
93	open(FILE, ">$filename2") || die "Can't create $filename2: $!";
94	print FILE <<'EOT'; close(FILE);
95<html>
96<body>
97   <h1>Chapter of document 2</h1>
98</body>
99</html>
100EOT
101
102		# Create objects
103	my $toc          = HTML::Toc->new();
104	my $tocGenerator = HTML::TocGenerator->new();
105
106		# Set ToC options
107   $toc->setOptions({'doLinkToFile' => 1});
108		# Generate ToC
109	$tocGenerator->generateFromFile($toc, [$filename, $filename2]);
110		# Test ToC
111	ok($toc->format(), <<EOT);
112
113<!-- Table of Contents generated by Perl - HTML::Toc -->
114<ul>
115   <li><a href=tmp.htm#h-1>Chapter of document 1</a>
116   <li><a href=tmp2.htm#h-2>Chapter of document 2</a>
117</ul>
118<!-- End of generated Table of Contents -->
119EOT
120}  # TestGenerateFromFiles()
121
122
123#--- TestGenerateFromFiles() --------------------------------------------------
124# function: Test 'HTML::TocGenerator->generateFromFile() using multiple files.
125
126sub TestGenerateFromFile {
127		# Assemble test file 1
128	open(FILE, ">$filename") || die "Can't create $filename: $!";
129	print FILE <<'EOT'; close(FILE);
130<html>
131<body>
132   <h1>Chapter</h1>
133</body>
134</html>
135EOT
136
137		# Create objects
138	my $toc          = HTML::Toc->new();
139	my $tocGenerator = HTML::TocGenerator->new();
140
141		# Generate ToC
142	$tocGenerator->generateFromFile($toc, $filename);
143		# Test ToC
144	ok($toc->format(), <<EOT);
145
146<!-- Table of Contents generated by Perl - HTML::Toc -->
147<ul>
148   <li><a href=#h-1>Chapter</a>
149</ul>
150<!-- End of generated Table of Contents -->
151EOT
152}  # TestGenerateFromFile()
153
154
155#--- TestInsertIntoFile() -----------------------------------------------------
156# function: Test 'HTML::TocInsertor->insertIntoFile()
157
158sub TestInsertIntoFile {
159		# Assemble test file
160	open(FILE, ">$filename") || die "Can't create $filename: $!";
161	print FILE <<'EOT'; close(FILE);
162<html>
163<body>
164   <h1>Chapter</h1>
165</body>
166</html>
167EOT
168
169		# Create objects
170	my $toc         = HTML::Toc->new();
171	my $tocInsertor = HTML::TocInsertor->new();
172	my $output;
173
174		# Generate ToC
175	$tocInsertor->insertIntoFile($toc, $filename, {'output' => \$output});
176		# Test ToC
177	ok($output, <<EOT);
178<html>
179<body>
180<!-- Table of Contents generated by Perl - HTML::Toc -->
181<ul>
182   <li><a href=#h-1>Chapter</a>
183</ul>
184<!-- End of generated Table of Contents -->
185
186   <a name=h-1><h1>Chapter</h1></a>
187</body>
188</html>
189EOT
190}  # TestInsertIntoFile()
191
192
193#--- TestInsertIntoFileUsingTocUpdator() --------------------------------------
194# function: Test 'HTML::TocUpdator->insertIntoFile()
195
196sub TestInsertIntoFileUsingTocUpdator {
197		# Assemble test file
198	open(FILE, ">$filename") || die "Can't create $filename: $!";
199	print FILE <<'EOT'; close(FILE);
200<html>
201<body>
202   <h1>
203   Chapter
204   </h1>
205</body>
206</html>
207EOT
208
209		# Create objects
210	my $toc        = HTML::Toc->new();
211	my $tocUpdator = HTML::TocUpdator->new();
212	my $output;
213
214		# Generate ToC
215	$tocUpdator->insertIntoFile($toc, $filename, {'output' => \$output});
216		# Test ToC
217	ok($output, <<EOT);
218<html>
219<body><!-- #BeginToc -->
220<!-- Table of Contents generated by Perl - HTML::Toc -->
221<ul>
222   <li><a href=#h-1> Chapter </a>
223</ul>
224<!-- End of generated Table of Contents -->
225<!-- #EndToc -->
226   <!-- #BeginTocAnchorNameBegin --><a name=h-1><!-- #EndTocAnchorNameBegin --><h1>
227   Chapter
228   </h1><!-- #BeginTocAnchorNameEnd --></a><!-- #EndTocAnchorNameEnd -->
229</body>
230</html>
231EOT
232}  # TestInsertIntoFileUsingTocUpdator()
233
234
235#--- TestGlobalGroups0() ------------------------------------------------------
236# function: Test 'HTML::TocGenerator' option 'doUseGroupsGlobal = 0'.
237
238sub TestGlobalGroups0 {
239		# Assemble test file
240	open(FILE, ">$filename") || die "Can't create $filename: $!";
241	print FILE <<'EOT'; close(FILE);
242<h1>Chapter</h1>
243<h2>Paragraph</h2>
244EOT
245
246		# Create objects
247	my $toc1         = HTML::Toc->new();
248	my $toc2         = HTML::Toc->new();
249	my $tocGenerator = HTML::TocGenerator->new();
250
251		# Set options
252	$toc1->setOptions({
253		'header'      => '',
254		'footer'      => '',
255		'tokenToToc' => [{'tokenBegin' => '<h1>'}]
256	});
257	$toc2->setOptions({
258		'header'      => '',
259		'footer'      => '',
260		'tokenToToc' => [{'tokenBegin' => '<h2>'}]
261	});
262		# Generate ToC
263	$tocGenerator->generateFromFile([$toc1, $toc2], $filename);
264		# Test ToC
265	ok($toc1->format() . $toc2->format() . "\n", <<'EOT');
266<ul>
267   <li><a href=#h-1>Chapter</a>
268</ul><ul>
269   <li><a href=#h-1>Paragraph</a>
270</ul>
271EOT
272}	# TestGlobalGroups0()
273
274
275#--- TestGlobalGroups1() ------------------------------------------------------
276# function: Test 'HTML::TocGenerator' option 'doUseGroupsGlobal = 0'.
277
278sub TestGlobalGroups1 {
279		# Assemble test file
280	open(FILE, ">$filename") || die "Can't create $filename: $!";
281	print FILE <<'EOT';
282<h1>Chapter</h1>
283<h2>Paragraph</h2>
284EOT
285	close(FILE);
286
287		# Create objects
288	my $toc1         = HTML::Toc->new();
289	my $toc2         = HTML::Toc->new();
290	my $tocGenerator = HTML::TocGenerator->new();
291
292		# Set options
293	$toc1->setOptions({
294		'header'      => '',
295		'footer'      => '',
296		'tokenToToc' => [{'tokenBegin' => '<h1>'}]
297	});
298	$toc2->setOptions({
299		'header'      => '',
300		'footer'      => '',
301		'tokenToToc' => [{'tokenBegin' => '<h2>'}]
302	});
303		# Generate ToC
304	$tocGenerator->generateFromFile(
305		[$toc1, $toc2], $filename, {'doUseGroupsGlobal' => 1}
306	);
307		# Test ToC
308	ok($toc1->format() . $toc2->format() . "\n", <<'EOT');
309<ul>
310   <li><a href=#h-1>Chapter</a>
311</ul><ul>
312   <li><a href=#h-2>Paragraph</a>
313</ul>
314EOT
315}	# TestGlobalGroups1()
316
317
318#--- TestMultipleGroupsAppendix() ---------------------------------------------
319# function: Test multiple ToCs
320
321sub TestMultipleGroupsAppendix() {
322		# Create objects
323	my $toc         = HTML::Toc->new();
324	my $tocInsertor = HTML::TocInsertor->new();
325	my $output;
326
327		# Set ToC options
328	$toc->setOptions({
329		'tokenToToc' => [{
330				'tokenBegin' => '<h1 class=-appendix>'
331			}, {
332				'tokenBegin' => '<h2 class=-appendix>',
333				'level'      => 2
334			}, {
335				'groupId'    => 'appendix',
336				'tokenBegin' => '<h1 class=appendix>',
337			}, {
338				'groupId'    => 'appendix',
339				'tokenBegin' => '<h2 class=appendix>',
340				'level'      => 2
341			}],
342	});
343		# Generate ToC
344	$tocInsertor->insert($toc, <<EOT, {'output' => \$output});
345<body>
346   <h1>Chapter</h1>
347   <h2>Paragraph</h2>
348   <h3>Subparagraph</h3>
349   <h1>Chapter</h1>
350   <h1 class=appendix>Appendix Chapter</h1>
351   <h2 class=appendix>Appendix Paragraph</h2>
352</body>
353EOT
354		# Test ToC
355	ok($output, <<EOT);
356<body>
357<!-- Table of Contents generated by Perl - HTML::Toc -->
358<ul>
359   <li><a href=#h-1>Chapter</a>
360   <ul>
361      <li><a href=#h-1.1>Paragraph</a>
362   </ul>
363   <li><a href=#h-2>Chapter</a>
364</ul>
365<ul>
366   <li><a href=#appendix-1>Appendix Chapter</a>
367   <ul>
368      <li><a href=#appendix-1.1>Appendix Paragraph</a>
369   </ul>
370</ul>
371<!-- End of generated Table of Contents -->
372
373   <a name=h-1><h1>Chapter</h1></a>
374   <a name=h-1.1><h2>Paragraph</h2></a>
375   <h3>Subparagraph</h3>
376   <a name=h-2><h1>Chapter</h1></a>
377   <a name=appendix-1><h1 class=appendix>Appendix Chapter</h1></a>
378   <a name=appendix-1.1><h2 class=appendix>Appendix Paragraph</h2></a>
379</body>
380EOT
381}  # TestMultipleGroupsAppendix()
382
383
384#--- TestMultipleGroupsPart() -------------------------------------------------
385# function: Test multiple ToCs
386
387sub TestMultipleGroupsPart() {
388		# Create objects
389	my $toc         = HTML::Toc->new();
390	my $tocInsertor = HTML::TocInsertor->new();
391	my $output;
392
393		# Set ToC options
394	$toc->setOptions({
395		'tokenToToc' => [{
396				'tokenBegin' => '<h1 class=-part>'
397			}, {
398				'tokenBegin' => '<h2 class=-part>',
399				'level'      => 2,
400			}, {
401				'groupId'        => 'part',
402				'tokenBegin'     => '<h1 class=part>',
403				'level'          => 1,
404				'doNumberToken'  => 1,
405				'numberingStyle' => 'upper-alpha'
406			}]
407	});
408		# Generate ToC
409	$tocInsertor->insert($toc, <<EOT, {'output' => \$output});
410<body>
411   <h1 class=part>First Part</h1>
412   <h1>Chapter</h1>
413   <h2>Paragraph</h2>
414   <h1 class=part>Second Part</h1>
415   <h1>Chapter</h1>
416   <h2>Paragraph</h2>
417</body>
418EOT
419		# Test ToC
420	ok($output, <<EOT);
421<body>
422<!-- Table of Contents generated by Perl - HTML::Toc -->
423<ul>
424   <li><a href=#part-A>First Part</a>
425</ul>
426<ul>
427   <li><a href=#h-1>Chapter</a>
428   <ul>
429      <li><a href=#h-1.1>Paragraph</a>
430   </ul>
431</ul>
432<ul>
433   <li><a href=#part-B>Second Part</a>
434</ul>
435<ul>
436   <li><a href=#h-2>Chapter</a>
437   <ul>
438      <li><a href=#h-2.1>Paragraph</a>
439   </ul>
440</ul>
441<!-- End of generated Table of Contents -->
442
443   <a name=part-A><h1 class=part>A &nbsp;First Part</h1></a>
444   <a name=h-1><h1>Chapter</h1></a>
445   <a name=h-1.1><h2>Paragraph</h2></a>
446   <a name=part-B><h1 class=part>B &nbsp;Second Part</h1></a>
447   <a name=h-2><h1>Chapter</h1></a>
448   <a name=h-2.1><h2>Paragraph</h2></a>
449</body>
450EOT
451}  # TestMultipleGroupsPart()
452
453
454#--- TestMultipleTocs() -------------------------------------------------------
455# function: Test multiple ToCs
456
457sub TestMultipleTocs() {
458		# Assemble test file
459	open(FILE, ">$filename") || die "Can't create $filename: $!";
460	print FILE <<'EOT'; close(FILE);
461<body>
462   <h1>Header One</h1>
463   <img src=test1.gif alt="First picture">
464   <h2>Paragraph One</h2>
465   <img src=test2.gif alt="Second picture">
466</body>
467EOT
468
469		# Create objects
470	my $toc1        = HTML::Toc->new();
471	my $toc2        = HTML::Toc->new();
472	my $tocInsertor = HTML::TocInsertor->new();
473	my $output;
474
475		# Set ToC options
476	$toc2->setOptions({
477		'tokenToToc'             => [{
478			'groupId'    => 'image',
479			'tokenBegin' => '<img alt=@>'
480		}],
481	});
482		# Generate ToC
483	$tocInsertor->insertIntoFile(
484		[$toc1, $toc2], $filename, {'output' => \$output}
485	);
486		# Test ToC
487	ok($output, <<EOT);
488<body>
489<!-- Table of Contents generated by Perl - HTML::Toc -->
490<ul>
491   <li><a href=#h-1>Header One</a>
492   <ul>
493      <li><a href=#h-1.1>Paragraph One</a>
494   </ul>
495</ul>
496<!-- End of generated Table of Contents -->
497
498<!-- Table of Contents generated by Perl - HTML::Toc -->
499<ul>
500   <li><a href=#image-1>First picture</a>
501   <li><a href=#image-2>Second picture</a>
502</ul>
503<!-- End of generated Table of Contents -->
504
505   <a name=h-1><h1>Header One</h1></a>
506   <a name=image-1><img src=test1.gif alt="First picture"></a>
507   <a name=h-1.1><h2>Paragraph One</h2></a>
508   <a name=image-2><img src=test2.gif alt="Second picture"></a>
509</body>
510EOT
511}  # TestMultipleTocs()
512
513
514#--- TestSpecifyNumberedList() ------------------------------------------------
515# function: Test specifying numbered list.
516
517sub TestSpecifyNumberedList {
518		# Assemble test file
519	open(FILE, ">$filename") || die "Can't create $filename: $!";
520	print FILE <<'EOT'; close(FILE);
521<body>
522   <h1>Chapter</h1>
523	<h2>Paragraph</h2>
524</body>
525EOT
526
527		# Create objects
528	my $toc        = HTML::Toc->new();
529   my $tocGenerator = HTML::TocGenerator->new();
530
531		# Set ToC options
532	$toc->setOptions({
533		'templateLevelBegin' => '"<ol>\n"',
534		'templateLevelEnd'   => '"</ol>\n"',
535	});
536		# Generate ToC
537	$tocGenerator->generateFromFile($toc, $filename);
538		# Test ToC
539	ok($toc->format(), <<EOT);
540
541<!-- Table of Contents generated by Perl - HTML::Toc -->
542<ol>
543   <li><a href=#h-1>Chapter</a>
544   <ol>
545      <li><a href=#h-1.1>Paragraph</a>
546   </ol>
547</ol>
548<!-- End of generated Table of Contents -->
549EOT
550}  # TestSpecifyNumberedList()
551
552
553#--- TestUpdateFile() ---------------------------------------------------------
554# function: Test 'HTML::TocUpdator->updateFile()'
555
556sub TestUpdateFile {
557		# Assemble test file
558	open(FILE, ">$filename") || die "Can't create $filename: $!";
559	print FILE <<'EOT'; close(FILE);
560<html>
561<body><!-- #BeginToc -->
562foo
563<!-- #EndToc -->
564   <!-- #BeginTocAnchorNameBegin -->bar<!-- #EndTocAnchorNameBegin --><h1>
565   Chapter
566   </h1><!-- #BeginTocAnchorNameEnd -->foo<!-- #EndTocAnchorNameEnd -->
567</body>
568</html>
569EOT
570
571		# Create objects
572	my $toc        = HTML::Toc->new();
573	my $tocUpdator = HTML::TocUpdator->new();
574	my $output;
575
576		# Generate ToC
577	$tocUpdator->updateFile($toc, $filename, {'output' => \$output});
578		# Test ToC
579	ok($output, <<EOT);
580<html>
581<body><!-- #BeginToc -->
582<!-- Table of Contents generated by Perl - HTML::Toc -->
583<ul>
584   <li><a href=#h-1> Chapter </a>
585</ul>
586<!-- End of generated Table of Contents -->
587<!-- #EndToc -->
588   <!-- #BeginTocAnchorNameBegin --><a name=h-1><!-- #EndTocAnchorNameBegin --><h1>
589   Chapter
590   </h1><!-- #BeginTocAnchorNameEnd --></a><!-- #EndTocAnchorNameEnd -->
591</body>
592</html>
593EOT
594}  # TestUpdateFile()
595
596
597#--- TestUsingCSS() -----------------------------------------------------------
598# function: Test multiple ToCs
599
600sub TestUsingCSS() {
601
602		# Create objects
603	my $toc          = new HTML::Toc;
604	my $tocInsertor  = new HTML::TocInsertor;
605	my $output;
606
607   $toc->setOptions({
608		'templateLevelBegin'   => '"<ol class=toc_$groupId$level>\n"',
609		'templateLevelEnd'     => '"</ol>\n"',
610		'doNumberToken'        => 1,
611      'tokenToToc' => [{
612				'groupId'        => 'appendix',
613            'tokenBegin'     => '<h1>',
614				'numberingStyle' => 'upper-alpha'
615			}, {
616				'groupId'        => 'appendix',
617            'tokenBegin' 	  => '<h2>',
618            'level'      	  => 2,
619         }]
620   });
621	$tocInsertor->insert($toc, <<EOT);
622<html>
623<head>
624   <style type="text/css">
625      ol.toc_appendix1 { list-style-type: upper-alpha }
626   </style>
627</head>
628<body>
629   <h1>Appendix</h1>
630   <h2>Appendix Paragraph</h2>
631   <h1>Appendix</h1>
632   <h2>Appendix Paragraph</h2>
633</body>
634</html>
635EOT
636		# Insert ToC
637	$tocInsertor->insert($toc, <<EOT, {'output' => \$output});
638<html>
639<head>
640   <style type="text/css">
641      ol.toc_appendix1 { list-style-type: upper-alpha }
642   </style>
643</head>
644<body>
645   <h1>Appendix</h1>
646   <h2>Appendix Paragraph</h2>
647   <h1>Appendix</h1>
648   <h2>Appendix Paragraph</h2>
649</body>
650</html>
651EOT
652		# Test ToC
653	ok($output, <<EOT);
654<html>
655<head>
656   <style type="text/css">
657      ol.toc_appendix1 { list-style-type: upper-alpha }
658   </style>
659</head>
660<body>
661<!-- Table of Contents generated by Perl - HTML::Toc -->
662<ol class=toc_appendix1>
663   <li><a href=#appendix-A>Appendix</a>
664   <ol class=toc_appendix2>
665      <li><a href=#appendix-A.1>Appendix Paragraph</a>
666   </ol>
667   <li><a href=#appendix-B>Appendix</a>
668   <ol class=toc_appendix2>
669      <li><a href=#appendix-B.1>Appendix Paragraph</a>
670   </ol>
671</ol>
672<!-- End of generated Table of Contents -->
673
674   <a name=appendix-A><h1>A &nbsp;Appendix</h1></a>
675   <a name=appendix-A.1><h2>A.1 &nbsp;Appendix Paragraph</h2></a>
676   <a name=appendix-B><h1>B &nbsp;Appendix</h1></a>
677   <a name=appendix-B.1><h2>B.1 &nbsp;Appendix Paragraph</h2></a>
678</body>
679</html>
680EOT
681}  # TestUsingCSS()
682
683
684	# Test 'extendFromFile()'
685TestExtendFromFile();
686	# Test 'generateFromFile()'
687TestGenerateFromFile();
688	# Test 'generateFromFiles()'
689TestGenerateFromFiles();
690	# Test 'doUseGroupsGlobal = 0'
691TestGlobalGroups0();
692	# Test 'doUseGroupsGlobal = 1'
693TestGlobalGroups1();
694	# Test 'tocInsertor->insertIntoFile'
695TestInsertIntoFile();
696	# Test 'tocUpdator->insertIntoFile'
697TestInsertIntoFileUsingTocUpdator();
698	# Test additional 'appendix' group
699TestMultipleGroupsAppendix();
700	# Test additional 'part' group
701TestMultipleGroupsPart();
702	# Test multiple ToCs
703TestMultipleTocs();
704	# Test specifying numbered list
705TestSpecifyNumberedList();
706	# Test 'updateFile()'
707TestUpdateFile();
708	# Test using CSS
709TestUsingCSS();
710