use v6.d;

use Test;

use Pod::ContentsKebab;

plan 50;

=head1 First heading
=NAME some app

=TITLE U<An app> - C<does> B<some> great stuff and U<prints> the results

=begin DESCRIPTION
Description of the app.

It's great.
=end DESCRIPTION

=head2 Second heading

=begin table
hcell00 | hcell01
=================
cell11  | cell12
cell21  | cell22
=end table

=begin pod
=head1 Some heading

Text in a paragraph with C<formatters>, a B<bold> and U<underline>.
more U<text> and C<formatters>.

=item an C<item>
=item B<another> one

=begin table
cell11 | cell12
cell21 | cell22
=end table

=item1 top-level item
=begin item2
sub-level item
with paragraph

the paragraph

     my $code;
     say 'code';

     return 'some string';
=end item2

=defn something
definition

=AUTHOR Siavash

=end pod

is-deeply $=pod.&get-first-pod('NAME'), $=pod[1], 'NAME was found correctly';

is-deeply $=pod.first({.?name ~~ 'pod'}).contents.first(Pod::Heading),
          $=pod.&get-first-pod('pod').&get-first-pod(Pod::Heading),
          'head1 was found correctly';

is-deeply $=pod.&get-first-pod(Pod::Item, :recurse),
          $=pod.first({.?name ~~ 'pod'}).contents.first(Pod::Item),
          'First item was found correctly using :recurse';

is-deeply $=pod.&get-first-pod(Pod::Block::Code, :recurse),
          $=pod.first({.?name ~~ 'pod'}).contents.grep(Pod::Item)[3].contents.first(Pod::Block::Code),
          'code was found correctly using :recurse';

is-deeply $=pod.&get-first-pod('pod').&get-pod-contents-of(Pod::Heading), ('Some heading',),
          'head1 content returned correctly';

is-deeply $=pod.&get-first-pod('pod').&join-pod-contents-of(Pod::Heading), 'Some heading',
          'head1 string returned correctly';

is-deeply $=pod.&get-pod-contents-of(Pod::Item, :recurse), ('an item',),
          'First item content returned correctly using :recurse';

is-deeply $=pod.&join-pod-contents-of(Pod::Item, :recurse), 'an item',
          'First item string returned correctly using :recurse';

is-deeply $=pod.&join-pod-contents-of('AUTHOR', :recurse), 'Siavash',
          'AUTHOR string returned correctly using :recurse';

is-deeply $=pod.first({.?name ~~ 'NAME'}).&get-pod-contents, ('some app',), 'NAME - Simple pod block content';

is-deeply $=pod.&get-pod-contents-of('NAME'), $=pod.first({.?name ~~ 'NAME'}).&get-pod-contents,
          'NAME contents returned correctly';

is-deeply $=pod.&join-pod-contents-of('NAME'), 'some app',
          'NAME - join-pod-contents-of: Simple pod block content';

is-deeply $=pod.first({.?name ~~ 'TITLE'}).&get-pod-contents,
          ('An app - does some great stuff and prints the results',),
          'TITLE - get-pod-contents: Content with inline formatters';

is-deeply $=pod.&get-pod-contents-of('TITLE'),
          ('An app - does some great stuff and prints the results',),
          'TITLE - get-pod-contents-of: Content with inline formatters';

is-deeply $=pod.&join-pod-contents-of('TITLE'),
          'An app - does some great stuff and prints the results',
          'TITLE - join-pod-contents-of: Content with inline formatters';

is-deeply $=pod.first({.?name ~~ 'TITLE'}).&get-pod-contents(:!inline-formatters),
          ('An app', ' - ', 'does', ' ', 'some', ' great stuff and ', 'prints', ' the results'),
          'TITLE - get-pod-contents: Content without inline formatters';

is-deeply $=pod.&get-pod-contents-of('TITLE', :!inline-formatters),
          ('An app', ' - ', 'does', ' ', 'some', ' great stuff and ', 'prints', ' the results'),
          'TITLE - get-pod-contents-of: Content without inline formatters';

is-deeply $=pod.&join-pod-contents-of('TITLE', :!inline-formatters),
          "An app\n\n - \n\ndoes\n\n \n\nsome\n\n great stuff and \n\nprints\n\n the results",
          'TITLE - join-pod-contents-of: Content without inline formatters';

is-deeply $=pod.first({.?name ~~ 'DESCRIPTION'}).&get-pod-contents, ('Description of the app.', 'It\'s great.'),
          'DESCRIPTION - get-pod-contents: Content with blank line';

