public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 86f47395f4f709005b8ccf6e684273a4e0bf92a3 3236 bytes (raw)
$ git show HEAD:t/address.t	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
 
#!perl -w
# Copyright (C) all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use v5.12;
use Test::More;
use_ok 'PublicInbox::Address';

sub test_pkg {
	my ($pkg) = @_;
	my $emails = $pkg->can('emails');
	my $names = $pkg->can('names');
	my $pairs = $pkg->can('pairs');
	my $objects = $pkg->can('objects');

	is_deeply([qw(e@example.com e@example.org)],
		[$emails->('User <e@example.com>, e@example.org')],
		'address extraction works as expected');

	is_deeply($pairs->('User <e@example.com>, e@example.org'),
			[[qw(User e@example.com)], [undef, 'e@example.org']],
		"pair extraction works ($pkg)");

	is_deeply(['user@example.com'],
		[$emails->('<user@example.com (Comment)>')],
		'comment after domain accepted before >');
	is_deeply($pairs->('<user@example.com (Comment)>'),
		[[qw(Comment user@example.com)]], "comment as name ($pkg)");

	my $s = 'User <e@e>, e@e, "John A. Doe" <j@d>, <x@x>, <y@x> (xyz), '.
		'U Ser <u@x> (do not use)';
	my @names = $names->($s);
	is_deeply(\@names, ['User', 'e', 'John A. Doe', 'x', 'xyz', 'U Ser'],
		'name extraction works as expected');
	is_deeply($pairs->($s), [ [ 'User', 'e@e' ], [ undef, 'e@e' ],
			[ 'John A. Doe', 'j@d' ], [ undef, 'x@x' ],
			[ 'xyz', 'y@x' ], [ 'U Ser', 'u@x' ] ],
		"pairs extraction works for $pkg");

	# only what's used by PublicInbox::IMAP:
	my @objs = $objects->($s);
	my @exp = (qw(User e e), qw(e e e), ('John A. Doe', qw(j d)),
		qw(x x x), qw(xyz y x), ('U Ser', qw(u x)));
	for (my $i = 0; $i <= $#objs; $i++) {
		my $exp_name = shift @exp;
		my $name = $objs[$i]->name;
		is $name, $exp_name, "->name #$i matches";
		is $objs[$i]->user, shift @exp, "->user #$i matches";
		is $objs[$i]->host , shift @exp, "->host #$i matches";
	}

	@names = $names->('"user@example.com" <user@example.com>');
	is_deeply(['user'], \@names,
		'address-as-name extraction works as expected');
	is_deeply($pairs->('"user@example.com" <user@example.com>'),
		[ [ 'user@example.com', 'user@example.com' ] ],
		"pairs for $pkg");

	{
		my $backwards = 'u@example.com (John Q. Public)';
		@names = $names->($backwards);
		is_deeply(\@names, ['John Q. Public'], 'backwards name OK');
		my @emails = $emails->($backwards);
		is_deeply(\@emails, ['u@example.com'], 'backwards emails OK');

		is_deeply($pairs->($backwards),
			[ [ 'John Q. Public', 'u@example.com' ] ],
			"backwards pairs $pkg");
	}

	$s = '"Quote Unneeded" <user@example.com>';
	@names = $names->($s);
	is_deeply(['Quote Unneeded'], \@names, 'extra quotes dropped');
	is_deeply($pairs->($s), [ [ 'Quote Unneeded', 'user@example.com' ] ],
		"extra quotes dropped in pairs $pkg");

	my @emails = $emails->('Local User <user>');
	is_deeply([], \@emails , 'no address for local address');
	@names = $emails->('Local User <user>');
	is_deeply([], \@names, 'no address, no name');

	my $p = $pairs->('NAME, a@example, wtf@');
	is scalar(grep { defined($_->[0] // $_->[1]) } @$p),
		scalar(@$p), 'something is always defined in bogus pairs';
}

test_pkg('PublicInbox::Address');

SKIP: {
	if ($INC{'PublicInbox/AddressPP.pm'}) {
		skip 'Email::Address::XS missing', 8;
	}
	use_ok 'PublicInbox::AddressPP';
	test_pkg('PublicInbox::AddressPP');
}

done_testing;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git