### ----------------------------------------------------
### -- Raku::Pod::Render
### -- Licenses: Artistic-2.0
### -- Authors: Richard Hainsworth
### -- File: xt/902-md/05-basic.rakutest
### ----------------------------------------------------
#!/usr/bin/env perl6
use v6.*;
use Test;
use File::Directory::Tree;
use Pod::To::MarkDown2;
my $rv;
my $processor;
my $pc = 0;
my $def-dir = 'xt/md-test-def'.IO;
my $tar-dir = 'xt/md-tar-dir'.IO;

rmtree ~$def-dir if $def-dir ~~ :e;
rmtree ~$tar-dir if $tar-dir ~~ :e;
mktree $tar-dir;

diag "\ntesting with distributed html-templates-rakuclosure\n";

my $top-dir = $*CWD;
plan 9;

=begin pod

=TITLE Insignificant Pod

Some pod

=end pod

throws-like { indir $tar-dir, Pod::To::MarkDown2.new(:$def-dir) },
        X::ProcessedPod::NoRenderPodDirectory,
        'No default directory';
mktree ~$def-dir;
throws-like { indir $tar-dir, Pod::To::MarkDown2.new(:$def-dir) },
        X::ProcessedPod::BadDefault,
        'No assets in default directory';
'resources/md-templates-mustache.raku'.IO.copy("$def-dir/md-templates-mustache.raku");
chdir ~$tar-dir;
lives-ok { $rv = Pod::To::MarkDown2.render($=pod[$pc], :$def-dir) }, 'captures Pod into MarkDown';

like $rv, / 'Insignificant Pod' .*? 'Some pod' .*? 'Rendered from' /, 'got consistent MarkDown';

lives-ok { $processor = Pod::To::MarkDown2.new(:$def-dir) }, 'returns new object';
like $processor.^name, /'Pod::To::MarkDown'/, 'correct return type';

$processor.process-pod($=pod[$pc++]);
$rv = $processor.source-wrap;
like $rv,
        / 'Insignificant Pod' .*? 'Some pod' .*? 'Rendered from' /, 'works like render';

my $fn = 'test-output';
$processor.file-wrap(:filename($fn), :ext<md>);

ok "$fn\.md".IO ~~ :f, 'file with md made';
"$fn\.md".IO.unlink if "$fn\.md".IO ~~ :e;

=begin pod
Another fascinating mess
=end pod

$rv = $processor.render-block($=pod[$pc]);

like $rv,
        / 'Another fascinating mess' /, 'latest snippit only';
diag "\nprepare for remaining HTML tests using fixed test raku closure templates\n";
chdir $top-dir;
"resources/test_things/md-mustache.raku".IO.copy("$tar-dir/md-templates-mustache.raku");

done-testing;


