45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package mailx_test
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
mailx "code.yun.ink/pkg/mailx"
|
||
|
|
)
|
||
|
|
|
||
|
|
// recordingLogger 记录日志调用的最小实现
|
||
|
|
type recordingLogger struct{}
|
||
|
|
|
||
|
|
func (recordingLogger) Debugf(context.Context, string, ...any) {}
|
||
|
|
func (recordingLogger) Infof(context.Context, string, ...any) {}
|
||
|
|
func (recordingLogger) Warnf(context.Context, string, ...any) {}
|
||
|
|
func (recordingLogger) Errorf(context.Context, string, ...any) {}
|
||
|
|
|
||
|
|
var _ mailx.Logger = recordingLogger{}
|
||
|
|
|
||
|
|
func TestLoggerRoundTrip(t *testing.T) {
|
||
|
|
ctx := mailx.WithLogger(context.Background(), recordingLogger{})
|
||
|
|
l := mailx.LoggerFromContext(ctx)
|
||
|
|
if _, ok := l.(recordingLogger); !ok {
|
||
|
|
t.Fatalf("logger type = %T, want recordingLogger", l)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLoggerDefaultNotNil(t *testing.T) {
|
||
|
|
l := mailx.LoggerFromContext(context.Background())
|
||
|
|
if l == nil {
|
||
|
|
t.Fatal("default logger should not be nil")
|
||
|
|
}
|
||
|
|
// 不应 panic
|
||
|
|
l.Debugf(context.Background(), "noop")
|
||
|
|
l.Infof(context.Background(), "noop")
|
||
|
|
l.Warnf(context.Background(), "noop")
|
||
|
|
l.Errorf(context.Background(), "noop")
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSetLoggerNilIgnored(t *testing.T) {
|
||
|
|
m := mailx.NewManager()
|
||
|
|
m.SetLogger(nil) // 不应 panic
|
||
|
|
m.SetLogger(recordingLogger{})
|
||
|
|
}
|