]> Sergey Matveev's repositories - public-inbox.git/blob - t/hl_mod.t
hval: replace "'" with "'" for compatibility
[public-inbox.git] / t / hl_mod.t
1 #!/usr/bin/perl -w
2 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use warnings;
6 use Test::More;
7 eval { require highlight } or
8         plan skip_all => 'failed to load highlight.pm';
9 use_ok 'PublicInbox::HlMod';
10 my $hls = PublicInbox::HlMod->new;
11 ok($hls, 'initialized OK');
12 is($hls->_shebang2lang(\"#!/usr/bin/perl -w\n"), 'perl', 'perl shebang OK');
13 is($hls->{-ext2lang}->{'pm'}, 'perl', '.pm suffix OK');
14 is($hls->{-ext2lang}->{'pl'}, 'perl', '.pl suffix OK');
15 is($hls->_path2lang('Makefile'), 'make', 'Makefile OK');
16 my $str = do { local $/; open(my $fh, __FILE__); <$fh> };
17 my $orig = $str;
18
19 {
20         my $ref = $hls->do_hl(\$str, 'foo.perl');
21         is(ref($ref), 'SCALAR', 'got a scalar reference back');
22         ok(utf8::valid($$ref), 'resulting string is utf8::valid');
23         like($$ref, qr/I can see you!/, 'we can see ourselves in output');
24         like($$ref, qr/&amp;&amp;/, 'escaped');
25         my $lref = $hls->do_hl_lang(\$str, 'perl');
26         is($$ref, $$lref, 'do_hl_lang matches do_hl');
27
28         use PublicInbox::Spawn qw(which);
29         if (eval { require IPC::Run } && which('w3m')) {
30                 require File::Temp;
31                 my $cmd = [ qw(w3m -T text/html -dump -config /dev/null) ];
32                 my ($out, $err) = ('', '');
33
34                 IPC::Run::run($cmd, \('<pre>'.$$ref.'</pre>'), \$out, \$err);
35                 # expand tabs and normalize whitespace,
36                 # w3m doesn't preserve tabs
37                 $orig =~ s/\t/        /gs;
38                 $out =~ s/\s*\z//sg;
39                 $orig =~ s/\s*\z//sg;
40                 is($out, $orig, 'w3m output matches');
41         }
42 }
43
44 if ('experimental, only for help text') {
45         my $tmp = <<'EOF';
46 :>
47 ```perl
48 my $foo = 1 & 2;
49 ```
50 :<
51 EOF
52         $hls->do_hl_text(\$tmp);
53         my @hl = split(/^/m, $tmp);
54         is($hl[0], ":&gt;\n", 'first line escaped');
55         is($hl[1], "```perl\n", '2nd line preserved');
56         like($hl[2], qr/<span\b/, 'code highlighted');
57         like($hl[2], qr/&amp;/, 'ampersand escaped');
58         is($hl[3], "```\n", '4th line preserved');
59         is($hl[4], ":&lt;\n", '5th line escaped');
60         is(scalar(@hl), 5, 'no extra line');
61
62 }
63
64 done_testing;