use v6.d;
use Test;

use RakuDoc::Render;

plan 13;

my $ast;
my $rv;
my RakuDoc::Processor $rdp .= new( :test );
$ast = Q:to/QAST/.AST;
    =begin rakudoc

    plain string
    =para    Ordinary       para
    =for comment
    01234567890123456789012345678901234567890

    =begin input
       This  is   a preformatted
       piece of   text in three colummns
       with  no   markup

    =end input
    =end rakudoc
    QAST

$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    'para' .+ 'contents' .+
    '｢plain string｣'
    /, 'String as 1st level of block is a para';
like $rv.body.Str, /
    'para' .+ 'contents' .+
     '｢Ordinary para｣'
    /, 'para works as expected';
my @posn;
my $strt;
my $n = 0;
my $input;
$input = $rv.body.Str.comb( /
    <?after 'input' .+ 'contents: ｢'>
    .+?
    <?before '｣'>
    /)[0];
$input ~~ /
    [
    { $strt = $/.pos }
     \h*: [
        { @posn[$n].push: $/.pos - $strt } \S+ \h*
        ]+:
     \v*
     { $n++ }
    ]+
/;
is $input.comb(/ \V+ \v* /).elems, 3, 'got three input content lines';
is-deeply @posn[0][^3], @posn[1][^3], 'input first two rows start' ;
is-deeply @posn[1][^3], @posn[2][^3], 'input second & third starts match' ;
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =begin code
       This  is   a preformatted
       piece of   text in B<three> colummns
       with  BB   markup but no markup expansion
    =end code
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
@posn = ();
$n = 0;
$input = $rv.body.Str.comb( /
    <?after 'code' .+ 'contents: ｢'>
    .+?
    <?before '｣'>
    /)[0];
$input ~~ /
    [
    { $strt = $/.pos }
     \h*: [
        { @posn[$n].push: $/.pos - $strt } \S+ \h*
        ]+:
     \v*
     { $n++ }
    ]+
/;
is $input.comb(/ \V+ \v* /).elems, 3, 'got three input content lines';
is-deeply @posn[0][^3], @posn[1][^3], 'input first two rows start' ;
is-deeply @posn[1][^3], @posn[2][^3], 'input second & third starts match' ;

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =begin input
       This  is   a preformatted
       piece of   text in B<three> colummns
       with  BB   markup expanded
    =end input
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
@posn = ();
my regex tag { '<' $<tag> = [\S+?] '>' .+? '</' $<tag> '>' \v }
$n = 0;
$input = $rv.body.Str.comb( /
    <?after 'input' .+ 'contents: ｢'>
    .+?
    <?before '｣' \s+ '</input'>
    /)[0];
$input ~~ /
    [
    { $strt = $/.pos }
     \h*: [
        { @posn[$n].push: $/.pos - $strt }
        [
            <tag>
            |
            \S+
        ] \h*
        ]+:
     \v*
     { $n++ }
    ]+
/;
is-deeply @posn[0][^3], @posn[1][^3], 'input first two rows start' ;
is-deeply @posn[1][^3], @posn[2][^3], 'input second & third starts match' ;

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =begin code :allow< B C i Ɵ>
       This  is   a preformatted
       piece of   text in B<three> colummns
       with  BB   markup expanded
    =end code
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
@posn = ();
$n = 0;
$input = $rv.body.Str.comb( /
    <?after 'code' .+ 'contents: ｢'>
    .+?
    <?before '｣' \s+ '</code'>
    /)[0];
$input ~~ /
    [
    { $strt = $/.pos }
     \h*: [
        { @posn[$n].push: $/.pos - $strt }
        [
            <tag>
            |
            \S+
        ] \h*
        ]+:
     \v*
     { $n++ }
    ]+
/;
is-deeply @posn[0][^3], @posn[1][^3], 'input first two rows start' ;
is-deeply @posn[1][^3], @posn[2][^3], 'input second & third starts match' ;

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =begin output
       =for head :caption<Hi there> :anoption
       Has a header

       =begin para :optionality
       This is an ordinary C<paragraph>
       in extended form
       =end para
       This  is   a preformatted
       piece of   text in B<three> colummns
       with  BB   markup expanded
    =end output
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body, /
    '<output' .+ 'contents' .+ '｢' .+?
    '=for head' .+ 'Has a header' .+
    'markup-C' .+ 'paragraph' .+ '</markup-C>' .+
    'markup exp' .+ '｣' .+
    '</output>'
/, 'output stringifies blocks but applies markup';
done-testing;

