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

plan 21;

my $ast;
my $rv;
my RakuDoc::Processor $rdp .= new(:test);
# only test B as H I J K R T U O are the same but different key words
# C V S tested later
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thingB<to be said>inside letters.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
like $rv.body, /
'Some thing'
'<markup-B>' .*? 'contents:' .*? '｢to be said｣' .*? '</markup-B>' .*?
'inside letters.'
/, 'B-markup works with text with spaces inside text';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thing to B<say> in between words.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
like $rv.body, /
'Some thing to '
'<markup-B>' .*? 'contents:' .*? '｢say｣' .+? '</markup-B>' .*?
' in between words.'
/, 'B-markup works with space';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thingZ<to be said>inside letters.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
like $rv.body, /
'Some thinginside letters.'
/, 'Z-markup works with text with spaces inside text';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thing to Z<say> in between words.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
like $rv.body, /
'Some thing to  in between words.'
/, 'Z-markup works with space';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Raku makes considerable use of the E<0xAB;0xBB> characters.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
like $rv.body, /
'Raku makes' .+
'<markup-E>' .*? 'contents: ｢«»｣' .*? '</markup-E>' .*? 'characters'
/, 'E-markup ';

# test verbose functionality for markup
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thingB<to be said>inside letters.
    And also I<in italics>.
    =end rakudoc
    QAST

$rdp.verbose('markup-I');
my $out = output-from {
    $rv = $rdp.render($ast, :pre-finalised);
};
like $out, / '<markup-I>' .*? 'in italics' .+ '</markup-I>'
/, 'the template result for I is output';
unlike $out, /
'<markup-B>' .*? 'contents:' .*? '｢to be said｣' .*? '</markup-B>' .*?
/, 'template result for B is not output';
like $rv.body, /
'Some thing' .*?
'<markup-B>' .*? 'contents:' .*? '｢to be said｣' .*? '</markup-B>' .*?
'inside letters.' \s*
'And also' .+? '<markup-I>' .*? 'in italics'
/, 'B & I works with text with spaces inside text';

# test undefined ASCII
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thing to G<say> in between words.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
like $rv.body, /
'Some thing to <markup-bad' .+
'G<say>' .+
' in between words.'
/, 'G code without change';
like $rv.warnings[0], / 'is reserved' /, 'got reserved warning';

# test possible but not defined

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thing to Ɵ<say> in between words.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
like $rv.body, /
'Some thing to <markup-bad' .+
'Ɵ<say>' .+
' in between words.'
/, 'Ɵ without change';
like $rv.warnings[0], / 'could be a custom code' /, 'got possible warning';

# test impossible

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thing to ɤ<say> in between words.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
like $rv.body, /
'Some thing to '
'ɤ<say>'
' in between words.'
/, 'ɤ code without change';

## test possible and defined
$rdp.add-template: (markup-Ɵ => sub (%prm, $) {
    express-params(%prm, 'markup-Ɵ')
});

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thing to Ɵ<say> in between words.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
like $rv.body, /
'Some thing to '
'<markup-Ɵ>' .*? 'contents:' .*? '｢say｣' .+? '</markup-Ɵ>' .*?
' in between words.'
/, 'Ɵ-markup works with space';
is $rv.warnings.elems, 0, 'no warning';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thing
    =comment In line comment

    between words.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
unlike $rv.body, /
'In line comment'
/, 'in line comment excluded';
like $rv.body, /
'<para' .+? 'Some thing' .+? 'between words.'
/, 'got context with comment';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thing
    =begin comment
    Multi line comment
    More comm
    =end comment
    between words.
    =end rakudoc
    QAST

$rv = $rdp.render($ast, :pre-finalised);
unlike $rv.body, /
'Multi line comment'
/, 'comment excluded';
like $rv.body, /
'<para' .+? 'Some thing' .+? 'between words.'
/, 'got context with comment';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    Some thing
    =begin comment
    =end comment
    between words.
    =end rakudoc
    QAST

lives-ok { $rv = $rdp.render($ast, :pre-finalised) }, 'empty comment is fine';
like $rv.body, /
'<para' .+? 'Some thing' .+? 'between words.'
/, 'got context with comment';
done-testing;
