use v6.d;
use Test;
use RakuDoc::Render;

plan 11;

my $ast;
my $rv;
my RakuDoc::Processor $rdp .= new(:test);
my $temp-dir = 'xt/test-files';
$ast = Q:scalar:to/QAST/.AST;
    =begin rakudoc
    P<https://raw.githubusercontent.com/rakudo/rakudo/master/LICENSE>
    P<file:$temp-dir/disclaimer.txt>
    =end rakudoc
    QAST

$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body, /
    'Artistic License 2.0' | 'caused LibCurl Exception, response code ｢0｣'
    /,'Got external link or failed with error message if offline';
like $rv.body, /
    'ABSOLUTELY NO WARRANTY IS IMPLIED'
    /,'File contents slurped';

$ast = qq:to/QAST/.AST;
    =begin rakudoc
    P<Cannot connect|https://raw.githubusercontent.com/rakudo/rakudo/master/XXLICENSE>
    P<Bad file|file:$temp-dir/xxdisclaimer.txt>
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body, /
    'Cannot connect'
    /,'Failed external link';
like $rv.body, /
    'Bad file'
    /,'Failed file';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =head1 Head One

    Some text

    P<toc:*>

    More text

    =head1 Head Two

    Again text

    =head2 level two head

    Final stuff

    =end rakudoc
    QAST
$rv = $rdp.render( $ast );
like $rv, /
    'rendered-index: ｢｣' .+
    'rendered-toc: ｢｣'
/, 'auto rendered toc is removed because of place and no index because no X<>';
like $rv, /
    '<rakudoc>' .+
    '<markup-P>' .+
    '<toc>' .+?
    '<toc-item>' .+ 'caption' .+ 'Head One' .+
    '</toc>' .+
    '</markup-P>' .+
    '</rakudoc>'
/, 'got a toc inside an inline placement, inside a rakudoc';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =head1 Head QQQ

    Some text

    =place toc:*

    More text

    =head1 Head WWW

    Again text

    =head2 level two SSSS

    Final stuff

    =end rakudoc
    QAST

$rv = $rdp.render( $ast );
like $rv, /
    'rendered-index: ｢｣' .+
    'rendered-toc: ｢｣'
/, 'no toc because its placed, and no index because no X<>';
like $rv, /
    '<rakudoc>' .+?
    'Some text' .+?
    '<place>' .+
    '<toc>' .+?
    '<toc-item>' .+ 'caption' .+ 'Head QQQ' .+
    '</toc>' .+
    '</place>' .+
    '</rakudoc>'
/, 'got a toc inside a block placement, inside a rakudoc';
$ast = Q:scalar:to/QAST/.AST;
    =begin rakudoc
    =head1 Head One

    Some text

    =place https://raw.githubusercontent.com/rakudo/rakudo/master/XXLICENSE  :toc<head> :caption<Raku's license>  :alt<A multicoloured butterfly>

    More text

    =head1 Head Two

    Again text

    =head2 level two head

    Final stuff

    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
is-deeply $rv.toc.map( *.<caption>.Str ), ('Head One',  'Raku\'s license', 'Head Two', 'level two head'), 'TOC structure contains a place TOC';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =defn lexiphania
    An unfortunate proclivity for
    employing grandiloquisms (for example, words such as "proclivity", "grandiloquism", and indeed "lexiphania").

    =defn glossoligation
    Restraint of the tongue (voluntary or otherwise)

    To treat his chronic P<defn:lexiphania> the doctor prescribed an immediate P<defn:glossoligation>
    or, if that proved ineffective, a complete cephalectomy.

    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    '<defn>' .+? 'target:' .+? 'defn_lexiphania' .+?
    '<defn>' .+? 'target:' .+? 'defn_glossoligation' .+
    'To treat his' .+?
    '<markup-P>' .*?
        'contents:' .+? 'lexiphania' .+
        'defn-expansion:' .+? '<defn>' .+? '｢An unfortunate proclivity' .+? 'indeed "lexiphania").｣' .+
        'defn-target:' .+? 'defn_lexiphania' .+
        'schema:' .+? 'defn' .+
    '</markup-P>' .*?
    '<markup-P>' .*?
        'contents:' .+? 'glossoligation' .+
        'defn-expansion:' .+? '<defn>' .+? '｢Restraint of' .+? 'otherwise)｣' .+
        'defn-target:' .+? 'defn_glossoligation' .+
        'schema:' .+? 'defn' .+
    '</markup-P>'
    /, 'placements to scoped defns work';

$ast = Q:to/QAST/.AST;
    =begin rakudoc

    To treat his chronic P<defn:lexiphania> the doctor prescribed an immediate P<defn:glossoligation>
    or, if that proved ineffective, a complete cephalectomy.

    =defn lexiphania
    An unfortunate proclivity for
    employing grandiloquisms (for example, words such as "proclivity", "grandiloquism", and indeed "lexiphania").

    =defn glossoligation
    Restraint of the tongue (voluntary or otherwise)
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    'To treat his' .+?
    '<markup-P>' .*?
        'contents:' .+? 'lexiphania' .+
        'defn-expansion:' .+? '<defn>' .+? '｢An unfortunate proclivity' .+? 'indeed "lexiphania").｣' .+
        'defn-target:' .+? 'defn_lexiphania' .+
        'schema:' .+? 'defn' .+
    '</markup-P>' .*?
    '<markup-P>' .*?
        'contents:' .+? 'glossoligation' .+
        'defn-expansion:' .+? '<defn>' .+? '｢Restraint of' .+? 'otherwise)｣' .+
        'defn-target:' .+? 'defn_glossoligation' .+
        'schema:' .+? 'defn' .+
    '</markup-P>' .+
    '<defn>' .+? 'target:' .+? 'defn_lexiphania' .+?
    '<defn>' .+? 'target:' .+? 'defn_glossoligation'
    /, 'placements to forward defns work';

done-testing;
