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.
This commit is contained in:
Winni Neessen 2024-10-27 10:09:25 +01:00
parent babf7b9780
commit 66e25d82d3
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -3276,6 +3276,16 @@ func TestMsg_GetAttachments(t *testing.T) {
messageBuf.String()) 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. // 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 // TestMsg_SetAttachments tests the Msg.GetAttachments method
func TestMsg_SetAttachments(t *testing.T) { func TestMsg_SetAttachments(t *testing.T) {