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