From 9834c6508d39b985fa8faf9b5d432883ec7aa40c Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Thu, 24 Oct 2024 17:09:55 +0200 Subject: [PATCH] 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. --- file_test.go | 252 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 153 insertions(+), 99 deletions(-) diff --git a/file_test.go b/file_test.go index 43b8cfe..9e23db5 100644 --- a/file_test.go +++ b/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) { }) } } + + +*/