### ----------------------------------------------------
### -- Raku::Pod::Render
### -- Licenses: Artistic-2.0
### -- Authors: Richard Hainsworth
### -- File: xt/20-page-components-rakudoc.rakutest
### ----------------------------------------------------
use Test;
use Test::Deeply::Relaxed;
use ProcessedPod;

plan 14;

my ProcessedPod $processor .= new;
# A helper sub for RakuClosureTemplates
sub gen-closure-template (Str $tag) is export {
    my $start = '<' ~ $tag ~ '>';
    my $end = '</' ~ $tag ~ '>';
    return sub (%prm, %tml? --> Str) {
        $start ~ (%prm<contents> // '') ~ $end;
    }
}
use RenderPod::Templating;
my @templates = SetupTemplates.new.required;

my %templates  = @templates Z=> @templates.map( { gen-closure-template( $_ ) });
%templates<escaped> = sub ($s) { $s };

# the following pod creates Footnotes, Meta, TOC and Glossary structures, which will only render minimally as above,
# Actual rendering will be tested in html tests.

=begin rakudoc

=TITLE testing

=SUBTITLE more tests

=AUTHOR An author is named

=SUMMARY This page is about Raku

When indexing X<an item|Define an item> another text can be used for the index.

=head1 Heading 1

This text has footnotes and indexed items. Some thing to say N<A footnote> in between words.

=head2 Heading 1.1

=head2 Heading 1.2

=head1 Heading 2

=head2 Heading 2.1

It is possible to index X<hierarchical items|defining,a term>with hierarchical levels.

And then index the X<same place|Same,almost;Place> with different index entries.

But X<|an entry can exist> without the text being marked.

An empty X<> is ignored.
=end rakudoc

$processor.templates(%templates);

dies-ok { $processor.render-block( $=pod[2]) }, 'fails if $=pod not defined';

lives-ok { $processor.render-block( $=pod[0] ); $processor.source-wrap; }, 'main processing methods work, not interested in return values yet';

my $rakudoc-structure;
lives-ok { $rakudoc-structure = $processor.emit-and-renew-processed-state}, 'renew method lives';

for $rakudoc-structure.^methods.grep({
    .name ~~ any(<name title renderedtime subtitle >)
}) { ok $_($rakudoc-structure) ne '', $_.name ~' has content' };

for $rakudoc-structure.^methods.grep({
    .name ~~ any(< raw-metadata raw-toc raw-glossary raw-footnotes >)
}) { ok $_($rakudoc-structure).elems, $_.name ~' has elements' };

is-deeply-relaxed $rakudoc-structure.templates-used.BagHash,
        ("pod"=>1,"heading"=>7,"para"=>9,"format-x"=>4,"format-n"=>1,"raw"=>9,"escaped"=>24,
        :1footnotes, :1glossary, :1source-wrap, :1meta, :1toc).BagHash,
        'used the expected templates';

is $processor.pod-file.renderedtime, '', 'time should be blank after a emit-and-renew-processed-state';

=begin rakudoc  :different<This is different> :difficult<shouldnt be>

=TITLE testing again

=SUBTITLE more tests

Stuff and Nonsense

=end rakudoc

$processor.render-block( $=pod[1] );
$processor.source-wrap;

my $rakudocf2 = $processor.emit-and-renew-processed-state;
$processor.render-block( $=pod[1] );
$processor.source-wrap;
my $rakudocf3 = $processor.emit-and-renew-processed-state;

is-deeply-relaxed $rakudocf2.templates-used.BagHash, $rakudocf3.templates-used.BagHash, 'two rakudoc-files are the same';
done-testing;


