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

plan 10;

my $ast;
my $rv;
my RakuDoc::Processor $rdp .= new( :test );
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =defn Happy
    When you're not blue.

    =defn Blue
    When you're not happy.
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    '<defn-list>' .+
    '<defn>' .+
    'contents:' .+ "When you're not blue" .+
    'target:' .+ 'defn_Happy' .*
    'term:' .+ 'Happy' .*
    '</defn>' .*
    '<defn>' .+
    'contents:' .+ "When you're not happy" .+
    'target:' .+ 'defn_Blue' .+
    'term:' .+ 'Blue' .+
    '</defn>' .+
    '</defn-list>'
    /, 'Basic definitions';

$ast = Q:to/QAST/.AST;
    =begin rakudoc
    =defn Happy
    When you're B<not> blue.

    =defn Blue
    When B<you're> not happy.
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );
like $rv.body.Str, /
    '<defn-list>' .*
    '<defn>' .*
    'contents:' .* "When you're " .* '<markup-B>' .* 'not' .* '</markup-B>' .* 'blue' .*
    'target:' .* 'defn_Happy' .*
    'term:' .* 'Happy' .*
    '</defn>' .*
    '<defn>' .*
    'contents:' .* 'When ' .* '<markup-B>' .* "you're" .* '</markup-B>' .* ' not happy' .*
    'target:' .* 'defn_Blue' .*
    'term:' .* 'Blue' .*
    '</defn>' .*
    '</defn-list>'
    /, 'Expositions have embedded markup';
$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 L<defn:lexiphania> the doctor prescribed an immediate L<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' .+
    '<markup-L>' .*?
        'extra:' .+? '｢An unfortunate proclivity' .+? 'indeed "lexiphania").｣' .+
        'link-label:' .+? 'lexiphania' .+
        'target:' .+? 'defn_lexiphania' .+
        'type:' .+? 'defn' .+
    '<markup-L>' .*?
        'extra:' .+? '｢Restraint of' .+? 'otherwise)｣' .+
        'link-label:' .+? 'glossoligation' .+
        'target:' .+? 'defn_glossoligation' .+
        'type:' .+? 'defn'
/, 'links to defns work';
$ast = Q:to/QAST/.AST;
    =begin rakudoc

    To treat his chronic L<defn:lexiphania> the doctor prescribed an immediate L<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-L>' .*?
        'extra:' .+? '｢An unfortunate proclivity' .+? 'indeed "lexiphania").｣' .+
        'link-label:' .+? 'lexiphania' .+
        'target:' .+? 'defn_lexiphania' .+
        'type:' .+? 'defn' .+
    '</markup-L>' .*?
    '<markup-L>' .*?
        'extra:' .+? '｢Restraint of' .+? 'otherwise)｣' .+
        'link-label:' .+? 'glossoligation' .+
        'target:' .+? 'defn_glossoligation' .+
        'type:' .+? 'defn' .+
    '</markup-L>' .+
    '<defn>' .+? 'target:' .+? 'defn_lexiphania' .+?
    '<defn>' .+? 'target:' .+? 'defn_glossoligation' .+
/, 'links to forward defns work';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    There ensued a terrible moment of D<coyotus interruptus>: a brief
    suspension of the effects of gravity.

    As the canyon edge loomed, a memory of L<defn:coyotus interruptus> passed him by.
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );

like $rv.body.Str, /
    'There ensued' .+?
    'of <markup-D>' .+?
    'contents' .+? 'coyotus interruptus' .+?
    '</markup-D>'
/, 'inline definition rendered';
like $rv.body.Str, /
    '<markup-L>' .+?
    'extra' .+? 'There ensued' .+?
    'link-label' .+? '｢coyotus interruptus｣' .+?
    'target' .+? '｢' <xdigit>+ '｣' .+
    '</markup-L>'
/, 'link picks up the inline-definition';
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    There ensued a terrible moment of D<coyotus interruptus|crazy coyote;speed runner revenge>: a brief
    suspension of the effects of gravity.

    As the canyon edge loomed, a memory of L<defn:crazy coyote> passed him by.

    He grinned at L<defn:speed runner revenge>
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );

like $rv.body.Str, /
    'There ensued' .+?
    'of <markup-D>' .+?
    'contents' .+? 'coyotus interruptus' .+?
    '</markup-D>'
/, 'inline definition rendered';
like $rv.body.Str, /
    '<markup-L>' .+?
    'extra' .+? 'There ensued' .+?
    'link-label' .+? '｢crazy coyote｣' .+?
    'target' .+? '｢' <xdigit>+ '｣' .+
    '</markup-L>' .+?
    '<markup-L>' .+?
    'extra' .+? 'There ensued' .+?
    'link-label' .+? '｢speed runner revenge｣' .+?
    'target' .+? '｢' <xdigit>+ '｣' .+
    '</markup-L>'
/, 'links picks out synonyms';
#
$ast = Q:to/QAST/.AST;
    =begin rakudoc
    There ensued a terrible moment of D<coyotus interruptus>: a brief
    suspension of the effects of gravity. For Speed Runner, it was an
    example of D<hubris>.

    As the canyon edge loomed, a memory of L<defn:coyotus interruptus> passed him by.

    Later his wife asked about L<defn:hubris>.
    =end rakudoc
    QAST
$rv = $rdp.render( $ast, :pre-finalised );

like $rv.body.Str, /
    'There ensued' .+?
    'of <markup-D>' .+?
    'contents' .+? 'coyotus interruptus' .+?
    '</markup-D>' .+
    '<markup-D>' .+?
    'contents' .+? 'hubris' .+?
    '</markup-D>'
/, 'inline definition rendered';
like $rv.body.Str, /
    '<markup-L>' .+?
    'extra' .+? 'There ensued' .+?
    'link-label' .+? '｢coyotus interruptus｣' .+?
    'target' .+? '｢' <xdigit>+ '｣' .+
    '</markup-L>' .+?
    '<markup-L>' .+?
    'extra' .+? 'There ensued' .+?
    'link-label' .+? '｢hubris｣' .+?
    'target' .+? '｢' <xdigit>+ '｣' .+
    '</markup-L>'
/, 'link picks one of synonyms';
done-testing;
