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