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