1#!/usr/bin/perl -w
2
3# t/strip_verbatim_indent.t.t - check verbatim indent stripping feature
4
5BEGIN {
6    chdir 't' if -d 't';
7}
8
9use strict;
10use lib '../lib';
11use Test::More tests => 103;
12#use Test::More 'no_plan';
13
14use_ok('Pod::Simple::XHTML') or exit;
15use_ok('Pod::Simple::XMLOutStream') or exit;
16
17isa_ok my $parser = Pod::Simple::XHTML->new, 'Pod::Simple::XHTML';
18
19ok $parser->strip_verbatim_indent(' '), 'Should be able to set striper to " "';
20ok $parser->strip_verbatim_indent('    '), 'Should be able to set striper to "    "';
21ok $parser->strip_verbatim_indent("t"), 'Should be able to set striper to "\\t"';
22ok $parser->strip_verbatim_indent(sub { ' ' }), 'Should be able to set striper to coderef';
23
24for my $spec (
25    [
26        "\n=pod\n\n foo bar baz\n",
27        undef,
28        qq{<Document><Verbatim\nxml:space="preserve"> foo bar baz</Verbatim></Document>},
29        "<pre><code> foo bar baz</code></pre>\n\n",
30        'undefined indent'
31    ],
32    [
33        "\n=pod\n\n foo bar baz\n",
34        ' ',
35        qq{<Document><Verbatim\nxml:space="preserve">foo bar baz</Verbatim></Document>},
36        "<pre><code>foo bar baz</code></pre>\n\n",
37        'single space indent'
38    ],
39    [
40        "\n=pod\n\n foo bar baz\n",
41        '  ',
42        qq{<Document><Verbatim\nxml:space="preserve"> foo bar baz</Verbatim></Document>},
43        "<pre><code> foo bar baz</code></pre>\n\n",
44        'too large indent'
45    ],
46    [
47        "\n=pod\n\n  foo bar baz\n",
48        '  ',
49        qq{<Document><Verbatim\nxml:space="preserve">foo bar baz</Verbatim></Document>},
50        "<pre><code>foo bar baz</code></pre>\n\n",
51        'double space indent'
52    ],
53    [
54        "\n=pod\n\n  foo bar baz\n",
55        sub { '  ' },
56        qq{<Document><Verbatim\nxml:space="preserve">foo bar baz</Verbatim></Document>},
57        "<pre><code>foo bar baz</code></pre>\n\n",
58        'code ref stripper'
59    ],
60    [
61        "\n=pod\n\n foo bar\n\n baz blez\n",
62        ' ',
63        qq{<Document><Verbatim\nxml:space="preserve">foo bar\n\nbaz blez</Verbatim></Document>},
64        "<pre><code>foo bar\n\nbaz blez</code></pre>\n\n",
65        'single space indent and empty line'
66    ],
67    [
68        "\n=pod\n\n foo bar\n\n baz blez\n",
69        sub { ' ' },
70        qq{<Document><Verbatim\nxml:space="preserve">foo bar\n\nbaz blez</Verbatim></Document>},
71        "<pre><code>foo bar\n\nbaz blez</code></pre>\n\n",
72        'code ref indent and empty line'
73    ],
74    [
75        "\n=pod\n\n foo bar\n\n baz blez\n",
76        sub { (my $s = shift->[0]) =~ s/\S.*//; $s },
77        qq{<Document><Verbatim\nxml:space="preserve">foo bar\n\nbaz blez</Verbatim></Document>},
78        "<pre><code>foo bar\n\nbaz blez</code></pre>\n\n",
79        'heuristic code ref indent'
80    ],
81    [
82        "\n=pod\n\n foo bar\n   baz blez\n",
83        sub { s/^\s+// for @{ $_[0] } },
84        qq{<Document><Verbatim\nxml:space="preserve">foo bar\nbaz blez</Verbatim></Document>},
85        "<pre><code>foo bar\nbaz blez</code></pre>\n\n",
86        'militant code ref'
87    ],
88    [
89        "\n=pod\n\n foo (bar\n   baz blez\n",
90        sub { (my $i = $_[0]->[0]) =~ s/S.*//; $i },
91        qq{<Document><Verbatim\nxml:space="preserve">\n   baz blez</Verbatim></Document>},
92        "<pre><code>\n   baz blez</code></pre>\n\n",
93        'code ref and paren'
94    ],
95) {
96    my ($pod, $indent, $xml, $xhtml, $desc) = @$spec;
97    # Test XML output.
98    ok my $p = Pod::Simple::XMLOutStream->new, "Construct XML parser to test $desc";
99    $p->hide_line_numbers(1);
100    my $output = '';
101    $p->output_string( \$output );
102    is $indent, $p->strip_verbatim_indent($indent),
103        'Set stripper for XML to ' . (defined $indent ? qq{"$indent"} : 'undef');
104    ok $p->parse_string_document( $pod ), "Parse POD to XML for $desc";
105    is $output, $xml, "Should have expected XML output for $desc";
106
107
108    # Test XHTML output.
109    ok $p = Pod::Simple::XHTML->new, "Construct XHMTL parser to test $desc";
110    $p->html_header('');
111    $p->html_footer('');
112    $output = '';
113    $p->output_string( \$output );
114    is $indent, $p->strip_verbatim_indent($indent),
115        'Set stripper for XHTML to ' . (defined $indent ? qq{"$indent"} : 'undef');
116    ok $p->parse_string_document( $pod ), "Parse POD to XHTML for $desc";
117    is $output, $xhtml, "Should have expected XHTML output for $desc";
118}
119
120for my $spec (
121    [
122        "\n=pod\n\n\t\tfoo bar baz\n",
123        0,
124        "<pre><code>\t\tfoo bar baz</code></pre>\n\n",
125        'preserve tabs'
126    ],
127    [
128        "\n=pod\n\n\t\tfoo bar baz\n",
129        undef,
130        "<pre><code>                foo bar baz</code></pre>\n\n",
131        'preserve tabs'
132    ],
133    [
134        "\n=pod\n\n\t\tfoo bar baz\n",
135        -1,
136        "<pre><code>                foo bar baz</code></pre>\n\n",
137        'preserve tabs'
138    ],
139    [
140        "\n=pod\n\n\t\tfoo bar baz\n",
141        1,
142        "<pre><code>  foo bar baz</code></pre>\n\n",
143        'tabs are xlate to one space each'
144    ],
145) {
146    my ($pod, $tabs, $xhtml, $desc) = @$spec;
147    # Test XHTML output.
148    ok my $p = Pod::Simple::XHTML->new, "Construct XHMTL parser to test $desc";
149    $p->html_header('');
150    $p->html_footer('');
151    my $output = '';
152    $p->output_string( \$output );
153    is $tabs, $p->expand_verbatim_tabs($tabs),
154        'Set tab  for XHTML to ' . (defined $tabs ? qq{"$tabs"} : 'undef');
155    ok $p->parse_string_document( $pod ), "Parse POD to XHTML for $desc";
156    is $output, $xhtml, "Should have expected XHTML output for $desc";
157}
158