2020-02-11 01:14:39 +00:00
|
|
|
// +build !binary_log
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
package zlog
|
2020-02-11 01:14:39 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 15:24:18 +00:00
|
|
|
|
|
|
|
func TestEvent_ObjectWithNil(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
|
|
|
|
_ = e.Object("obj", nil)
|
|
|
|
_ = e.write()
|
|
|
|
|
|
|
|
want := `{"obj":null}`
|
|
|
|
got := strings.TrimSpace(buf.String())
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Event.Object() = %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvent_EmbedObjectWithNil(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
|
|
|
|
_ = e.EmbedObject(nil)
|
|
|
|
_ = e.write()
|
|
|
|
|
|
|
|
want := "{}"
|
|
|
|
got := strings.TrimSpace(buf.String())
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Event.EmbedObject() = %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|