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