From babf7b9780448500a036cf52fc42a416b5480277 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 27 Oct 2024 10:07:05 +0100 Subject: [PATCH] Add tests for GetAttachments method in Msg This commit introduces unit tests for the GetAttachments method in the Msg struct to ensure it correctly handles both single and multiple attachments. The tests verify the name and content of the attachments to validate the expected behavior. --- msg_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/msg_test.go b/msg_test.go index 061614e..3f0d065 100644 --- a/msg_test.go +++ b/msg_test.go @@ -3207,6 +3207,77 @@ func TestMsg_GetParts(t *testing.T) { }) } +func TestMsg_GetAttachments(t *testing.T) { + t.Run("GetAttachments with single attachment", func(t *testing.T) { + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + message.AttachFile("testdata/attachment.txt") + attachments := message.GetAttachments() + if len(attachments) != 1 { + t.Fatalf("GetAttachments: expected 1 attachment, got: %d", len(attachments)) + } + if attachments[0] == nil { + t.Fatalf("GetAttachments: expected attachment, got nil") + } + if attachments[0].Name != "attachment.txt" { + t.Errorf("GetAttachments: expected attachment name to be %s, got: %s", "attachment.txt", + attachments[0].Name) + } + messageBuf := bytes.NewBuffer(nil) + _, err := attachments[0].Writer(messageBuf) + if err != nil { + t.Errorf("GetAttachments: Writer func failed: %s", err) + } + if !strings.EqualFold(messageBuf.String(), "This is a test attachment\n") { + t.Errorf("GetParts: expected message body to be %s, got: %s", "This is a test attachment\n", + messageBuf.String()) + } + }) + t.Run("GetAttachments with multiple attachments", func(t *testing.T) { + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + message.AttachFile("testdata/attachment.txt") + message.AttachFile("testdata/attachment.txt", WithFileName("attachment2.txt")) + attachments := message.GetAttachments() + if len(attachments) != 2 { + t.Fatalf("GetAttachments: expected 2 attachment, got: %d", len(attachments)) + } + if attachments[0] == nil || attachments[1] == nil { + t.Fatalf("GetAttachments: expected attachment, got nil") + } + if attachments[0].Name != "attachment.txt" { + t.Errorf("GetAttachments: expected attachment name to be %s, got: %s", "attachment.txt", + attachments[0].Name) + } + if attachments[1].Name != "attachment2.txt" { + t.Errorf("GetAttachments: expected attachment name to be %s, got: %s", "attachment2.txt", + attachments[1].Name) + } + messageBuf := bytes.NewBuffer(nil) + _, err := attachments[0].Writer(messageBuf) + if err != nil { + t.Errorf("GetAttachments: Writer func failed: %s", err) + } + if !strings.EqualFold(messageBuf.String(), "This is a test attachment\n") { + t.Errorf("GetParts: expected message body to be %s, got: %s", "This is a test attachment\n", + messageBuf.String()) + } + messageBuf.Reset() + _, err = attachments[1].Writer(messageBuf) + if err != nil { + t.Errorf("GetAttachments: Writer func failed: %s", err) + } + if !strings.EqualFold(messageBuf.String(), "This is a test attachment\n") { + t.Errorf("GetParts: expected message body to be %s, got: %s", "This is a test attachment\n", + messageBuf.String()) + } + }) +} + // checkAddrHeader verifies the correctness of an AddrHeader in a Msg based on the provided criteria. // It checks whether the AddrHeader contains the correct address, name, and number of fields. func checkAddrHeader(t *testing.T, message *Msg, header AddrHeader, fn string, field, wantFields int,