use strict; use warnings; use Test::More 0.88 (); use Test::Most; plan skip_all => 'These tests requires HTML::Tidy' unless eval { require HTML::Tidy; 1 }; use File::Slurp qw( read_file ); use Path::Class qw( file ); use Text::TOC::HTML; { my $gen = Text::TOC::HTML->new(); my $file = file( 't', 'corpus', 'headers.html' ); $gen->add_file( file => $file ); my $toc_html = <<'EOF'; EOF html_output_ok( $gen->html_for_toc(), $toc_html, 'unordered list for single-document TOC' ); my $page_html = <<'EOF';

Page Title

Some content.

H2 A

More content.

H3 A

More content.

H4 A

More content.

H5 A

More content.

H6 A

More content.

H2 B

More content.

H2 C

More content.

H2 D

More content.

H4 B

More content.

EOF html_output_ok( $gen->html_for_document($file), $page_html, 'unordered list for single-document TOC' ); html_output_ok( $gen->html_for_document_body($file), $page_html, 'unordered list for single-document TOC' ); } { my $gen = Text::TOC::HTML->new(); my $file = file( 't', 'corpus', 'headers.html' ); $gen->add_file( file => $file, content => scalar read_file( $file->stringify() ), ); my $toc_html = <<'EOF'; EOF html_output_ok( $gen->html_for_toc(), $toc_html, 'unordered list for single-document TOC - content as string' ); } { my $gen = Text::TOC::HTML->new( style => 'ordered' ); $gen->add_file( file => file( 't', 'corpus', 'headers.html' ) ); my $toc_html = <<'EOF';
  1. H2 A
    1. H3 A
      1. H4 A
  2. H2 B
  3. H2 C
  4. H2 D
      1. H4 B
EOF html_output_ok( $gen->html_for_toc(), $toc_html, 'ordered list for single-document TOC' ); } { my $gen = Text::TOC::HTML->new( multi => 1 ); $gen->add_file( file => file( 't', 'corpus', 'headers.html' ) ); my $toc_html = <<'EOF'; EOF html_output_ok( $gen->html_for_toc(), $toc_html, 'unordered list for multi-document TOC' ); } { my $gen = Text::TOC::HTML->new( multi => 1 ); $gen->add_file( file => file( 't', 'corpus', 'headers.html' ) ); $gen->add_file( file => file( 't', 'corpus', 'more.html' ) ); my $toc_html = <<'EOF'; EOF html_output_ok( $gen->html_for_toc(), $toc_html, 'unordered list for multi-document TOC with multiple documents' ); } { my $gen = Text::TOC::HTML->new(); my $file = file( 't', 'corpus', 'out-of-order-headers.html' ); $gen->add_file( file => $file ); my $toc_html = <<'EOF'; EOF html_output_ok( $gen->html_for_toc(), $toc_html, 'can handle out of order headers (first h3 before first h2)' ); } { my $gen = Text::TOC::HTML->new(); my $file = file( 't', 'corpus', 'headers.html' )->stringify(); lives_ok { $gen->add_file( file => $file ) } 'Can pass a string path to add_file'; } done_testing(); sub html_output_ok { my $got_html = shift; my $expect_html = shift; my $desc = shift; my $tidy = HTML::Tidy->new( { doctype => 'transitional', 'sort-attributes' => 'alpha', } ); my $real_expect_html = <<"EOF"; $expect_html EOF my ($got_body) = $tidy->clean($got_html) =~ m{(.+)}s; my ($expect_body) = $tidy->clean($real_expect_html) =~ m{(.+)}s; eq_or_diff( $got_body, $expect_body, $desc ); }