]> Sergey Matveev's repositories - public-inbox.git/blob - t/msg_iter.t
4ee3a201c797f4570a5ca2a1e719f062b442a71c
[public-inbox.git] / t / msg_iter.t
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Hval qw(ascii_html);
8 use_ok('PublicInbox::MsgIter');
9
10 {
11         my $mime = eml_load 't/msg_iter-order.eml';
12         my @parts;
13         msg_iter($mime, sub {
14                 my ($part, $level, @ex) = @{$_[0]};
15                 my $s = $part->body_str;
16                 $s =~ s/\s+//s;
17                 push @parts, [ $s, $level, @ex ];
18         });
19         is_deeply(\@parts, [ [ qw(a 1 1) ], [ qw(b 1 2) ] ], 'order is fine');
20 }
21
22 {
23         my $mime = eml_load 't/msg_iter-nested.eml';
24         my @parts;
25         msg_iter($mime, sub {
26                 my ($part, $level, @ex) = @{$_[0]};
27                 my $s = $part->body_str;
28                 $s =~ s/\s+//s;
29                 push @parts, [ $s, $level, @ex ];
30         });
31         is_deeply(\@parts, [ [qw(a 2 1.1)], [qw(b 2 1.2)], [qw(sig 1 2)] ],
32                 'nested part shows up properly');
33 }
34
35 {
36         my $mime = eml_load 't/iso-2202-jp.eml';
37         my $raw = '';
38         msg_iter($mime, sub {
39                 my ($part, $level, @ex) = @{$_[0]};
40                 my ($s, $err) = msg_part_text($part, 'text/plain');
41                 ok(!$err, 'no error');
42                 $raw .= $s;
43         });
44         ok(length($raw) > 0, 'got non-empty message');
45         is(index($raw, '$$$'), -1, 'no unescaped $$$');
46 }
47
48 {
49         my $mime = eml_load 't/x-unknown-alpine.eml';
50         my $raw = '';
51         msg_iter($mime, sub {
52                 my ($part, $level, @ex) = @{$_[0]};
53                 my ($s, $err) = msg_part_text($part, 'text/plain');
54                 $raw .= $s;
55         });
56         like($raw, qr!^\thttps://!ms, 'tab expanded with X-UNKNOWN');
57         like(ascii_html($raw), qr/&#8226; bullet point/s,
58                 'got bullet point when X-UNKNOWN assumes UTF-8');
59 }
60
61 { # API not finalized
62         my @warn;
63         local $SIG{__WARN__} = sub { push @warn, [ @_ ] };
64         my $attr = "So and so wrote:\n";
65         my $q = "> hello world\n" x 10;
66         my $nq = "hello world\n" x 10;
67         my @sections = PublicInbox::MsgIter::split_quotes($attr . $q . $nq);
68         is($sections[0], $attr, 'attribution matches');
69         is($sections[1], $q, 'quoted section matches');
70         is($sections[2], $nq, 'non-quoted section matches');
71         is(scalar(@sections), 3, 'only three sections for short message');
72         is_deeply(\@warn, [], 'no warnings');
73
74         $q x= 3300;
75         $nq x= 3300;
76         @sections = PublicInbox::MsgIter::split_quotes($attr . $q . $nq);
77         is_deeply(\@warn, [], 'no warnings on giant message');
78         is(join('', @sections), $attr . $q . $nq, 'result matches expected');
79         is(shift(@sections), $attr, 'attribution is first section');
80         my @check = ('', '');
81         while (defined(my $l = shift @sections)) {
82                 next if $l eq '';
83                 like($l, qr/\n\z/s, 'section ends with newline');
84                 my $idx = ($l =~ /\A>/) ? 0 : 1;
85                 $check[$idx] .= $l;
86         }
87         is($check[0], $q, 'long quoted section matches');
88         is($check[1], $nq, 'long quoted section matches');
89 }
90
91 done_testing();
92 1;