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