File: Dpkg_Email_Address.t

package info (click to toggle)
dpkg 1.23.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 59,120 kB
  • sloc: ansic: 40,205; perl: 30,746; sh: 18,907; cpp: 6,054; makefile: 5,226; sed: 127
file content (207 lines) | stat: -rw-r--r-- 7,061 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/perl
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use v5.36;

use Test::More tests => 31;
use Test::Dpkg qw(:paths);

use ok qw(Dpkg::Email::Address);
use ok qw(Dpkg::Email::AddressList);

my $addr = Dpkg::Email::Address->new();

$addr->parse('Some Name <email@example.org>');
is($addr->as_string, 'Some Name <email@example.org>',
    'Parse address correctly');
is($addr->name, 'Some Name',
    'Parse bare name from address correctly');
is($addr->email, 'email@example.org',
    'Parse bare name from address correctly');

$addr->parse('Some "Alias" Name <email@example.org>');
is($addr->as_string, 'Some "Alias" Name <email@example.org>',
    'Parse address with alias correctly');
is($addr->name, 'Some "Alias" Name',
    'Parse name from address with alias correctly');
is($addr->email, 'email@example.org',
    'Parse email from address with alias correctly');

$addr->parse('"Some Name, Comma" <email@example.org>');
is($addr->as_string, '"Some Name, Comma" <email@example.org>',
    'Parse address with quoted comma correctly');
is($addr->name, '"Some Name, Comma"',
    'Parse name from address with quoted comma correctly');
is($addr->email, 'email@example.org',
    'Parse email from address with quoted comma correctly');

my @uploaders_exp = (
    {
        desc => 'bare single address',
        value => 'Some Name <email@example.org>',
        parsed => [
            {
                name => 'Some Name',
                email => 'email@example.org',
            },
        ],
    },
    {
        desc => 'single bare address with trailing comma',
        value => 'Some Name <email@example.org> , ',
        normalized => 'Some Name <email@example.org>',
        parsed => [
            {
                name => 'Some Name',
                email => 'email@example.org',
            },
        ],
    },
    {
        desc => 'single address with alias',
        value => 'Some "Alias" Name <email@example.org>',
        parsed => [
            {
                name => 'Some "Alias" Name',
                email => 'email@example.org',
            },
        ],
    },
    {
        desc => 'single address with quoted comma',
        value => '"Some Name, Comma" <email@example.org>',
        parsed => [
            {
                name => '"Some Name, Comma"',
                email => 'email@example.org',
            },
        ],
    },
    {
        desc => 'two bare addresses',
        value => 'Some Name <some@example.org> , Other Name <other@example.org>',
        normalized => 'Some Name <some@example.org>, Other Name <other@example.org>',
        parsed => [
            {
                name => 'Some Name',
                email => 'some@example.org',
            },
            {
                name => 'Other Name',
                email => 'other@example.org',
            },
        ],
    },
    {
        desc => 'two bare addresses with trailing comma',
        value => 'Some Name <some@example.org> , Other Name <other@example.org> , ',
        normalized => 'Some Name <some@example.org>, Other Name <other@example.org>',
        parsed => [
            {
                name => 'Some Name',
                email => 'some@example.org',
            },
            {
                name => 'Other Name',
                email => 'other@example.org',
            },
        ],
    },
    {
        desc => 'two addresses with quoted comma and trailing comma',
        value => 'Some "Alias" Name <some@example.org> , "Other Name, Comma" <other@example.org> , ',
        normalized => 'Some "Alias" Name <some@example.org>, "Other Name, Comma" <other@example.org>',
        parsed => [
            {
                name => 'Some "Alias" Name',
                email => 'some@example.org',
            },
            {
                name => '"Other Name, Comma"',
                email => 'other@example.org',
            },
        ],
    },
    {
        desc => 'three bare addresses',
        value => 'Some Name <some@example.org> , Other Name <other@example.org>, Yet Another Name <yan@example.org>',
        normalized => 'Some Name <some@example.org>, Other Name <other@example.org>, Yet Another Name <yan@example.org>',
        parsed => [
            {
                name => 'Some Name',
                email => 'some@example.org',
            },
            {
                name => 'Other Name',
                email => 'other@example.org',
            },
            {
                name => 'Yet Another Name',
                email => 'yan@example.org',
            },
        ],
    },
    {
        desc => 'two bare addresses with trailing comma',
        value => 'Some Name <some@example.org> , Other Name <other@example.org> , Yet Another Name <yan@example.org> , ',
        normalized => 'Some Name <some@example.org>, Other Name <other@example.org>, Yet Another Name <yan@example.org>',
        parsed => [
            {
                name => 'Some Name',
                email => 'some@example.org',
            },
            {
                name => 'Other Name',
                email => 'other@example.org',
            },
            {
                name => 'Yet Another Name',
                email => 'yan@example.org',
            },
        ],
    },
    {
        desc => 'three addresses with quoted comma and trailing comma',
        value => 'Some "Alias" Name <some@example.org> , "Other Name, Comma"  <other@example.org> , Yet "Ds, Another" Name <yan@example.org> , ',
        normalized => 'Some "Alias" Name <some@example.org>, "Other Name, Comma"  <other@example.org>, Yet "Ds, Another" Name <yan@example.org>',
        parsed => [
            {
                name => 'Some "Alias" Name',
                email => 'some@example.org',
            },
            {
                name => '"Other Name, Comma" ',
                email => 'other@example.org',
            },
            {
                name => 'Yet "Ds, Another" Name',
                email => 'yan@example.org',
            },
        ],
    },
);

foreach my $uploader (@uploaders_exp) {
    my $addrlist = Dpkg::Email::AddressList->new($uploader->{value});

    my @res = map {
        $_->as_struct()
    } $addrlist->addrlist();

    is_deeply(\@res, $uploader->{parsed},
        "Parse uploader from $uploader->{desc}");
    is($addrlist->as_string(), $uploader->{normalized} // $uploader->{value},
        "Parse uploader from $uploader->{desc}, and stringified back");
}