lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / address.t
blob86f47395f4f709005b8ccf6e684273a4e0bf92a3
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12;
5 use Test::More;
6 use_ok 'PublicInbox::Address';
8 sub test_pkg {
9         my ($pkg) = @_;
10         my $emails = $pkg->can('emails');
11         my $names = $pkg->can('names');
12         my $pairs = $pkg->can('pairs');
13         my $objects = $pkg->can('objects');
15         is_deeply([qw(e@example.com e@example.org)],
16                 [$emails->('User <e@example.com>, e@example.org')],
17                 'address extraction works as expected');
19         is_deeply($pairs->('User <e@example.com>, e@example.org'),
20                         [[qw(User e@example.com)], [undef, 'e@example.org']],
21                 "pair extraction works ($pkg)");
23         is_deeply(['user@example.com'],
24                 [$emails->('<user@example.com (Comment)>')],
25                 'comment after domain accepted before >');
26         is_deeply($pairs->('<user@example.com (Comment)>'),
27                 [[qw(Comment user@example.com)]], "comment as name ($pkg)");
29         my $s = 'User <e@e>, e@e, "John A. Doe" <j@d>, <x@x>, <y@x> (xyz), '.
30                 'U Ser <u@x> (do not use)';
31         my @names = $names->($s);
32         is_deeply(\@names, ['User', 'e', 'John A. Doe', 'x', 'xyz', 'U Ser'],
33                 'name extraction works as expected');
34         is_deeply($pairs->($s), [ [ 'User', 'e@e' ], [ undef, 'e@e' ],
35                         [ 'John A. Doe', 'j@d' ], [ undef, 'x@x' ],
36                         [ 'xyz', 'y@x' ], [ 'U Ser', 'u@x' ] ],
37                 "pairs extraction works for $pkg");
39         # only what's used by PublicInbox::IMAP:
40         my @objs = $objects->($s);
41         my @exp = (qw(User e e), qw(e e e), ('John A. Doe', qw(j d)),
42                 qw(x x x), qw(xyz y x), ('U Ser', qw(u x)));
43         for (my $i = 0; $i <= $#objs; $i++) {
44                 my $exp_name = shift @exp;
45                 my $name = $objs[$i]->name;
46                 is $name, $exp_name, "->name #$i matches";
47                 is $objs[$i]->user, shift @exp, "->user #$i matches";
48                 is $objs[$i]->host , shift @exp, "->host #$i matches";
49         }
51         @names = $names->('"user@example.com" <user@example.com>');
52         is_deeply(['user'], \@names,
53                 'address-as-name extraction works as expected');
54         is_deeply($pairs->('"user@example.com" <user@example.com>'),
55                 [ [ 'user@example.com', 'user@example.com' ] ],
56                 "pairs for $pkg");
58         {
59                 my $backwards = 'u@example.com (John Q. Public)';
60                 @names = $names->($backwards);
61                 is_deeply(\@names, ['John Q. Public'], 'backwards name OK');
62                 my @emails = $emails->($backwards);
63                 is_deeply(\@emails, ['u@example.com'], 'backwards emails OK');
65                 is_deeply($pairs->($backwards),
66                         [ [ 'John Q. Public', 'u@example.com' ] ],
67                         "backwards pairs $pkg");
68         }
70         $s = '"Quote Unneeded" <user@example.com>';
71         @names = $names->($s);
72         is_deeply(['Quote Unneeded'], \@names, 'extra quotes dropped');
73         is_deeply($pairs->($s), [ [ 'Quote Unneeded', 'user@example.com' ] ],
74                 "extra quotes dropped in pairs $pkg");
76         my @emails = $emails->('Local User <user>');
77         is_deeply([], \@emails , 'no address for local address');
78         @names = $emails->('Local User <user>');
79         is_deeply([], \@names, 'no address, no name');
81         my $p = $pairs->('NAME, a@example, wtf@');
82         is scalar(grep { defined($_->[0] // $_->[1]) } @$p),
83                 scalar(@$p), 'something is always defined in bogus pairs';
86 test_pkg('PublicInbox::Address');
88 SKIP: {
89         if ($INC{'PublicInbox/AddressPP.pm'}) {
90                 skip 'Email::Address::XS missing', 8;
91         }
92         use_ok 'PublicInbox::AddressPP';
93         test_pkg('PublicInbox::AddressPP');
96 done_testing;