-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from wneessen/feature/152_recipients_from_sin…
…gle_string Add new methods and tests for handling email addresses
- Loading branch information
Showing
2 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2937,3 +2937,180 @@ func TestMsg_EmbedReadSeeker(t *testing.T) { | |
t.Errorf("EmbedReadSeeker() failed. Expected string: %q, got: %q", ts, wbuf.String()) | ||
} | ||
} | ||
|
||
// TestMsg_ToFromString tests Msg.ToFromString in different scenarios | ||
func TestMsg_ToFromString(t *testing.T) { | ||
tests := []struct { | ||
n string | ||
v string | ||
w []*mail.Address | ||
sf bool | ||
}{ | ||
{"valid single address", "[email protected]", []*mail.Address{ | ||
{Name: "", Address: "[email protected]"}, | ||
}, false}, | ||
{ | ||
"valid multiple addresses", "[email protected],[email protected]", | ||
[]*mail.Address{ | ||
{Name: "", Address: "[email protected]"}, | ||
{Name: "", Address: "[email protected]"}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"valid multiple addresses with space and name", | ||
`[email protected], "Toni Tester" <[email protected]>`, | ||
[]*mail.Address{ | ||
{Name: "", Address: "[email protected]"}, | ||
{Name: "Toni Tester", Address: "[email protected]"}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid and valid multiple addresses", "[email protected],test2#example.com", nil, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.n, func(t *testing.T) { | ||
m := NewMsg() | ||
if err := m.ToFromString(tt.v); err != nil && !tt.sf { | ||
t.Errorf("Msg.ToFromString failed: %s", err) | ||
return | ||
} | ||
mto := m.GetTo() | ||
if len(mto) != len(tt.w) { | ||
t.Errorf("Msg.ToFromString failed, expected len: %d, got: %d", len(tt.w), | ||
len(mto)) | ||
return | ||
} | ||
for i := range mto { | ||
w := tt.w[i] | ||
g := mto[i] | ||
if w.String() != g.String() { | ||
t.Errorf("Msg.ToFromString failed, expected address: %s, got: %s", | ||
w.String(), g.String()) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// TestMsg_CcFromString tests Msg.CcFromString in different scenarios | ||
func TestMsg_CcFromString(t *testing.T) { | ||
tests := []struct { | ||
n string | ||
v string | ||
w []*mail.Address | ||
sf bool | ||
}{ | ||
{"valid single address", "[email protected]", []*mail.Address{ | ||
{Name: "", Address: "[email protected]"}, | ||
}, false}, | ||
{ | ||
"valid multiple addresses", "[email protected],[email protected]", | ||
[]*mail.Address{ | ||
{Name: "", Address: "[email protected]"}, | ||
{Name: "", Address: "[email protected]"}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"valid multiple addresses with space and name", | ||
`[email protected], "Toni Tester" <[email protected]>`, | ||
[]*mail.Address{ | ||
{Name: "", Address: "[email protected]"}, | ||
{Name: "Toni Tester", Address: "[email protected]"}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid and valid multiple addresses", "[email protected],test2#example.com", nil, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.n, func(t *testing.T) { | ||
m := NewMsg() | ||
if err := m.CcFromString(tt.v); err != nil && !tt.sf { | ||
t.Errorf("Msg.CcFromString failed: %s", err) | ||
return | ||
} | ||
mto := m.GetCc() | ||
if len(mto) != len(tt.w) { | ||
t.Errorf("Msg.CcFromString failed, expected len: %d, got: %d", len(tt.w), | ||
len(mto)) | ||
return | ||
} | ||
for i := range mto { | ||
w := tt.w[i] | ||
g := mto[i] | ||
if w.String() != g.String() { | ||
t.Errorf("Msg.CcFromString failed, expected address: %s, got: %s", | ||
w.String(), g.String()) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// TestMsg_BccFromString tests Msg.BccFromString in different scenarios | ||
func TestMsg_BccFromString(t *testing.T) { | ||
tests := []struct { | ||
n string | ||
v string | ||
w []*mail.Address | ||
sf bool | ||
}{ | ||
{"valid single address", "[email protected]", []*mail.Address{ | ||
{Name: "", Address: "[email protected]"}, | ||
}, false}, | ||
{ | ||
"valid multiple addresses", "[email protected],[email protected]", | ||
[]*mail.Address{ | ||
{Name: "", Address: "[email protected]"}, | ||
{Name: "", Address: "[email protected]"}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"valid multiple addresses with space and name", | ||
`[email protected], "Toni Tester" <[email protected]>`, | ||
[]*mail.Address{ | ||
{Name: "", Address: "[email protected]"}, | ||
{Name: "Toni Tester", Address: "[email protected]"}, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid and valid multiple addresses", "[email protected],test2#example.com", nil, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.n, func(t *testing.T) { | ||
m := NewMsg() | ||
if err := m.BccFromString(tt.v); err != nil && !tt.sf { | ||
t.Errorf("Msg.BccFromString failed: %s", err) | ||
return | ||
} | ||
mto := m.GetBcc() | ||
if len(mto) != len(tt.w) { | ||
t.Errorf("Msg.BccFromString failed, expected len: %d, got: %d", len(tt.w), | ||
len(mto)) | ||
return | ||
} | ||
for i := range mto { | ||
w := tt.w[i] | ||
g := mto[i] | ||
if w.String() != g.String() { | ||
t.Errorf("Msg.BccFromString failed, expected address: %s, got: %s", | ||
w.String(), g.String()) | ||
} | ||
} | ||
}) | ||
} | ||
} |