X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=t%2Fmsg_iter.t;h=ac2066a2780d8d4fd7acb09a242633ae1863a183;hb=5d8dbb4aca5afcc6b98a4d951f03003fb7eaf356;hp=cc58b93fd6ffff9d8f3b0e81975a71533e6ae6f8;hpb=4f7977b42aab1595b3a45b41e19499b38c46c8cc;p=public-inbox.git diff --git a/t/msg_iter.t b/t/msg_iter.t index cc58b93f..ac2066a2 100644 --- a/t/msg_iter.t +++ b/t/msg_iter.t @@ -1,40 +1,110 @@ -# Copyright (C) 2016 all contributors +# Copyright (C) 2016-2020 all contributors # License: AGPL-3.0+ use strict; use warnings; use Test::More; -use Email::MIME; +use PublicInbox::TestCommon; +use PublicInbox::Hval qw(ascii_html); +use PublicInbox::InboxWritable; use_ok('PublicInbox::MsgIter'); { - my $parts = [ Email::MIME->create(body => 'a'), - Email::MIME->create(body => 'b') ]; - my $mime = Email::MIME->create(parts => $parts, + my $mime = mime_load 't/msg_iter-order.eml', sub { + my $parts = [ Email::MIME->create(body => "a\n"), + Email::MIME->create(body => "b\n") ]; + Email::MIME->create(parts => $parts, header_str => [ From => 'root@localhost' ]); + }; # mime_load sub my @parts; msg_iter($mime, sub { my ($part, $level, @ex) = @{$_[0]}; - push @parts, [ $part->body_str, $level, @ex ]; + my $s = $part->body_str; + $s =~ s/\s+//s; + push @parts, [ $s, $level, @ex ]; }); is_deeply(\@parts, [ [ qw(a 1 1) ], [ qw(b 1 2) ] ], 'order is fine'); } { + my $mime = mime_load 't/msg_iter-nested.eml', sub { my $parts = [ Email::MIME->create(body => 'a'), Email::MIME->create(body => 'b') ]; $parts = [ Email::MIME->create(parts => $parts, header_str => [ From => 'sub@localhost' ]), Email::MIME->create(body => 'sig') ]; - my $mime = Email::MIME->create(parts => $parts, + Email::MIME->create(parts => $parts, header_str => [ From => 'root@localhost' ]); + }; # mime_load sub my @parts; msg_iter($mime, sub { my ($part, $level, @ex) = @{$_[0]}; - push @parts, [ $part->body_str, $level, @ex ]; + my $s = $part->body_str; + $s =~ s/\s+//s; + push @parts, [ $s, $level, @ex ]; }); - is_deeply(\@parts, [ [ qw(a 2 1 1)], [qw(b 2 1 2)], [qw(sig 1 2)] ], + is_deeply(\@parts, [ [qw(a 2 1 1)], [qw(b 2 1 2)], [qw(sig 1 2)] ], 'nested part shows up properly'); } +{ + my $f = 't/iso-2202-jp.eml'; + my $mime = PublicInbox::InboxWritable::mime_from_path($f) or + die "open $f: $!"; + my $raw = ''; + msg_iter($mime, sub { + my ($part, $level, @ex) = @{$_[0]}; + my ($s, $err) = msg_part_text($part, 'text/plain'); + ok(!$err, 'no error'); + $raw .= $s; + }); + ok(length($raw) > 0, 'got non-empty message'); + is(index($raw, '$$$'), -1, 'no unescaped $$$'); +} + +{ + my $f = 't/x-unknown-alpine.eml'; + my $mime = PublicInbox::InboxWritable::mime_from_path($f) or + die "open $f: $!"; + my $raw = ''; + msg_iter($mime, sub { + my ($part, $level, @ex) = @{$_[0]}; + my ($s, $err) = msg_part_text($part, 'text/plain'); + $raw .= $s; + }); + like($raw, qr!^\thttps://!ms, 'tab expanded with X-UNKNOWN'); + like(ascii_html($raw), qr/• bullet point/s, + 'got bullet point when X-UNKNOWN assumes UTF-8'); +} + +{ # API not finalized + my @warn; + local $SIG{__WARN__} = sub { push @warn, [ @_ ] }; + my $attr = "So and so wrote:\n"; + my $q = "> hello world\n" x 10; + my $nq = "hello world\n" x 10; + my @sections = PublicInbox::MsgIter::split_quotes($attr . $q . $nq); + is($sections[0], $attr, 'attribution matches'); + is($sections[1], $q, 'quoted section matches'); + is($sections[2], $nq, 'non-quoted section matches'); + is(scalar(@sections), 3, 'only three sections for short message'); + is_deeply(\@warn, [], 'no warnings'); + + $q x= 3300; + $nq x= 3300; + @sections = PublicInbox::MsgIter::split_quotes($attr . $q . $nq); + is_deeply(\@warn, [], 'no warnings on giant message'); + is(join('', @sections), $attr . $q . $nq, 'result matches expected'); + is(shift(@sections), $attr, 'attribution is first section'); + my @check = ('', ''); + while (defined(my $l = shift @sections)) { + next if $l eq ''; + like($l, qr/\n\z/s, 'section ends with newline'); + my $idx = ($l =~ /\A>/) ? 0 : 1; + $check[$idx] .= $l; + } + is($check[0], $q, 'long quoted section matches'); + is($check[1], $nq, 'long quoted section matches'); +} + done_testing(); 1;