mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 05:40:50 +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
252
file_test.go
252
file_test.go
|
@ -6,108 +6,159 @@ package mail
|
|||
|
||||
import "testing"
|
||||
|
||||
// TestFile_SetGetHeader tests the set-/getHeader method of the File object
|
||||
func TestFile_SetGetHeader(t *testing.T) {
|
||||
f := File{
|
||||
Name: "testfile.txt",
|
||||
Header: make(map[string][]string),
|
||||
}
|
||||
f.setHeader(HeaderContentType, "text/plain")
|
||||
fi, ok := f.getHeader(HeaderContentType)
|
||||
if !ok {
|
||||
t.Errorf("getHeader method of File did not return a value")
|
||||
return
|
||||
}
|
||||
if fi != "text/plain" {
|
||||
t.Errorf("getHeader returned wrong value. Expected: %s, got: %s", "text/plain", fi)
|
||||
}
|
||||
fi, ok = f.getHeader(HeaderContentTransferEnc)
|
||||
if ok {
|
||||
t.Errorf("getHeader method of File did return a value, but wasn't supposed to")
|
||||
return
|
||||
}
|
||||
if fi != "" {
|
||||
t.Errorf("getHeader returned wrong value. Expected: %s, got: %s", "", fi)
|
||||
}
|
||||
func TestFile(t *testing.T) {
|
||||
t.Run("setHeader", func(t *testing.T) {
|
||||
f := File{
|
||||
Name: "testfile.txt",
|
||||
Header: make(map[string][]string),
|
||||
}
|
||||
f.setHeader(HeaderContentType, "text/plain")
|
||||
contentType, ok := f.Header[HeaderContentType.String()]
|
||||
if !ok {
|
||||
t.Fatalf("setHeader failed. Expected header %s to be set", HeaderContentType)
|
||||
}
|
||||
if len(contentType) != 1 {
|
||||
t.Fatalf("setHeader failed. Expected header %s to have one value, got: %d", HeaderContentType,
|
||||
len(contentType))
|
||||
}
|
||||
if contentType[0] != "text/plain" {
|
||||
t.Fatalf("setHeader failed. Expected header %s to have value %s, got: %s",
|
||||
HeaderContentType.String(), "text/plain", contentType[0])
|
||||
}
|
||||
})
|
||||
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)
|
||||
if !ok {
|
||||
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 {
|
||||
name string
|
||||
desc string
|
||||
}{
|
||||
{"File description: test", "test"},
|
||||
{"File description: with newline", "test\n"},
|
||||
{"File description: empty", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
message.AttachFile("file.go", WithFileDescription(tt.desc))
|
||||
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.Desc != tt.desc {
|
||||
t.Errorf("WithFileDescription() failed. Expected: %s, got: %s", tt.desc,
|
||||
firstAttachment.Desc)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
t.Run("WithFileContentID", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
id string
|
||||
}{
|
||||
{"Content-ID: test", "test"},
|
||||
{"Content-ID: with newline", "test\n"},
|
||||
{"Content-ID: empty", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
message.AttachFile("file.go", WithFileContentID(tt.id))
|
||||
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")
|
||||
}
|
||||
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) {
|
||||
tests := []struct {
|
||||
name string
|
||||
encoding Encoding
|
||||
want Encoding
|
||||
}{
|
||||
{"File encoding: US-ASCII", EncodingUSASCII, EncodingUSASCII},
|
||||
{"File encoding: 8bit raw", NoEncoding, NoEncoding},
|
||||
{"File encoding: Base64", EncodingB64, EncodingB64},
|
||||
{"File encoding: quoted-printable (not allowed)", EncodingQP, ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message := NewMsg()
|
||||
message.AttachFile("file.go", WithFileEncoding(tt.encoding))
|
||||
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.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_WithFileDescription tests the WithFileDescription option
|
||||
func TestFile_WithFileDescription(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
desc string
|
||||
}{
|
||||
{"File description: test", "test"},
|
||||
{"File description: empty", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
m := NewMsg()
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m.AttachFile("file.go", WithFileDescription(tt.desc))
|
||||
al := m.GetAttachments()
|
||||
if len(al) <= 0 {
|
||||
t.Errorf("AttachFile() failed. Attachment list is empty")
|
||||
}
|
||||
a := al[0]
|
||||
if a.Desc != tt.desc {
|
||||
t.Errorf("WithFileDescription() failed. Expected: %s, got: %s", tt.desc, a.Desc)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
||||
// TestFile_WithContentID tests the WithFileContentID option
|
||||
func TestFile_WithContentID(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
contentid string
|
||||
}{
|
||||
{"File Content-ID: test", "test"},
|
||||
{"File Content-ID: empty", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
m := NewMsg()
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m.AttachFile("file.go", WithFileContentID(tt.contentid))
|
||||
al := m.GetAttachments()
|
||||
if len(al) <= 0 {
|
||||
t.Errorf("AttachFile() failed. Attachment list is empty")
|
||||
}
|
||||
a := al[0]
|
||||
if a.Header.Get(HeaderContentID.String()) != tt.contentid {
|
||||
t.Errorf("WithFileContentID() failed. Expected: %s, got: %s", tt.contentid,
|
||||
a.Header.Get(HeaderContentID.String()))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFile_WithFileEncoding tests the WithFileEncoding option
|
||||
func TestFile_WithFileEncoding(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
enc Encoding
|
||||
want Encoding
|
||||
}{
|
||||
{"File encoding: 8bit raw", NoEncoding, NoEncoding},
|
||||
{"File encoding: Base64", EncodingB64, EncodingB64},
|
||||
{"File encoding: quoted-printable (not allowed)", EncodingQP, ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
m := NewMsg()
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m.AttachFile("file.go", WithFileEncoding(tt.enc))
|
||||
al := m.GetAttachments()
|
||||
if len(al) <= 0 {
|
||||
t.Errorf("AttachFile() failed. Attachment list is empty")
|
||||
}
|
||||
a := al[0]
|
||||
if a.Enc != tt.want {
|
||||
t.Errorf("WithFileEncoding() failed. Expected: %s, got: %s", tt.enc, a.Enc)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFile_WithFileContentType tests the WithFileContentType option
|
||||
func TestFile_WithFileContentType(t *testing.T) {
|
||||
|
@ -137,3 +188,6 @@ func TestFile_WithFileContentType(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue