Better test coverage for senderror.go

This commit is contained in:
Winni Neessen 2023-01-02 21:55:37 +01:00
parent b8ee25f014
commit 8d86c3d8b2
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
2 changed files with 31 additions and 1 deletions

View file

@ -68,7 +68,7 @@ type SendErrReason int
// Error implements the error interface for the SendError type
func (e *SendError) Error() string {
if e.Reason > 10 {
return "client_send: unknown error"
return "unknown reason"
}
var em strings.Builder

View file

@ -6,6 +6,8 @@ package mail
import (
"errors"
"fmt"
"strings"
"testing"
)
@ -36,6 +38,8 @@ func TestSendError_Error(t *testing.T) {
{"ErrConnCheck/perm", ErrConnCheck, false},
{"ErrNoUnencoded/temp", ErrNoUnencoded, true},
{"ErrNoUnencoded/perm", ErrNoUnencoded, false},
{"ErrAmbiguous/temp", ErrAmbiguous, true},
{"ErrAmbiguous/perm", ErrAmbiguous, false},
{"Unknown/temp", 9999, true},
{"Unknown/perm", 9999, false},
}
@ -48,11 +52,37 @@ func TestSendError_Error(t *testing.T) {
t.Errorf("error mismatch, expected: %s (temp: %t), got: %s (temp: %t)", tt.r, tt.te,
exp.Error(), exp.isTemp)
}
if !strings.Contains(fmt.Sprintf("%s", err), tt.r.String()) {
t.Errorf("error string mismatch, expected: %s, got: %s",
tt.r.String(), fmt.Sprintf("%s", err))
}
}
})
}
}
func TestSendError_IsTemp(t *testing.T) {
var se *SendError
err1 := returnSendError(ErrAmbiguous, true)
if !errors.As(err1, &se) {
t.Errorf("error mismatch, expected error to be of type *SendError")
return
}
if errors.As(err1, &se) && !se.IsTemp() {
t.Errorf("error mismatch, expected temporary error")
return
}
err2 := returnSendError(ErrAmbiguous, false)
if !errors.As(err2, &se) {
t.Errorf("error mismatch, expected error to be of type *SendError")
return
}
if errors.As(err2, &se) && se.IsTemp() {
t.Errorf("error mismatch, expected non-temporary error")
return
}
}
// returnSendError is a helper method to retunr a SendError with a specific reason
func returnSendError(r SendErrReason, t bool) error {
return &SendError{Reason: r, isTemp: t}