### ----------------------------------------------------
### -- Pod::To::Man
### -- Licenses: Artistic-2.0
### -- Authors: Mike Clarke, Vadim Belman, Samuel Young
### -- File: t/01-basic.rakutest
### ----------------------------------------------------
use Test;
use Pod::To::Man;

plan 35;

=begin pod

=begin comment
Comments
shouldn't
render
=end comment

=head1 NAME

Test - Some test pod

=head1 SYNOPSIS

Example:

    ... code is here
=end pod

=begin pod

Entering text.

=item1 Level 1.1
=item2 Level 2.1
=item2 Level 2.2

=item1 Level 1.2

=begin item1
Big item

with few paragraphs

=begin item
A nested item

with paragraphs

=item A
=item B
=end item
=item Nested single line

One more para
=end item1

=begin defn
Term
definition

text
=end defn

=table
    Animal | Legs |    Eats
    =======================
    Zebra  +   4  | Cookies
    Human  +   2  |   Pizza
    Shark  +   0  |    Fish

=table
    \+term          | prefix
    term1 \+ term2  | infix
    term\+\+        | postfix

=end pod

=begin pod

=NAME Test

=HEAD1 SYNOPSIS

  yada yada...

=end pod

#| A class that exists to test class pod declarators.
my class TestClass {

    #| Foo of TestClass
    has $.foo;

    #| Concatenates a and b
    method foobar(
        $a, #= Argument a
        $b, #= Argument b
        --> Str
    ) {
        $a ~ $b;
    }

}

#| A sub that exists to test sub pod declarators.
my sub test-sub(
    $c, #= Argument c
    $d, #= Argument d
    --> Int
) {
    $c + $d;
}

#| An enum that exists to test enum pod declarators.
enum TestEnum <foo bar baz>;

#| A module that exists to test module pod declarators.
my module TestModule { }

#| A subset that exists to test subset pod declarators.
subset test-subset of Str;

#| A package that exists to test package pod declarators.
my package TestPackage { }

%*ENV = ();

my $roff;

lives-ok {
    $roff = Pod::To::Man.render($=pod[0]);
}, "first pod renders ok";

ok $roff.contains('.TH Test'), "has the header";
ok $roff.contains("\n.SH NAME"), "NAME heading";
like $roff, /Test .* \.SH \s SYNOPSIS .* Example\: .*/, "no text lost";
like $roff, /\.EX .* code \s is \s here/, "code is rendered";
unlike $roff, /Comments .* shouldn\'t .* render/, "comments are not rendered";

lives-ok {
    $roff = Pod::To::Man.render($=pod[1]);
}, "second pod renders ok";

like $roff, /^^ 'Entering text\\&.' .* 'Level 1\\&.1' .* 'Level 2\\&.1' .* 'Big item' .* 'A nested item' .* 'definition'/, "all expected text is in place";
like $roff, /'Level 1\\&.1'\n .* 'Level 2\\&.1' .* /, "level items are rendered with indentation";
like $roff, /'Big item' \n \.IP \n 'with few paragraphs' \n \.IP \n \.RS .* \.IP .* 'A nested item' \n \.IP \n 'with paragraphs'/, 'nested multiparagraph items';

ok $roff.contains(q:to/HERE/), "table was rendered";
Animal | Legs | Eats   
=======================
Zebra  | 4    | Cookies
Human  | 2    | Pizza  
Shark  | 0    | Fish   
HERE

ok $roff.contains(q:to/HERE/), "table with no header was rendered";
+term         | prefix 
term1 + term2 | infix  
term++        | postfix
HERE

lives-ok {
    $roff = Pod::To::Man.pod2man(
        $=pod[0],
        :program('program'),
        :section('1337'),
        :date('2015-11-24'.Date),
        :version('13.37'),
        :center('SomeTestDocumentation')
    );
}, "pod renders with custom options ok";

ok $roff.contains('.TH program'), "has the header with custom program";
ok $roff.contains('program 1337'), "has the header with custom section";
ok $roff.contains('1337 "2015-11-24"'), "has the header with custom date";
ok $roff.contains('"2015-11-24" "13.37"'), "has the header with custom version";
ok $roff.contains('"13.37" "SomeTestDocumentation"'), "has the header with custom center";

lives-ok {
    $roff = Pod::To::Man.render($=pod[2])
}, "third pod renders ok";

ok $roff.contains('.TH Test'), "has the header specified by =NAME";

%*ENV = (
    RAKUDOC2MAN_PROGRAM => 'program',
    RAKUDOC2MAN_SECTION => '1337',
    RAKUDOC2MAN_DATE    => '2015-11-24',
    RAKUDOC2MAN_VERSION => '13.37',
    RAKUDOC2MAN_CENTER  => 'SomeTestDocumentation',
    RAKUDOC2MAN_URLS    => '0',
);

lives-ok {
    $roff = Pod::To::Man.render($=pod[0]);
}, "pod renders with environment variables ok";

ok $roff.contains('.TH program'), "has the header with custom program";
ok $roff.contains('program 1337'), "has the header with custom section";
ok $roff.contains('1337 "2015-11-24"'), "has the header with custom date";
ok $roff.contains('"2015-11-24" "13.37"'), "has the header with custom version";
ok $roff.contains('"13.37" "SomeTestDocumentation"'), "has the header with custom center";

lives-ok {
    $roff = Pod::To::Man.pod2man($=pod[3..*])
}, "declarator pod renders ok";

ok $roff.contains(q:to/HERE/), "has the class documentation";
.SS class TestClass

.PP
A class that exists to test class pod declarators\&.
HERE

ok $roff.contains(qq:to/HERE/), "has the attribute documentation";
.SS attribute \$!foo

.RS 4m
.EX
has Mu \$!foo

.EE
.RE
.PP
Foo of TestClass
HERE

ok $roff.contains(qq:to/HERE/), "has the method documentation";
.SS method foobar

.RS 4m
.EX
method foobar (
\t\$a, # Argument a
\t\$b, # Argument b
\t\\-\\-> Str
)

.EE
.RE
.PP
Concatenates a and b
HERE

ok $roff.contains(qq:to/HERE/), "has the sub documentation";
.SS sub test-sub

.RS 4m
.EX
sub test\\-sub (
\t\$c, # Argument c
\t\$d, # Argument d
\t\\-\\-> Int
)

.EE
.RE
.PP
A sub that exists to test sub pod declarators\\&.

.SS class \$c

.PP
Argument c

.SS class \$d

.PP
Argument d
HERE

ok $roff.contains(qq:to/HERE/), "has the enum documentation";
.SS enum TestEnum

.RS 4m
.EX
enum TestEnum (
\t:foo(0), 
\t:bar(1), 
\t:baz(2), 
)

.EE
.RE
.PP
An enum that exists to test enum pod declarators\\&.
HERE

ok $roff.contains(qq:to/HERE/), "has the module documentation";
.SS module TestModule

.PP
A module that exists to test module pod declarators\\&.
HERE

ok $roff.contains(qq:to/HERE/), "has the subset documentation";
.SS subset test-subset

.RS 4m
.EX
subset test\\-subset of Str
.EE
.RE
.PP
A subset that exists to test subset pod declarators\\&.
HERE

ok $roff.contains(qq:to/HERE/), "has the package documentation";
.SS package TestPackage

.PP
A package that exists to test package pod declarators\\&.
HERE

done-testing;

# vim: expandtab shiftwidth=4


