More tests. 50% overall coverage now

This commit is contained in:
Winni Neessen 2022-03-18 15:05:33 +01:00
parent c698312133
commit e909bd5499
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
3 changed files with 67 additions and 3 deletions

View file

@ -90,7 +90,7 @@ func TestNewClientWithOptions(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
c, err := NewClient(DefaultHost, tt.option) c, err := NewClient(DefaultHost, tt.option, nil)
if err != nil && !tt.shouldfail { if err != nil && !tt.shouldfail {
t.Errorf("failed to create new client: %s", err) t.Errorf("failed to create new client: %s", err)
return return

View file

@ -3,6 +3,7 @@ package mail
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io"
"net/mail" "net/mail"
"testing" "testing"
"time" "time"
@ -68,7 +69,7 @@ func TestNewMsgCharset(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
m := NewMsg(WithCharset(tt.value)) m := NewMsg(WithCharset(tt.value), nil)
if m.charset != tt.want { if m.charset != tt.want {
t.Errorf("WithCharset() failed. Expected: %s, got: %s", tt.want, m.charset) t.Errorf("WithCharset() failed. Expected: %s, got: %s", tt.want, m.charset)
} }
@ -914,7 +915,7 @@ func TestMsg_AttachFile(t *testing.T) {
m := NewMsg() m := NewMsg()
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
m.AttachFile(tt.file, WithFileName(tt.fn)) m.AttachFile(tt.file, WithFileName(tt.fn), nil)
if len(m.attachments) != 1 && !tt.sf { if len(m.attachments) != 1 && !tt.sf {
t.Errorf("AttachFile() failed. Number of attachments expected: %d, got: %d", 1, t.Errorf("AttachFile() failed. Number of attachments expected: %d, got: %d", 1,
len(m.attachments)) len(m.attachments))
@ -930,8 +931,36 @@ func TestMsg_AttachFile(t *testing.T) {
t.Errorf("AttachFile() failed. Filename of attachment expected: %s, got: %s", tt.fn, t.Errorf("AttachFile() failed. Filename of attachment expected: %s, got: %s", tt.fn,
file.Name) file.Name)
} }
buf := bytes.Buffer{}
if err := file.Writer(&buf); err != nil {
t.Errorf("failed to execute WriterFunc: %s", err)
return
}
} }
m.Reset() m.Reset()
}) })
} }
} }
// TestMsg_AttachFileBrokenFunc tests WriterFunc of the Msg.AttachFile method
func TestMsg_AttachFileBrokenFunc(t *testing.T) {
m := NewMsg()
m.AttachFile("README.md")
if len(m.attachments) != 1 {
t.Errorf("AttachFile() failed. Number of attachments expected: %d, got: %d", 1,
len(m.attachments))
return
}
file := m.attachments[0]
if file == nil {
t.Errorf("AttachFile() failed. Attachment file pointer is nil")
return
}
file.Writer = func(io.Writer) error {
return fmt.Errorf("failing intentionally")
}
buf := bytes.Buffer{}
if err := file.Writer(&buf); err == nil {
t.Errorf("execute WriterFunc did not fail, but was expected to fail")
}
}

35
part_test.go Normal file
View file

@ -0,0 +1,35 @@
package mail
import "testing"
// TestPartEncoding tests the WithPartEncoding and Part.SetEncoding methods
func TestPartEncoding(t *testing.T) {
tests := []struct {
name string
enc Encoding
want string
}{
{"Part encoding: Base64", EncodingB64, "base64"},
{"Part encoding: Quoted-Printable", EncodingQP, "quoted-printable"},
{"Part encoding: 8bit", NoEncoding, "8bit"},
}
for _, tt := range tests {
m := NewMsg()
t.Run(tt.name, func(t *testing.T) {
part := m.newPart(TypeTextPlain, WithPartEncoding(tt.enc), nil)
if part == nil {
t.Errorf("newPart() WithPartEncoding() failed: no part returned")
}
if part.enc.String() != tt.want {
t.Errorf("newPart() WithPartEncoding() failed: expected encoding: %s, got: %s", tt.want,
part.enc.String())
}
part.enc = ""
part.SetEncoding(tt.enc)
if part.enc.String() != tt.want {
t.Errorf("newPart() SetEncoding() failed: expected encoding: %s, got: %s", tt.want,
part.enc.String())
}
})
}
}