mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 13:50:49 +01:00
Refactor file_test.go to use subtests
Consolidate multiple test functions into a single TestFile function using subtests. This improves test organization and enhances readability. Added a new test case for testing file name attachment.
This commit is contained in:
parent
75e035c783
commit
9834c6508d
1 changed files with 153 additions and 99 deletions
162
file_test.go
162
file_test.go
|
@ -6,109 +6,160 @@ package mail
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
// TestFile_SetGetHeader tests the set-/getHeader method of the File object
|
func TestFile(t *testing.T) {
|
||||||
func TestFile_SetGetHeader(t *testing.T) {
|
t.Run("setHeader", func(t *testing.T) {
|
||||||
f := File{
|
f := File{
|
||||||
Name: "testfile.txt",
|
Name: "testfile.txt",
|
||||||
Header: make(map[string][]string),
|
Header: make(map[string][]string),
|
||||||
}
|
}
|
||||||
f.setHeader(HeaderContentType, "text/plain")
|
f.setHeader(HeaderContentType, "text/plain")
|
||||||
fi, ok := f.getHeader(HeaderContentType)
|
contentType, ok := f.Header[HeaderContentType.String()]
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Errorf("getHeader method of File did not return a value")
|
t.Fatalf("setHeader failed. Expected header %s to be set", HeaderContentType)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if fi != "text/plain" {
|
if len(contentType) != 1 {
|
||||||
t.Errorf("getHeader returned wrong value. Expected: %s, got: %s", "text/plain", fi)
|
t.Fatalf("setHeader failed. Expected header %s to have one value, got: %d", HeaderContentType,
|
||||||
|
len(contentType))
|
||||||
}
|
}
|
||||||
fi, ok = f.getHeader(HeaderContentTransferEnc)
|
if contentType[0] != "text/plain" {
|
||||||
if ok {
|
t.Fatalf("setHeader failed. Expected header %s to have value %s, got: %s",
|
||||||
t.Errorf("getHeader method of File did return a value, but wasn't supposed to")
|
HeaderContentType.String(), "text/plain", contentType[0])
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if fi != "" {
|
})
|
||||||
t.Errorf("getHeader returned wrong value. Expected: %s, got: %s", "", fi)
|
t.Run("getHeader", func(t *testing.T) {
|
||||||
|
f := File{
|
||||||
|
Name: "testfile.txt",
|
||||||
|
Header: make(map[string][]string),
|
||||||
}
|
}
|
||||||
}
|
f.setHeader(HeaderContentType, "text/plain")
|
||||||
|
contentType, ok := f.getHeader(HeaderContentType)
|
||||||
// TestFile_WithFileDescription tests the WithFileDescription option
|
if !ok {
|
||||||
func TestFile_WithFileDescription(t *testing.T) {
|
t.Fatalf("setHeader failed. Expected header %s to be set", HeaderContentType)
|
||||||
|
}
|
||||||
|
if contentType != "text/plain" {
|
||||||
|
t.Fatalf("setHeader failed. Expected header %s to have value %s, got: %s",
|
||||||
|
HeaderContentType.String(), "text/plain", contentType)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("WithFileDescription", func(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
desc string
|
desc string
|
||||||
}{
|
}{
|
||||||
{"File description: test", "test"},
|
{"File description: test", "test"},
|
||||||
|
{"File description: with newline", "test\n"},
|
||||||
{"File description: empty", ""},
|
{"File description: empty", ""},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
m := NewMsg()
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
m.AttachFile("file.go", WithFileDescription(tt.desc))
|
message := NewMsg()
|
||||||
al := m.GetAttachments()
|
message.AttachFile("file.go", WithFileDescription(tt.desc))
|
||||||
if len(al) <= 0 {
|
attachments := message.GetAttachments()
|
||||||
t.Errorf("AttachFile() failed. Attachment list is empty")
|
if len(attachments) <= 0 {
|
||||||
|
t.Fatalf("failed to retrieve attachments list")
|
||||||
}
|
}
|
||||||
a := al[0]
|
firstAttachment := attachments[0]
|
||||||
if a.Desc != tt.desc {
|
if firstAttachment == nil {
|
||||||
t.Errorf("WithFileDescription() failed. Expected: %s, got: %s", tt.desc, a.Desc)
|
t.Fatalf("failed to retrieve first attachment, got nil")
|
||||||
|
}
|
||||||
|
if firstAttachment.Desc != tt.desc {
|
||||||
|
t.Errorf("WithFileDescription() failed. Expected: %s, got: %s", tt.desc,
|
||||||
|
firstAttachment.Desc)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
t.Run("WithFileContentID", func(t *testing.T) {
|
||||||
// TestFile_WithContentID tests the WithFileContentID option
|
|
||||||
func TestFile_WithContentID(t *testing.T) {
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
contentid string
|
id string
|
||||||
}{
|
}{
|
||||||
{"File Content-ID: test", "test"},
|
{"Content-ID: test", "test"},
|
||||||
{"File Content-ID: empty", ""},
|
{"Content-ID: with newline", "test\n"},
|
||||||
|
{"Content-ID: empty", ""},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
m := NewMsg()
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
m.AttachFile("file.go", WithFileContentID(tt.contentid))
|
message := NewMsg()
|
||||||
al := m.GetAttachments()
|
message.AttachFile("file.go", WithFileContentID(tt.id))
|
||||||
if len(al) <= 0 {
|
attachments := message.GetAttachments()
|
||||||
t.Errorf("AttachFile() failed. Attachment list is empty")
|
if len(attachments) <= 0 {
|
||||||
|
t.Fatalf("failed to retrieve attachments list")
|
||||||
}
|
}
|
||||||
a := al[0]
|
firstAttachment := attachments[0]
|
||||||
if a.Header.Get(HeaderContentID.String()) != tt.contentid {
|
if firstAttachment == nil {
|
||||||
t.Errorf("WithFileContentID() failed. Expected: %s, got: %s", tt.contentid,
|
t.Fatalf("failed to retrieve first attachment, got nil")
|
||||||
a.Header.Get(HeaderContentID.String()))
|
}
|
||||||
|
contentID := firstAttachment.Header.Get(HeaderContentID.String())
|
||||||
|
if contentID != tt.id {
|
||||||
|
t.Errorf("WithFileContentID() failed. Expected: %s, got: %s", tt.id,
|
||||||
|
contentID)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
t.Run("WithFileEncoding", func(t *testing.T) {
|
||||||
// TestFile_WithFileEncoding tests the WithFileEncoding option
|
|
||||||
func TestFile_WithFileEncoding(t *testing.T) {
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
enc Encoding
|
encoding Encoding
|
||||||
want Encoding
|
want Encoding
|
||||||
}{
|
}{
|
||||||
|
{"File encoding: US-ASCII", EncodingUSASCII, EncodingUSASCII},
|
||||||
{"File encoding: 8bit raw", NoEncoding, NoEncoding},
|
{"File encoding: 8bit raw", NoEncoding, NoEncoding},
|
||||||
{"File encoding: Base64", EncodingB64, EncodingB64},
|
{"File encoding: Base64", EncodingB64, EncodingB64},
|
||||||
{"File encoding: quoted-printable (not allowed)", EncodingQP, ""},
|
{"File encoding: quoted-printable (not allowed)", EncodingQP, ""},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
m := NewMsg()
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
m.AttachFile("file.go", WithFileEncoding(tt.enc))
|
message := NewMsg()
|
||||||
al := m.GetAttachments()
|
message.AttachFile("file.go", WithFileEncoding(tt.encoding))
|
||||||
if len(al) <= 0 {
|
attachments := message.GetAttachments()
|
||||||
t.Errorf("AttachFile() failed. Attachment list is empty")
|
if len(attachments) <= 0 {
|
||||||
|
t.Fatalf("failed to retrieve attachments list")
|
||||||
}
|
}
|
||||||
a := al[0]
|
firstAttachment := attachments[0]
|
||||||
if a.Enc != tt.want {
|
if firstAttachment == nil {
|
||||||
t.Errorf("WithFileEncoding() failed. Expected: %s, got: %s", tt.enc, a.Enc)
|
t.Fatalf("failed to retrieve first attachment, got nil")
|
||||||
|
}
|
||||||
|
if firstAttachment.Enc != tt.want {
|
||||||
|
t.Errorf("WithFileEncoding() failed. Expected: %s, got: %s", tt.want, firstAttachment.Enc)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
t.Run("WithFileName", func(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fileName string
|
||||||
|
}{
|
||||||
|
{"File name: test", "test"},
|
||||||
|
{"File name: with newline", "test\n"},
|
||||||
|
{"File name: empty", ""},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
message := NewMsg()
|
||||||
|
message.AttachFile("file.go", WithFileName(tt.fileName))
|
||||||
|
attachments := message.GetAttachments()
|
||||||
|
if len(attachments) <= 0 {
|
||||||
|
t.Fatalf("failed to retrieve attachments list")
|
||||||
|
}
|
||||||
|
firstAttachment := attachments[0]
|
||||||
|
if firstAttachment == nil {
|
||||||
|
t.Fatalf("failed to retrieve first attachment, got nil")
|
||||||
|
}
|
||||||
|
if firstAttachment.Name != tt.fileName {
|
||||||
|
t.Errorf("WithFileName() failed. Expected: %s, got: %s", tt.fileName,
|
||||||
|
firstAttachment.Name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
|
||||||
// TestFile_WithFileContentType tests the WithFileContentType option
|
// TestFile_WithFileContentType tests the WithFileContentType option
|
||||||
func TestFile_WithFileContentType(t *testing.T) {
|
func TestFile_WithFileContentType(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
@ -137,3 +188,6 @@ func TestFile_WithFileContentType(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
Loading…
Reference in a new issue