]> Sergey Matveev's repositories - public-inbox.git/blob - t/hl_mod.t
hlmod: disable enclosing <pre> tag
[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         like($$ref, qr/I can see you!/, 'we can see ourselves in output');
23         like($$ref, qr/&amp;&amp;/, 'escaped');
24
25         use PublicInbox::Spawn qw(which);
26         if (eval { require IPC::Run } && which('w3m')) {
27                 require File::Temp;
28                 my $cmd = [ qw(w3m -T text/html -dump -config /dev/null) ];
29                 my ($out, $err) = ('', '');
30                 IPC::Run::run($cmd, \('<pre>'.$$ref.'</pre>'), \$out, \$err);
31                 # expand tabs and normalize whitespace,
32                 # w3m doesn't preserve tabs
33                 $orig =~ s/\t/        /gs;
34                 $out =~ s/\s*\z//sg;
35                 $orig =~ s/\s*\z//sg;
36                 is($out, $orig, 'w3m output matches');
37         }
38 }
39
40 my $nr = $ENV{TEST_MEMLEAK};
41 if ($nr && -r "/proc/$$/status") {
42         my $fh;
43         open $fh, '<', "/proc/$$/status";
44         diag "starting at memtest at ".join('', grep(/VmRSS:/, <$fh>));
45         PublicInbox::HlMod->new->do_hl(\$orig) for (1..$nr);
46         open $fh, '<', "/proc/$$/status";
47         diag "creating $nr instances: ".join('', grep(/VmRSS:/, <$fh>));
48         my $hls = PublicInbox::HlMod->new;
49         $hls->do_hl(\$orig) for (1..$nr);
50         $hls = undef;
51         open $fh, '<', "/proc/$$/status";
52         diag "reused instance $nr times: ".join('', grep(/VmRSS:/, <$fh>));
53 }
54
55 done_testing;