is-deeply $=pod.&get-pod-contents-of('DESCRIPTION'), ('Description of the app.', 'It\'s great.'),
          'DESCRIPTION - get-pod-contents-of: Content with blank line';

is-deeply $=pod.&join-pod-contents-of('DESCRIPTION'), "Description of the app.\n\nIt's great.",
          'DESCRIPTION - join-pod-contents-of: Content with blank line';

is $=pod.&get-pods(Pod::Heading).elems, 2, 'Number of all headings of this Pod is correct';

is-deeply $=pod.&get-pods(Pod::Heading).&get-pod-contents,
          ('First heading', 'Second heading'),
          'Get all headings of this Pod';

is $=pod.&get-pods(Pod::Block::Table, :recurse).elems, 2, 'Number of all tables using :recurse is correct';

is $=pod.&get-pods(Pod::Item, :recurse).elems, 4, 'Number of all items using :recurse is correct';

is $=pod.&get-first-pod('pod').&get-pods(Pod::Item).elems, 4, 'Number of all items of Pod is correct';

is $=pod.&get-pods('pod')[0].&get-pods(Pod::Item, :recurse).elems, 4,
   'Number of all items of Pod using :recurse is correct';

is-deeply $=pod.&get-pods('pod')[0].&get-pods('AUTHOR', :recurse).&join-pod-contents, 'Siavash',
          'AUTHOR was returned correctly using get-pods :recurse';

is-deeply $=pod.&get-pods('pod')[0].&get-pods('AUTHOR').&join-pod-contents, 'Siavash',
          'AUTHOR was returned correctly using get-pods';

is-deeply $=pod.&get-pods(Pod::Item, :recurse)[0,1].&get-pod-contents,
          ('an item', 'another one'),
          'Get all items';

is-deeply $=pod.&get-pods(Pod::Item, :recurse)[0,1].&join-pod-contents,
          "an item\n\nanother one",
          'Join all items';

is-deeply $=pod.&get-pods(Pod::Item, :recurse)[0,1].&join-pod-contents(:adjacent-items),
          "an item\nanother one",
          'Join all items using :adjacent-items';

is-deeply $=pod.&get-first-pod('pod').contents.grep(Pod::Item)[0,1].&get-pod-contents(:adjacent-items),
          (('an item', 'another one'),),
          'Get a list of items with :adjacent-items';

is-deeply $=pod.&get-first-pod('pod').contents[0,2,3].&get-pod-contents(:adjacent-items),
          ('Some heading', ('an item', 'another one')),
          'Get a list of items with :adjacent-items';

is-deeply $=pod.&get-first-pod('pod').contents[0,2,3].&join-pod-contents(:adjacent-items),
          "Some heading\n\nan item\nanother one",
          'Get a string of items with :adjacent-items';

is-deeply $=pod.&get-pod-contents-of(Pod::Block::Table, :include-title),
          (<hcell00 hcell01>, <cell11 cell12>, <cell21 cell22>),
          'Table - get-pod-contents-of: table with title';

is-deeply $=pod.&join-pod-contents-of(Pod::Block::Table, :include-title),
          "hcell00 hcell01\ncell11 cell12\ncell21 cell22",
          'Table - join-pod-contents-of: table with title';

my @contents-without-indentation is List:D = (
    'Some heading',
    'Text in a paragraph with formatters, a bold and underline. more text and formatters.',
    'an item', 'another one', (<cell11 cell12>, <cell21 cell22>), 'top-level item',
    'sub-level item with paragraph', 'the paragraph',
    "my \$code;\nsay 'code';\n\nreturn 'some string';",
    "definition",
    "Siavash",
);

is-deeply $=pod.first({.?name ~~ 'pod'}).&get-pod-contents, @contents-without-indentation,
          'pod - get-pod-contents: Content with pod block, paragraphs and formatters without indentation';

is-deeply $=pod.&get-pod-contents-of('pod'), @contents-without-indentation,
          'pod - get-pod-contents-of: Content with pod block, paragraphs and formatters without indentation';

my @contents-with-indentation is List:D = (
    'Some heading',
    'Text in a paragraph with formatters, a bold and underline. more text and formatters.',
    'an item', 'another one', (<cell11 cell12>, <cell21 cell22>), 'top-level item',
    '    sub-level item with paragraph', '    the paragraph',
    "        my \$code;\n        say 'code';\n        \n        return 'some string';",
    "    definition",
    "Siavash",
);

