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