40 lines
738 B
Go
40 lines
738 B
Go
// +build !binary_log
|
|
|
|
package zerolog
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type nilError struct{}
|
|
|
|
func (nilError) Error() string {
|
|
return ""
|
|
}
|
|
|
|
func TestEvent_AnErr(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
err error
|
|
want string
|
|
}{
|
|
{"nil", nil, `{}`},
|
|
{"error", errors.New("test"), `{"err":"test"}`},
|
|
{"nil interface", func() *nilError { return nil }(), `{}`},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
|
|
e.AnErr("err", tt.err)
|
|
_ = e.write()
|
|
if got, want := strings.TrimSpace(buf.String()), tt.want; got != want {
|
|
t.Errorf("Event.AnErr() = %v, want %v", got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|