Aozora2Epub

 view release on metacpan or  search on metacpan

t/toc.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More;
use Test::Base;
use Aozora2Epub;
use Aozora2Epub::Gensym;
use lib qw/./;
use t::Util;

sub drop_nlsp {
    my $s = shift;
    $s =~ s/\n *//sg;
    $s;
}

filters {
    html => 'chomp',
    name => 'yaml',
    id => 'yaml',
    title => 'yaml',
    file => 'yaml',
    subsection => 'yaml',
};

run {
    my $block = shift;
    Aozora2Epub::Gensym->reset_counter;
    my $aozora = Aozora2Epub->new($block->html);
    my $toc = $aozora->toc;

    for my $k (qw/id title file/) {
        no strict 'refs';
        my $expected = $block->$k;
        next unless $expected;
        my @got = map { $_->{$k} } @$toc;
        is_deeply(\@got, $expected, $block->name . " ". $k);
    }
    # subsection
    my $ss_list = $block->subsection;
    for my $ss (@$ss_list) {
        my $parent = $ss->{parent};
        my $subsections = $toc->[$ss->{parent}]->{children};
        my @got = map { { id => $_->{id}, title => $_->{title}, file => $_->{file} } }
                      @$subsections;
        is_deeply(\@got, $ss->{expected}, $block->name . " subsection of section $parent");
    }
};

done_testing;

__DATA__

=== h4 only
--- html
<div class="jisage_6" style="margin-left: 6em">
  <h4 class="naka-midashi"><a class="midashi_anchor" id="midashi10">いち</a></h4>
</div>
あいう
<div class="jisage_6" style="margin-left: 6em">
  <h4 class="naka-midashi"><a class="midashi_anchor" id="midashi11">に</a></h4>
</div>
えお



( run in 1.061 second using v1.01-cache-2.11-cpan-99c4e6809bf )