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
    =head1 Item testing

    Some text
    =item first item should be level 1
    =item second item L1
    =item third item L1
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, / '<head>' .+ 'Item testing' .+ '</head>' /, 'head template good';
like $rv.body.Str, / '</head>' .+ '<item-list>'
    .+ '<item>' .+ 'contents: ｢first' .+ 'level: ｢1｣' .+ '</item>'
    .+ 'third' .+ '</item>' .+ '</item-list>'
    /, 'list seems OK';

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

    Some text
    =item first item should be level 1
    =item second item L1
    =item third item L1

    =head after list

    some more text
    =end rakudoc
    QAST

$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, / '<head>' .+ 'Item testing' .+ '</head>' .+ '<item-list>'
    .+ '<item>' .+ 'contents: ｢first' .+ 'level: ｢1｣' .+ '</item>'
    .+ 'third' .+ '</item>' .+ '</item-list>'
    .+ 'head' .+ 'after list' .+ '</head>'
    .+ 'some more text'
    /, 'list before new block seems OK';

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

    Some text
    =item first item should be level 1
    =item second item L1
    =item third item L1

    End the item list with a paragraph.

    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    'Some text' .+?
    '<item-list>' .+?
    '<item>' .+? 'contents: ｢first' .+? 'level: ｢1｣' .+? '</item>' .+?
    '<item>' .+? 'third' .+? '</item>' .+
    '</item-list>' .+
    'End the item list with a paragraph'
    /, 'list ends with paragraph';

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

    Some text on a line
    more text on a line

    Next paragraph

    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    '<para>' .+?
    'contents' .+? 'Some text on a line more text on a line' .+?
    '</para' .+?
    '<para>' .+?
    'contents' .+? 'Next paragraph' .+?
    '</para'
/, 'implied first paragraph';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    paragraph before
    =begin nested
    Some text on a line
    more text on a line

    Next paragraph
    =end nested
    paragraph after
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    '<para>' .+?
    'contents' .+? 'paragraph before' .+?
    '</para' .+?

    '<nested>' .+?
    'contents' .+?
    '<para>' .+? 'Some text on a line more text on a line' .+?
    '</para' .+?
    '<para>' .+?
    'contents' .+? 'Next paragraph' .+?
    '</para>' .+?
    '</nested' .+?

    '<para>' .+?
    'contents' .+? 'paragraph after' .+?
    '</para' .+?
    /, 'nested paragraphs';
# now enumerated
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =numpara First line
    =numnested A note 1
    =numnested A second note
    =numpara Fourth para
    =end rakudoc
    QAST

$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    '<para>' .+?
    'contents' .+? 'First line' .+?
    'numera' .+? '1.' .+?
    '</para' .+?
    '<nested>' .+?
    'contents' .+?
    '<para>' .+? 'A note 1' .+?
    '</para' .+?
    '</nested' .+?
    '<nested>' .+?
    '<para>' .+?
    'contents' .+? 'A second note' .+?
    '</para>' .+?
    '</nested' .+?
    '<para>' .+?
    'contents' .+? 'Fourth para' .+?
    'numeration' .+? '4.' .+?
    '</para'
    /, 'numnested paragraphs';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =numpara First line
    =begin nested
    =numpara A second note
    =numpara A subordinate note
    =end nested
    =numpara Fourth para
    =end rakudoc
    QAST

$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    '<para>' .+?
    'contents' .+? 'First line' .+?
    'numera' .+? '1.' .+?
    '</para' .+?
    '<nested>' .+?
    'contents' .+?
    '<para>' .+? 'A second note' .+?
    'numeration' .+? '2.' .+?
    '</para' .+?
    '<para>' .+?
    'contents' .+? 'A subordinate note' .+?
    'numeration' .+? '3.' .+?
    '</para>' .+?
    '</nested' .+?
    '<para>' .+?
    'contents' .+? 'Fourth para' .+?
    'numeration' .+? '4.' .+?
    '</para'
    /, 'nested num paragraphs';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =numpara Before first
    =begin para
    First line of second

    Second note

    Third note
    =end para
    =numpara Three para
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    '<para>' .+?
    'contents' .+? 'Before first' .+?
    'numera' .+? '1.' .+?
    '</para' .+?
    '<para>' .+?
    'contents' .+?'First line of second' .+?
    'extension' .+? 'Second note' .+? 'Third note' .+?
    '</para' .+?
    '<para>' .+?
    'contents' .+? 'Three para' .+?
    'numeration' .+? '3.' .+?
    '</para'
    /, 'extended paragraph';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =begin item
    =table
    one | two
    ===|===
    three | four
    =end item
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    '<item>' .+? '<table>' .+? '</table' .+? '</item>'
/, 'table inside extended item';
is $rv.warnings.elems, 0, 'No warnings generated';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =begin item
    Some initial text
    =table
    one | two
    ===|===
    three | four
    =end item
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    '<item>' .+?
    'Some initial text' .+?
    '<table>' .+? '</table' .+? '</item>'
    /, 'table inside extended item';
is $rv.warnings.elems, 0, 'No warnings generated';

done-testing;
