From 66e25d82d310b7bc176da3d5192e10bade112956 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 27 Oct 2024 10:09:25 +0100 Subject: [PATCH] Add test for GetAttachments method with no attachment Introduced a new test case to check `GetAttachments` when no attachments are present in the message object. Removed the redundant `TestMsg_GetAttachments` function to simplify the test suite. --- msg_test.go | 57 ++++++++++------------------------------------------- 1 file changed, 10 insertions(+), 47 deletions(-) diff --git a/msg_test.go b/msg_test.go index 3f0d065..934c36e 100644 --- a/msg_test.go +++ b/msg_test.go @@ -3276,6 +3276,16 @@ func TestMsg_GetAttachments(t *testing.T) { messageBuf.String()) } }) + t.Run("GetAttachments with no attachment", func(t *testing.T) { + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + attachments := message.GetAttachments() + if len(attachments) != 0 { + t.Fatalf("GetAttachments: expected 1 attachment, got: %d", len(attachments)) + } + }) } // checkAddrHeader verifies the correctness of an AddrHeader in a Msg based on the provided criteria. @@ -3488,53 +3498,6 @@ func checkGenHeader(t *testing.T, message *Msg, header Header, fn string, field, } } -// TestMsg_GetAttachments tests the Msg.GetAttachments method - - func TestMsg_GetAttachments(t *testing.T) { - tests := []struct { - name string - files []string - }{ - {"File: README.md", []string{"README.md"}}, - {"File: doc.go", []string{"doc.go"}}, - {"File: README.md and doc.go", []string{"README.md", "doc.go"}}, - {"File: nonexisting", nil}, - } - m := NewMsg() - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - for _, f := range tt.files { - m.AttachFile(f, WithFileName(f), nil) - } - if len(m.attachments) != len(tt.files) { - t.Errorf("AttachFile() failed. Number of attachments expected: %d, got: %d", len(tt.files), - len(m.attachments)) - return - } - ff := m.GetAttachments() - if len(m.attachments) != len(ff) { - t.Errorf("GetAttachments() failed. Number of attachments expected: %d, got: %d", len(m.attachments), - len(ff)) - return - } - var fn []string - for _, f := range ff { - fn = append(fn, f.Name) - } - sort.Strings(fn) - sort.Strings(tt.files) - for i, f := range tt.files { - if f != fn[i] { - t.Errorf("GetAttachments() failed. Attachment name expected: %s, got: %s", f, - fn[i]) - return - } - } - m.Reset() - }) - } - } - // TestMsg_SetAttachments tests the Msg.GetAttachments method func TestMsg_SetAttachments(t *testing.T) {