Debian Codegraph @

t/base.t

raw
1 #!/usr/bin/env perl -w
2
3 use strict;
4 use warnings;
5 use Test::More tests => 38;
6 #use Test::More 'no_plan';
7 use File::Spec::Functions qw(catdir);
8 use HTML::Entities;
9
10 BEGIN { use_ok 'Text::Markup' or die; }
11
12 can_ok 'Text::Markup' => qw(
13 register
14 formats
15 new
16 parse
17 default_format
18 _get_parser
19 );
20
21 # Find core parsers.
22 my $dir = catdir qw(lib Text Markup);
23 opendir my $dh, $dir or die "Cannot open diretory $dir: $!\n";
24 my @core_parsers = sort qw(
25 creole
26 mediawiki
27 html
28 rest
29 pod
30 bbcode
31 markdown
32 textile
33 asciidoc
34 multimarkdown
35 trac
36 );
37
38 is_deeply [Text::Markup->formats], \@core_parsers,
39 'Should have core formats';
40
41 ok my %matchers = Text::Markup->format_matchers,
42 'Get format matchers';
43 is_deeply [sort keys %matchers], [sort @core_parsers],
44 'Should have core format matchers';
45 isa_ok $_, 'Regexp', $_ for values %matchers;
46
47 # Register one.
48 PARSER: {
49 package My::Cool::Parser;
50 use Text::Markup;
51 Text::Markup->register(cool => qr{cool});
52 sub parser {
53 return $_[2]->[0] || 'hello';
54 }
55 }
56
57 is_deeply [Text::Markup->formats], [sort @core_parsers, 'cool'],
58 'Should be now have the "cool" parser';
59
60 my $parser = new_ok 'Text::Markup';
61 is $parser->default_format, undef, 'Should have no default format';
62
63 $parser = new_ok 'Text::Markup', [default_format => 'cool'];
64 is $parser->default_format, 'cool', 'Should have default format';
65
66 is $parser->_get_parser({ format => 'cool' }), My::Cool::Parser->can('parser'),
67 'Should be able to find specific parser';
68
69 is $parser->_get_parser({ file => 'foo' }), My::Cool::Parser->can('parser'),
70 'Should be able to find default format parser';
71
72 $parser->default_format(undef);
73 is $parser->_get_parser({ file => 'foo'}), Text::Markup::None->can('parser'),
74 'Should be find the specified default parser';
75
76 # Now make it guess the format.
77 $parser->default_format(undef);
78 is $parser->_get_parser({ file => 'foo.cool'}),
79 My::Cool::Parser->can('parser'),
80 'Should be able to guess the parser file the file name';
81
82 # Now test guess_format.
83 is $parser->guess_format('foo.cool'), 'cool',
84 'Should guess "cool" format file "foo.cool"';
85 is $parser->guess_format('foocool'), undef,
86 'Should not guess "cool" format file "foocool"';
87 is $parser->guess_format('foo.cool.txt'), undef,
88 'Should not guess "cool" format file "foo.cool.txt"';
89
90 # Add another parser.
91 PARSER: {
92 package My::Funky::Parser;
93 Text::Markup->register(funky => qr{funky(?:[.]txt)?});
94 sub parser {
95 # Must return a UTF-8 encoded string.
96 use utf8;
97 my $ret = 'fünky';
98 utf8::encode($ret);
99 return $ret;
100 }
101 }
102
103 is_deeply [Text::Markup->formats], [sort @core_parsers, qw(cool funky)],
104 'Should be now have the "cool" and "funky" parsers';
105 is $parser->guess_format('foo.cool'), 'cool',
106 'Should still guess "cool" format file "foo.cool"';
107 is $parser->guess_format('foo.funky'), 'funky',
108 'Should guess "funky" format file "foo.funky"';
109 is $parser->guess_format('foo.funky.txt'), 'funky',
110 'Should guess "funky" format file "foo.funky.txt"';
111
112 # Now try parsing.
113 is $parser->parse(
114 file => 'README.md',
115 format => 'cool',
116 ), 'hello', 'Test the "cool" parser';
117
118 # Send output to a file.
119 is $parser->parse(
120 file => 'README.md',
121 format => 'funky',
122 ), 'fünky', 'Test the "funky" parser';
123
124 # Test opts to the parser.
125 is $parser->parse(
126 file => 'README.md',
127 format => 'cool',
128 options => ['goodbye'],
129 ), 'goodbye', 'Test the "cool" parser with options';
130
131 my $pod_dir = catdir (qw(t markups));
132
133 like $parser->parse(
134 file => "$pod_dir/pod.txt",
135 format => "pod",
136 options => [
137 html_header => '',
138 ],
139 ), qr|</html>|, 'Test pod option to suppress HTML header';
140
141 unlike $parser->parse(
142 file => "$pod_dir/pod.txt",
143 format => "pod",
144 options => [
145 html_header => '',
146 html_footer => '',
147 ],
148 ), qr|</html>|, 'Test pod options to suppress HTML header and footer';
149
150 # Test the "none" parser.
151 my $output = do {
152 my $f = __FILE__;
153 open my $fh, '<:utf8', $f or die "Cannot open $f: $!\n";
154 local $/;
155 my $html = encode_entities(<$fh>, '<>&"');
156 utf8::encode($html);
157 qq{<html>
158 <head>
159 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
160 </head>
161 <body>
162 <pre>$html</pre>
163 </body>
164 </html>
165 };
166 };
167 $parser->default_format(undef);
168 is $parser->parse(
169 file => __FILE__,
170 ), $output, 'Test the "none" parser';