### ----------------------------------------------------
### -- Raku::Pod::Render
### -- Licenses: Artistic-2.0
### -- Authors: Richard Hainsworth
### -- File: xt/30-pod-meta-data.rakutest
### ----------------------------------------------------
use Test;
use Test::Deeply::Relaxed;
use ProcessedPod;

# A helper class for RakuClosureTemplates
multi sub gen-closure-template (Str $tag) is export {
    my $start = '<' ~ $tag ~ '>';
    my $end = '</' ~ $tag ~ '>';
    return sub (%prm, %tml? --> Str) {
        $start ~ (%prm<contents> // '') ~ $end;
    }
}
plan 9;

my $processor = ProcessedPod.new;
my $pv = 0;
my $rv;

use RenderPod::Templating;
my @templates = SetupTemplates.new.required;

my %templates  = @templates Z=> @templates.map( { gen-closure-template( $_ ) });
%templates<escaped> = sub ($s) { $s };
%templates<meta> = sub ( %prm, %tmp ) {
    "<meta>\n"
    ~ %prm<meta>
        .grep({ $_<name> ~~ any(<VERSION DESCRIPTION AUTHOR SUMMARY>) } )
        .map( { $_<name>.tclc ~ '=' ~ $_<value> ~ "\n"} )
    ~ '</meta>'
};
%templates<source-wrap> = sub (%prm, %tml ) {
    "<file><file-conf>\n{ %prm<config>.sort.fmt("%s=>%s") }\n</file-conf>\n<body>{ %prm<body> }</body>{ %prm<metadata> }</file>"
};
%templates<heading> = sub (%prm, %tml) {
    "<config>" ~ %prm<text> ~ %prm<config>.sort.fmt("%s=>%s") ~ '</config>'
};
%templates<AUTHOR> = sub (%prm, %tml) {
    %prm<raw-contents>
};
%templates<SUMMARY> = sub (%prm, %tml) {
    %prm<raw-contents>
};
$processor.templates(%templates);

=begin pod  :kind("Language") :subkind("Language") :category("fundamental") :content-columns

=TITLE testing

=SUBTITLE more tests

Stuff

=head1 first

=end pod

$rv = $processor.render-block( $=pod[$pv++] );
like $rv, /
    'category=>fundamental'
    .+ 'content-columns=>True'
    .+ 'kind=>Language'
    .+ 'subkind=>Language'
/, 'got configs';

is-deeply-relaxed $processor.pod-file.pod-config-data,
        %( :kind<Language>, :subkind<Language>, :category<fundamental>, :content-columns ),
        'got pod config data';

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

=TITLE testing again

=SUBTITLE more tests

Stuff and Nonsense

=config head1 :temporary

=head1 second

=end pod
$rv = $processor.render-block( $=pod[$pv]) ;
like $rv, /
    'difficult=>shouldnt be'
/, 'second pod change the lexical configs';
like $rv, /
    "head1=>temporary\tTrue"
    /, 'so does config directive';

isnt-deeply-relaxed $processor.pod-file.pod-config-data, %( :different<This is different>, :difficult<shouldnt be> ),
        'only first pod config data included in file configs';

$processor.emit-and-renew-processed-state;

$rv = $processor.render-block( $=pod[$pv]) ;
is-deeply-relaxed
        $processor.pod-file.pod-config-data,
        %( :different("This is different"), :difficult("shouldnt be") ),
        'second block config data now file config data because of reset';

=begin pod :file-content :something<else>
=AUTHOR A.N. Writer
=SUMMARY Some summarised remarks

Stuff
=config head1 :this<is one> :that<is another>
=head1 third

=end pod

$processor.emit-and-renew-processed-state;
$processor.pod-file.path = 'test-file';
$rv = $processor.render-tree( $=pod[++$pv] );
like $rv, /
    "head1=>that\tis another"
    \s* "this\tis one"
/, 'got multiple attributes in config directive';
$rv = $processor.source-wrap;
like $rv,
        /
        '<file>'
        .+
        '<meta>'
        [ \s* 'Author=A.N. Writer' | \s* 'Summary=Some summarised remarks' ] **2
        \s* '</meta></file>'
        /, 'Got meta data structure';
like $rv, /
    '<file-conf>'
    .+ 'file-content' .+ 'True'
    .+ 'path' .+ 'test-file'
    .+ 'something' .+ 'else'
    .+ '</file-conf>'
/, 'got file config';

done-testing;


