]> Sergey Matveev's repositories - public-inbox.git/blob - t/msg_iter.t
testcommon: introduce mime_load sub
[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 PublicInbox::InboxWritable;
9 use_ok('PublicInbox::MsgIter');
10
11 {
12         my $mime = mime_load 't/msg_iter-order.eml', sub {
13         my $parts = [ Email::MIME->create(body => "a\n"),
14                         Email::MIME->create(body => "b\n") ];
15         Email::MIME->create(parts => $parts,
16                                 header_str => [ From => 'root@localhost' ]);
17         }; # mime_load sub
18         my @parts;
19         msg_iter($mime, sub {
20                 my ($part, $level, @ex) = @{$_[0]};
21                 my $s = $part->body_str;
22                 $s =~ s/\s+//s;
23                 push @parts, [ $s, $level, @ex ];
24         });
25         is_deeply(\@parts, [ [ qw(a 1 1) ], [ qw(b 1 2) ] ], 'order is fine');
26 }
27
28 {
29         my $mime = mime_load 't/msg_iter-nested.eml', sub {
30         my $parts = [ Email::MIME->create(body => 'a'),
31                         Email::MIME->create(body => 'b') ];
32         $parts = [ Email::MIME->create(parts => $parts,
33                                 header_str => [ From => 'sub@localhost' ]),
34                         Email::MIME->create(body => 'sig') ];
35         Email::MIME->create(parts => $parts,
36                                 header_str => [ From => 'root@localhost' ]);
37         }; # mime_load sub
38         my @parts;
39         msg_iter($mime, sub {
40                 my ($part, $level, @ex) = @{$_[0]};
41                 my $s = $part->body_str;
42                 $s =~ s/\s+//s;
43                 push @parts, [ $s, $level, @ex ];
44         });
45         is_deeply(\@parts, [ [qw(a 2 1 1)], [qw(b 2 1 2)], [qw(sig 1 2)] ],
46                 'nested part shows up properly');
47 }
48
49 {
50         my $f = 't/iso-2202-jp.eml';
51         my $mime = PublicInbox::InboxWritable::mime_from_path($f) or
52                 die "open $f: $!";
53         my $raw = '';
54         msg_iter($mime, sub {
55                 my ($part, $level, @ex) = @{$_[0]};
56                 my ($s, $err) = msg_part_text($part, 'text/plain');
57                 ok(!$err, 'no error');
58                 $raw .= $s;
59         });
60         ok(length($raw) > 0, 'got non-empty message');
61         is(index($raw, '$$$'), -1, 'no unescaped $$$');
62 }
63
64 {
65         my $f = 't/x-unknown-alpine.eml';
66         my $mime = PublicInbox::InboxWritable::mime_from_path($f) or
67                 die "open $f: $!";
68         my $raw = '';
69         msg_iter($mime, sub {
70                 my ($part, $level, @ex) = @{$_[0]};
71                 my ($s, $err) = msg_part_text($part, 'text/plain');
72                 $raw .= $s;
73         });
74         like($raw, qr!^\thttps://!ms, 'tab expanded with X-UNKNOWN');
75         like(ascii_html($raw), qr/&#8226; bullet point/s,
76                 'got bullet point when X-UNKNOWN assumes UTF-8');
77 }
78
79 { # API not finalized
80         my @warn;
81         local $SIG{__WARN__} = sub { push @warn, [ @_ ] };
82         my $attr = "So and so wrote:\n";
83         my $q = "> hello world\n" x 10;
84         my $nq = "hello world\n" x 10;
85         my @sections = PublicInbox::MsgIter::split_quotes($attr . $q . $nq);
86         is($sections[0], $attr, 'attribution matches');
87         is($sections[1], $q, 'quoted section matches');
88         is($sections[2], $nq, 'non-quoted section matches');
89         is(scalar(@sections), 3, 'only three sections for short message');
90         is_deeply(\@warn, [], 'no warnings');
91
92         $q x= 3300;
93         $nq x= 3300;
94         @sections = PublicInbox::MsgIter::split_quotes($attr . $q . $nq);
95         is_deeply(\@warn, [], 'no warnings on giant message');
96         is(join('', @sections), $attr . $q . $nq, 'result matches expected');
97         is(shift(@sections), $attr, 'attribution is first section');
98         my @check = ('', '');
99         while (defined(my $l = shift @sections)) {
100                 next if $l eq '';
101                 like($l, qr/\n\z/s, 'section ends with newline');
102                 my $idx = ($l =~ /\A>/) ? 0 : 1;
103                 $check[$idx] .= $l;
104         }
105         is($check[0], $q, 'long quoted section matches');
106         is($check[1], $nq, 'long quoted section matches');
107 }
108
109 done_testing();
110 1;