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:
Winni Neessen 2024-10-24 17:09:55 +02:00
parent 75e035c783
commit 9834c6508d
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -6,108 +6,159 @@ 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 len(contentType) != 1 {
if fi != "text/plain" { t.Fatalf("setHeader failed. Expected header %s to have one value, got: %d", HeaderContentType,
t.Errorf("getHeader returned wrong value. Expected: %s, got: %s", "text/plain", fi) 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.Run("getHeader", func(t *testing.T) {
t.Errorf("getHeader returned wrong value. Expected: %s, got: %s", "", fi) 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 // TestFile_WithFileContentType tests the WithFileContentType option
func TestFile_WithFileContentType(t *testing.T) { func TestFile_WithFileContentType(t *testing.T) {
@ -137,3 +188,6 @@ func TestFile_WithFileContentType(t *testing.T) {
}) })
} }
} }
*/