is-deeply $=pod.first({.?name ~~ 'pod'}).&get-pod-contents(:indent-content), @contents-with-indentation,
          'pod - get-pod-contents: Content with pod block and paragraphs and formatters with indentation';

is-deeply $=pod.&get-pod-contents-of('pod', :indent-content), @contents-with-indentation,
          'pod - get-pod-contents-of: Content with pod block and paragraphs and formatters with indentation';

my @contents-with-indentation-title is List:D = (
    'Some heading',
    'Text in a paragraph with formatters, a bold and underline. more text and formatters.',
    'an item', 'another one', (<cell11 cell12>, <cell21 cell22>), 'top-level item',
    '    sub-level item with paragraph', '    the paragraph',
    "        my \$code;\n        say 'code';\n        \n        return 'some string';",
    ('something', '    definition'),
    "Siavash",
);

is-deeply $=pod.first({.?name ~~ 'pod'}).&get-pod-contents(:indent-content, :include-title),
          @contents-with-indentation-title,
          'pod - get-pod-contents: Content with pod block, paragraphs, formatters with indentation and title';

is-deeply $=pod.&get-pod-contents-of('pod', :indent-content, :include-title),
          @contents-with-indentation-title,
          'pod - get-pod-contents-of: Content with pod block, paragraphs, formatters with indentation and title';
my @contents-indented-with-custom-level is List:D = (
    'Some heading',
    'Text in a paragraph with formatters, a bold and underline. more text and formatters.',
    'an item', 'another one', (<cell11 cell12>, <cell21 cell22>), 'top-level item',
    '  sub-level item with paragraph', '  the paragraph',
    "    my \$code;\n    say 'code';\n    \n    return 'some string';",
    "  definition",
    "Siavash",
);

is-deeply $=pod.first({.?name ~~ 'pod'}).&get-pod-contents(:indent-content, :indent-level(2)),
           @contents-indented-with-custom-level,
          'pod - get-pod-contents: Content with pod block and paragraphs and formatters with indentation 2';

is-deeply $=pod.&get-pod-contents-of('pod', :indent-content, :indent-level(2)),
          @contents-indented-with-custom-level,
          'pod - get-pod-contents-of: Content with pod block and paragraphs and formatters with indentation 2';

my $content-without-indentation =
｢Some heading

Text in a paragraph with formatters, a bold and underline. more text and formatters.

an item

another one

cell11 cell12
cell21 cell22

top-level item

sub-level item with paragraph

the paragraph

my $code;
say 'code';

return 'some string';

definition

Siavash｣;

is-deeply $=pod.&join-pod-contents-of('pod'), $content-without-indentation,
          'pod - join-pod-contents-of: Content with pod block, paragraphs and formatters without indentation';

my $content-without-indentation-joined-with-one-newline =
｢Some heading
Text in a paragraph with formatters, a bold and underline. more text and formatters.
an item
another one
cell11 cell12
cell21 cell22
top-level item
sub-level item with paragraph
the paragraph
my $code;
say 'code';

return 'some string';
definition
Siavash｣;

is-deeply $=pod.&join-pod-contents-of('pod', "\n"), $content-without-indentation-joined-with-one-newline,
          'pod - join-pod-contents-of: Content with pod block, paragraphs, formatters. Joined with a newline, without indentation';

my $content-with-indentation =
｢Some heading

Text in a paragraph with formatters, a bold and underline. more text and formatters.

an item

another one

cell11 cell12
cell21 cell22

top-level item

    sub-level item with paragraph

    the paragraph

        my $code;
        say 'code';
        
        return 'some string';

    definition

Siavash｣;

is-deeply $=pod.&get-first-pod('pod').&join-pod-contents(:indent-content), $content-with-indentation,
          'pod - join-pod-contents: Content with pod block and paragraphs and formatters with indentation';

is-deeply $=pod.&join-pod-contents-of('pod', :indent-content), $content-with-indentation,
          'pod - join-pod-contents-of: Content with pod block, paragraphs and formatters with indentation';

my $content-with-indentation-title =
｢Some heading

Text in a paragraph with formatters, a bold and underline. more text and formatters.

an item

another one

cell11 cell12
cell21 cell22

top-level item

    sub-level item with paragraph

    the paragraph

        my $code;
        say 'code';
        
        return 'some string';

something
    definition

Siavash｣;

is-deeply $=pod.&join-pod-contents-of('pod', :indent-content, :include-title), $content-with-indentation-title,
          'pod - join-pod-contents-of: Content with pod block, paragraphs and formatters with title';
