mirror of
https://github.com/wneessen/go-mail.git
synced 2024-12-23 02:50:39 +01:00
Refactor test cases with shared test data
Consolidated repetitive test data into shared variables to improve code maintainability and readability. This modification also helps in reducing redundancy and streamlining future updates to test data.
This commit is contained in:
parent
c4946af3ab
commit
120c2efd08
1 changed files with 144 additions and 142 deletions
286
msg_test.go
286
msg_test.go
|
@ -12,17 +12,83 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
charsetTests = []struct {
|
||||
name string
|
||||
value Charset
|
||||
want Charset
|
||||
}{
|
||||
{"charset is UTF-7", CharsetUTF7, "UTF-7"},
|
||||
{"charset is UTF-8", CharsetUTF8, "UTF-8"},
|
||||
{"charset is US-ASCII", CharsetASCII, "US-ASCII"},
|
||||
{"charset is ISO-8859-1", CharsetISO88591, "ISO-8859-1"},
|
||||
{"charset is ISO-8859-2", CharsetISO88592, "ISO-8859-2"},
|
||||
{"charset is ISO-8859-3", CharsetISO88593, "ISO-8859-3"},
|
||||
{"charset is ISO-8859-4", CharsetISO88594, "ISO-8859-4"},
|
||||
{"charset is ISO-8859-5", CharsetISO88595, "ISO-8859-5"},
|
||||
{"charset is ISO-8859-6", CharsetISO88596, "ISO-8859-6"},
|
||||
{"charset is ISO-8859-7", CharsetISO88597, "ISO-8859-7"},
|
||||
{"charset is ISO-8859-9", CharsetISO88599, "ISO-8859-9"},
|
||||
{"charset is ISO-8859-13", CharsetISO885913, "ISO-8859-13"},
|
||||
{"charset is ISO-8859-14", CharsetISO885914, "ISO-8859-14"},
|
||||
{"charset is ISO-8859-15", CharsetISO885915, "ISO-8859-15"},
|
||||
{"charset is ISO-8859-16", CharsetISO885916, "ISO-8859-16"},
|
||||
{"charset is ISO-2022-JP", CharsetISO2022JP, "ISO-2022-JP"},
|
||||
{"charset is ISO-2022-KR", CharsetISO2022KR, "ISO-2022-KR"},
|
||||
{"charset is windows-1250", CharsetWindows1250, "windows-1250"},
|
||||
{"charset is windows-1251", CharsetWindows1251, "windows-1251"},
|
||||
{"charset is windows-1252", CharsetWindows1252, "windows-1252"},
|
||||
{"charset is windows-1255", CharsetWindows1255, "windows-1255"},
|
||||
{"charset is windows-1256", CharsetWindows1256, "windows-1256"},
|
||||
{"charset is KOI8-R", CharsetKOI8R, "KOI8-R"},
|
||||
{"charset is KOI8-U", CharsetKOI8U, "KOI8-U"},
|
||||
{"charset is Big5", CharsetBig5, "Big5"},
|
||||
{"charset is GB18030", CharsetGB18030, "GB18030"},
|
||||
{"charset is GB2312", CharsetGB2312, "GB2312"},
|
||||
{"charset is TIS-620", CharsetTIS620, "TIS-620"},
|
||||
{"charset is EUC-KR", CharsetEUCKR, "EUC-KR"},
|
||||
{"charset is Shift_JIS", CharsetShiftJIS, "Shift_JIS"},
|
||||
{"charset is GBK", CharsetGBK, "GBK"},
|
||||
{"charset is Unknown", CharsetUnknown, "Unknown"},
|
||||
}
|
||||
encodingTests = []struct {
|
||||
name string
|
||||
value Encoding
|
||||
want Encoding
|
||||
}{
|
||||
{"encoding is Quoted-Printable", EncodingQP, "quoted-printable"},
|
||||
{"encoding is Base64", EncodingB64, "base64"},
|
||||
{"encoding is Unencoded 8-Bit", NoEncoding, "8bit"},
|
||||
{"encoding is US-ASCII 7-Bit", EncodingUSASCII, "7bit"},
|
||||
}
|
||||
pgpTests = []struct {
|
||||
name string
|
||||
value PGPType
|
||||
}{
|
||||
{"Not a PGP encoded message", NoPGP},
|
||||
{"PGP encrypted message", PGPEncrypt},
|
||||
{"PGP signed message", PGPSignature},
|
||||
}
|
||||
boundaryTests = []struct {
|
||||
name string
|
||||
value string
|
||||
}{
|
||||
{"boundary: test123", "test123"},
|
||||
{"boundary is empty", ""},
|
||||
}
|
||||
mimeTests = []struct {
|
||||
name string
|
||||
value MIMEVersion
|
||||
want MIMEVersion
|
||||
}{
|
||||
{"MIME version: 1.0", MIME10, "1.0"},
|
||||
{"MIME version: 1.1", MIMEVersion("1.1"), "1.1"},
|
||||
}
|
||||
)
|
||||
|
||||
//go:embed README.md
|
||||
var efs embed.FS
|
||||
|
||||
/*
|
||||
addrHeader: make(map[AddrHeader][]*mail.Address),
|
||||
charset: CharsetUTF8,
|
||||
encoding: EncodingQP,
|
||||
genHeader: make(map[Header][]string),
|
||||
preformHeader: make(map[Header]string),
|
||||
mimever: MIME10,
|
||||
*/
|
||||
func TestNewMsg(t *testing.T) {
|
||||
t.Run("create new message", func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
|
@ -67,45 +133,7 @@ func TestNewMsg(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("new message with custom charsets", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value Charset
|
||||
want Charset
|
||||
}{
|
||||
{"charset is UTF-7", CharsetUTF7, "UTF-7"},
|
||||
{"charset is UTF-8", CharsetUTF8, "UTF-8"},
|
||||
{"charset is US-ASCII", CharsetASCII, "US-ASCII"},
|
||||
{"charset is ISO-8859-1", CharsetISO88591, "ISO-8859-1"},
|
||||
{"charset is ISO-8859-2", CharsetISO88592, "ISO-8859-2"},
|
||||
{"charset is ISO-8859-3", CharsetISO88593, "ISO-8859-3"},
|
||||
{"charset is ISO-8859-4", CharsetISO88594, "ISO-8859-4"},
|
||||
{"charset is ISO-8859-5", CharsetISO88595, "ISO-8859-5"},
|
||||
{"charset is ISO-8859-6", CharsetISO88596, "ISO-8859-6"},
|
||||
{"charset is ISO-8859-7", CharsetISO88597, "ISO-8859-7"},
|
||||
{"charset is ISO-8859-9", CharsetISO88599, "ISO-8859-9"},
|
||||
{"charset is ISO-8859-13", CharsetISO885913, "ISO-8859-13"},
|
||||
{"charset is ISO-8859-14", CharsetISO885914, "ISO-8859-14"},
|
||||
{"charset is ISO-8859-15", CharsetISO885915, "ISO-8859-15"},
|
||||
{"charset is ISO-8859-16", CharsetISO885916, "ISO-8859-16"},
|
||||
{"charset is ISO-2022-JP", CharsetISO2022JP, "ISO-2022-JP"},
|
||||
{"charset is ISO-2022-KR", CharsetISO2022KR, "ISO-2022-KR"},
|
||||
{"charset is windows-1250", CharsetWindows1250, "windows-1250"},
|
||||
{"charset is windows-1251", CharsetWindows1251, "windows-1251"},
|
||||
{"charset is windows-1252", CharsetWindows1252, "windows-1252"},
|
||||
{"charset is windows-1255", CharsetWindows1255, "windows-1255"},
|
||||
{"charset is windows-1256", CharsetWindows1256, "windows-1256"},
|
||||
{"charset is KOI8-R", CharsetKOI8R, "KOI8-R"},
|
||||
{"charset is KOI8-U", CharsetKOI8U, "KOI8-U"},
|
||||
{"charset is Big5", CharsetBig5, "Big5"},
|
||||
{"charset is GB18030", CharsetGB18030, "GB18030"},
|
||||
{"charset is GB2312", CharsetGB2312, "GB2312"},
|
||||
{"charset is TIS-620", CharsetTIS620, "TIS-620"},
|
||||
{"charset is EUC-KR", CharsetEUCKR, "EUC-KR"},
|
||||
{"charset is Shift_JIS", CharsetShiftJIS, "Shift_JIS"},
|
||||
{"charset is GBK", CharsetGBK, "GBK"},
|
||||
{"charset is Unknown", CharsetUnknown, "Unknown"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
for _, tt := range charsetTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg(WithCharset(tt.value), nil)
|
||||
if message == nil {
|
||||
|
@ -119,17 +147,7 @@ func TestNewMsg(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("new message with custom encoding", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value Encoding
|
||||
want Encoding
|
||||
}{
|
||||
{"encoding is Quoted-Printable", EncodingQP, "quoted-printable"},
|
||||
{"encoding is Base64", EncodingB64, "base64"},
|
||||
{"encoding is Unencoded 8-Bit", NoEncoding, "8bit"},
|
||||
{"encoding is US-ASCII 7-Bit", EncodingUSASCII, "7bit"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
for _, tt := range encodingTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg(WithEncoding(tt.value), nil)
|
||||
if message == nil {
|
||||
|
@ -143,15 +161,7 @@ func TestNewMsg(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("new message with custom MIME version", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value MIMEVersion
|
||||
want MIMEVersion
|
||||
}{
|
||||
{"MIME version: 1.0", MIME10, "1.0"},
|
||||
{"MIME version: 1.1", MIMEVersion("1.1"), "1.1"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
for _, tt := range mimeTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg(WithMIMEVersion(tt.value))
|
||||
if message == nil {
|
||||
|
@ -165,14 +175,7 @@ func TestNewMsg(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("new message with custom boundary", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value string
|
||||
}{
|
||||
{"boundary: test123", "test123"},
|
||||
{"boundary is empty", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
for _, tt := range boundaryTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg(WithBoundary(tt.value))
|
||||
if message == nil {
|
||||
|
@ -186,15 +189,7 @@ func TestNewMsg(t *testing.T) {
|
|||
}
|
||||
})
|
||||
t.Run("new message with custom PGP type", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value PGPType
|
||||
}{
|
||||
{"Not a PGP encoded message", NoPGP},
|
||||
{"PGP encrypted message", PGPEncrypt},
|
||||
{"PGP signed message", PGPSignature},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
for _, tt := range pgpTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg(WithPGPType(tt.value))
|
||||
if message == nil {
|
||||
|
@ -240,46 +235,8 @@ func TestNewMsg(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestMsg_SetCharset(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value Charset
|
||||
want Charset
|
||||
}{
|
||||
{"charset is UTF-7", CharsetUTF7, "UTF-7"},
|
||||
{"charset is UTF-8", CharsetUTF8, "UTF-8"},
|
||||
{"charset is US-ASCII", CharsetASCII, "US-ASCII"},
|
||||
{"charset is ISO-8859-1", CharsetISO88591, "ISO-8859-1"},
|
||||
{"charset is ISO-8859-2", CharsetISO88592, "ISO-8859-2"},
|
||||
{"charset is ISO-8859-3", CharsetISO88593, "ISO-8859-3"},
|
||||
{"charset is ISO-8859-4", CharsetISO88594, "ISO-8859-4"},
|
||||
{"charset is ISO-8859-5", CharsetISO88595, "ISO-8859-5"},
|
||||
{"charset is ISO-8859-6", CharsetISO88596, "ISO-8859-6"},
|
||||
{"charset is ISO-8859-7", CharsetISO88597, "ISO-8859-7"},
|
||||
{"charset is ISO-8859-9", CharsetISO88599, "ISO-8859-9"},
|
||||
{"charset is ISO-8859-13", CharsetISO885913, "ISO-8859-13"},
|
||||
{"charset is ISO-8859-14", CharsetISO885914, "ISO-8859-14"},
|
||||
{"charset is ISO-8859-15", CharsetISO885915, "ISO-8859-15"},
|
||||
{"charset is ISO-8859-16", CharsetISO885916, "ISO-8859-16"},
|
||||
{"charset is ISO-2022-JP", CharsetISO2022JP, "ISO-2022-JP"},
|
||||
{"charset is ISO-2022-KR", CharsetISO2022KR, "ISO-2022-KR"},
|
||||
{"charset is windows-1250", CharsetWindows1250, "windows-1250"},
|
||||
{"charset is windows-1251", CharsetWindows1251, "windows-1251"},
|
||||
{"charset is windows-1252", CharsetWindows1252, "windows-1252"},
|
||||
{"charset is windows-1255", CharsetWindows1255, "windows-1255"},
|
||||
{"charset is windows-1256", CharsetWindows1256, "windows-1256"},
|
||||
{"charset is KOI8-R", CharsetKOI8R, "KOI8-R"},
|
||||
{"charset is KOI8-U", CharsetKOI8U, "KOI8-U"},
|
||||
{"charset is Big5", CharsetBig5, "Big5"},
|
||||
{"charset is GB18030", CharsetGB18030, "GB18030"},
|
||||
{"charset is GB2312", CharsetGB2312, "GB2312"},
|
||||
{"charset is TIS-620", CharsetTIS620, "TIS-620"},
|
||||
{"charset is EUC-KR", CharsetEUCKR, "EUC-KR"},
|
||||
{"charset is Shift_JIS", CharsetShiftJIS, "Shift_JIS"},
|
||||
{"charset is GBK", CharsetGBK, "GBK"},
|
||||
{"charset is Unknown", CharsetUnknown, "Unknown"},
|
||||
}
|
||||
t.Run("SetCharset on new message", func(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
for _, tt := range charsetTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
if message == nil {
|
||||
|
@ -309,18 +266,8 @@ func TestMsg_SetCharset(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestMsg_SetEncoding(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value Encoding
|
||||
want Encoding
|
||||
}{
|
||||
{"encoding is Quoted-Printable", EncodingQP, "quoted-printable"},
|
||||
{"encoding is Base64", EncodingB64, "base64"},
|
||||
{"encoding is Unencoded 8-Bit", NoEncoding, "8bit"},
|
||||
{"encoding is US-ASCII 7-Bit", EncodingUSASCII, "7bit"},
|
||||
}
|
||||
t.Run("SetEncoding on new message", func(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
for _, tt := range encodingTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
if message == nil {
|
||||
|
@ -350,15 +297,8 @@ func TestMsg_SetEncoding(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestMsg_SetBoundary(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value string
|
||||
}{
|
||||
{"boundary: test123", "test123"},
|
||||
{"boundary is empty", ""},
|
||||
}
|
||||
t.Run("SetBoundary on new message", func(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
for _, tt := range boundaryTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
if message == nil {
|
||||
|
@ -387,6 +327,68 @@ func TestMsg_SetBoundary(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestMsg_SetMIMEVersion(t *testing.T) {
|
||||
t.Run("SetMIMEVersion on new message", func(t *testing.T) {
|
||||
for _, tt := range mimeTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
if message == nil {
|
||||
t.Fatal("message is nil")
|
||||
}
|
||||
message.SetMIMEVersion(tt.value)
|
||||
if message.mimever != tt.value {
|
||||
t.Errorf("failed to set mime version. Expected: %s, got: %s", tt.value, message.mimever)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
t.Run("SetMIMEVersion to override WithMIMEVersion", func(t *testing.T) {
|
||||
message := NewMsg(WithMIMEVersion("1.1"))
|
||||
if message == nil {
|
||||
t.Fatal("message is nil")
|
||||
}
|
||||
if message.mimever != "1.1" {
|
||||
t.Errorf("failed to set mime version on message creation. Expected: %s, got: %s", "1.1",
|
||||
message.mimever)
|
||||
}
|
||||
message.SetMIMEVersion(MIME10)
|
||||
if message.mimever != MIME10 {
|
||||
t.Errorf("failed to set mime version. Expected: %s, got: %s", MIME10, message.mimever)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestMsg_SetPGPType(t *testing.T) {
|
||||
t.Run("SetPGPType on new message", func(t *testing.T) {
|
||||
for _, tt := range pgpTests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
if message == nil {
|
||||
t.Fatal("message is nil")
|
||||
}
|
||||
message.SetPGPType(tt.value)
|
||||
if message.pgptype != tt.value {
|
||||
t.Errorf("failed to set pgp type. Expected: %d, got: %d", tt.value, message.pgptype)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
t.Run("SetPGPType to override WithPGPType", func(t *testing.T) {
|
||||
message := NewMsg(WithPGPType(PGPSignature))
|
||||
if message == nil {
|
||||
t.Fatal("message is nil")
|
||||
}
|
||||
if message.pgptype != PGPSignature {
|
||||
t.Errorf("failed to set pgp type on message creation. Expected: %d, got: %d", PGPSignature,
|
||||
message.pgptype)
|
||||
}
|
||||
message.SetPGPType(PGPEncrypt)
|
||||
if message.pgptype != PGPEncrypt {
|
||||
t.Errorf("failed to set pgp type. Expected: %d, got: %d", PGPEncrypt, message.pgptype)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
// TestNewMsgWithMiddleware tests WithMiddleware
|
||||
|
||||
|
|
Loading…
Reference in a new issue