summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/perl/cpan/Pod-Simple/t/strpvbtm.t
blob: 25c41cc55bd2604c8dbafc0d6aa03309b9a01495 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/perl -w

# t/strip_verbatim_indent.t.t - check verbatim indent stripping feature

BEGIN {
    chdir 't' if -d 't';
}

use strict;
use lib '../lib';
use Test::More tests => 103;
#use Test::More 'no_plan';

use_ok('Pod::Simple::XHTML') or exit;
use_ok('Pod::Simple::XMLOutStream') or exit;

isa_ok my $parser = Pod::Simple::XHTML->new, 'Pod::Simple::XHTML';

ok $parser->strip_verbatim_indent(' '), 'Should be able to set striper to " "';
ok $parser->strip_verbatim_indent('    '), 'Should be able to set striper to "    "';
ok $parser->strip_verbatim_indent("t"), 'Should be able to set striper to "\\t"';
ok $parser->strip_verbatim_indent(sub { ' ' }), 'Should be able to set striper to coderef';

for my $spec (
    [
        "\n=pod\n\n foo bar baz\n",
        undef,
        qq{<Document><Verbatim\nxml:space="preserve"> foo bar baz</Verbatim></Document>},
        "<pre><code> foo bar baz</code></pre>\n\n",
        'undefined indent'
    ],
    [
        "\n=pod\n\n foo bar baz\n",
        ' ',
        qq{<Document><Verbatim\nxml:space="preserve">foo bar baz</Verbatim></Document>},
        "<pre><code>foo bar baz</code></pre>\n\n",
        'single space indent'
    ],
    [
        "\n=pod\n\n foo bar baz\n",
        '  ',
        qq{<Document><Verbatim\nxml:space="preserve"> foo bar baz</Verbatim></Document>},
        "<pre><code> foo bar baz</code></pre>\n\n",
        'too large indent'
    ],
    [
        "\n=pod\n\n  foo bar baz\n",
        '  ',
        qq{<Document><Verbatim\nxml:space="preserve">foo bar baz</Verbatim></Document>},
        "<pre><code>foo bar baz</code></pre>\n\n",
        'double space indent'
    ],
    [
        "\n=pod\n\n  foo bar baz\n",
        sub { '  ' },
        qq{<Document><Verbatim\nxml:space="preserve">foo bar baz</Verbatim></Document>},
        "<pre><code>foo bar baz</code></pre>\n\n",
        'code ref stripper'
    ],
    [
        "\n=pod\n\n foo bar\n\n baz blez\n",
        ' ',
        qq{<Document><Verbatim\nxml:space="preserve">foo bar\n\nbaz blez</Verbatim></Document>},
        "<pre><code>foo bar\n\nbaz blez</code></pre>\n\n",
        'single space indent and empty line'
    ],
    [
        "\n=pod\n\n foo bar\n\n baz blez\n",
        sub { ' ' },
        qq{<Document><Verbatim\nxml:space="preserve">foo bar\n\nbaz blez</Verbatim></Document>},
        "<pre><code>foo bar\n\nbaz blez</code></pre>\n\n",
        'code ref indent and empty line'
    ],
    [
        "\n=pod\n\n foo bar\n\n baz blez\n",
        sub { (my $s = shift->[0]) =~ s/\S.*//; $s },
        qq{<Document><Verbatim\nxml:space="preserve">foo bar\n\nbaz blez</Verbatim></Document>},
        "<pre><code>foo bar\n\nbaz blez</code></pre>\n\n",
        'heuristic code ref indent'
    ],
    [
        "\n=pod\n\n foo bar\n   baz blez\n",
        sub { s/^\s+// for @{ $_[0] } },
        qq{<Document><Verbatim\nxml:space="preserve">foo bar\nbaz blez</Verbatim></Document>},
        "<pre><code>foo bar\nbaz blez</code></pre>\n\n",
        'militant code ref'
    ],
    [
        "\n=pod\n\n foo (bar\n   baz blez\n",
        sub { (my $i = $_[0]->[0]) =~ s/S.*//; $i },
        qq{<Document><Verbatim\nxml:space="preserve">\n   baz blez</Verbatim></Document>},
        "<pre><code>\n   baz blez</code></pre>\n\n",
        'code ref and paren'
    ],
) {
    my ($pod, $indent, $xml, $xhtml, $desc) = @$spec;
    # Test XML output.
    ok my $p = Pod::Simple::XMLOutStream->new, "Construct XML parser to test $desc";
    $p->hide_line_numbers(1);
    my $output = '';
    $p->output_string( \$output );
    is $indent, $p->strip_verbatim_indent($indent),
        'Set stripper for XML to ' . (defined $indent ? qq{"$indent"} : 'undef');
    ok $p->parse_string_document( $pod ), "Parse POD to XML for $desc";
    is $output, $xml, "Should have expected XML output for $desc";


    # Test XHTML output.
    ok $p = Pod::Simple::XHTML->new, "Construct XHMTL parser to test $desc";
    $p->html_header('');
    $p->html_footer('');
    $output = '';
    $p->output_string( \$output );
    is $indent, $p->strip_verbatim_indent($indent),
        'Set stripper for XHTML to ' . (defined $indent ? qq{"$indent"} : 'undef');
    ok $p->parse_string_document( $pod ), "Parse POD to XHTML for $desc";
    is $output, $xhtml, "Should have expected XHTML output for $desc";
}

for my $spec (
    [
        "\n=pod\n\n\t\tfoo bar baz\n",
        0,
        "<pre><code>\t\tfoo bar baz</code></pre>\n\n",
        'preserve tabs'
    ],
    [
        "\n=pod\n\n\t\tfoo bar baz\n",
        undef,
        "<pre><code>                foo bar baz</code></pre>\n\n",
        'preserve tabs'
    ],
    [
        "\n=pod\n\n\t\tfoo bar baz\n",
        -1,
        "<pre><code>                foo bar baz</code></pre>\n\n",
        'preserve tabs'
    ],
    [
        "\n=pod\n\n\t\tfoo bar baz\n",
        1,
        "<pre><code>  foo bar baz</code></pre>\n\n",
        'tabs are xlate to one space each'
    ],
) {
    my ($pod, $tabs, $xhtml, $desc) = @$spec;
    # Test XHTML output.
    ok my $p = Pod::Simple::XHTML->new, "Construct XHMTL parser to test $desc";
    $p->html_header('');
    $p->html_footer('');
    my $output = '';
    $p->output_string( \$output );
    is $tabs, $p->expand_verbatim_tabs($tabs),
        'Set tab  for XHTML to ' . (defined $tabs ? qq{"$tabs"} : 'undef');
    ok $p->parse_string_document( $pod ), "Parse POD to XHTML for $desc";
    is $output, $xhtml, "Should have expected XHTML output for $desc";
}