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