mirror of
https://github.com/rs/zerolog
synced 2026-06-08 17:13:30 +00:00
8148645974
* Add array handling of IPAddr and IPPrefix Updated documentation, benchmark, and tests including catching up some missing. Cleaned up couple lint messages * Added cbor encoding for []net.IP and []net.IPNet * Fix binary_test Typos galore, thanks CI * Remove feral x character * Added IPAddrs and IPPrefixes tests for json encoder * Increase code coverage and test cleanup Added Type to the BenchmarkLogFieldType and BenchmarkContexdtFieldType test cases, also to log_test.go Moved the test-fixture data for them and also shared them with new event test for nil events. Added a couple strings for encodeByteTest for FF, CR, LF Added complete cbor testing for AppendInt*, AppendInts*, AppendUint*, and AppendUints* methods. Added test for float IsNaN and Inf(0) and Inf(-1). Added all min/max/smallest value tests for ints and floats Extended the Array test to include IPPrefix, MACAddr, Interface, and object marshaled. Better test of MarshalZerologObject. Added test for encoder AppendStrings and AppendStringers. * Remove merge detritus. * Remove code duplication when appending objects * Add missing Stringers support to Context * Add test cases for Err and Object in Array * Add missing benchmark for Events and Contexts Events: Any, Bytes, Hex, Float32, Floats32, Stringer, Stringers, Timestamp Context: Any, Bytes, Hex, Float32, Floats32, Stringers (also renamed Float,Floats to Float64,Floats64) * Add test of MarshalZeroLogObject of error * Build out Event test cases Test of nil Event - write - Array, Dict - Fields - Int8, Ints8, Int16, Ints16, Int32, Ints32, Int64, Ints64 - Uint8, Uints8, Uint16, Uints16, Uint32, Uints32, Uint64, Uints64 - RawCBOR, RawJSON, EmbedObject - Stringers - Caller, CallerSkip, Stack - Send, Msg, Msgf, MsgFunc - (renamed tests of Float, Floats to Float64, Floats64) Added test of MsgFunc * Add more tests - Dur (With) - Any (With, Fields, Fields map, Disabled) - Interface - Stack (Fields) Fix issue with testing a nil loggableError * Split out the With tests again type arrays Simplifies the "want" maintenance * Add missing test fixture data Bytes, Floats32, Ints8, Ints16, Ints32, Ints64, Uints8, Uints16, Uints32, Uints64, RawJSON, RawCBOR Stringers is now an array (not a single Stringer) (also renamed Float, Floats to Float64, Floats64) * Add examples for IPAddrs, IPPrefixes, Times Missing examples that increase coverage * Build out the complete ErrorMarshalFunc tests * Add AppendStrings to CBOR tests * Add CBOR AppendTimes, AppendDuration, AppendDurations tests * Add CBOR tests - BeginMarker, EndMarker, AppendArrayDim, AppendLineBreak - AppendObjectData - AppendIterface - AppendType - AppendHex - Empty arrays for - Bools - Floats32, Floats64 - Large arrays for - Bools, - Uints8, Uints16, Uints32, Uints64 - Floats32, Floats64 - IPAddrs, IPPrefixes - Small arrays for - Ints, Ints8, Ints16, Ints32, Ints64 - Uints, Uints32, Uints64 - Floats32, Floats64 * Add CBOR test for AppendKey * Added more string encoding tests Test escape character handling. * Fix test import cycle * Rename hex to hexCharacters to avoid namespace. * Remove leftover comment * Add test cases to share between CBOR and JSON tests * Switched CBOR to shared test cases * Add JSON test for AppendKey * Switch to shared test cases Added test for AppendStringer * Added JSON test - AppendNil - AppendBeginMarker, AppendEndMarker - AppendArrayStart, AppendArrayEnd - AppendArrayDelim - AppendLineBreak - AppendObjectData - AppendInterface - AppendBool, AppendBools * Add JSON time tests - AppendTime, AppendTimes - AppendTime (past/present) (integer/float) - AppendDuration, AppendDurations * Added JSON tests for plurals Split out the Int and Float tests into distinct files. Add - AppendInt - AppendInts8, AppendInts16, AppendInts32, AppendInts64, AppendInts - AppendUints8, AppendUints16, AppendUints32, AppendUints64, AppendUints - AppendFloats32, AppendFloats64
304 lines
6.7 KiB
Go
304 lines
6.7 KiB
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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEvent_writeWithNil(t *testing.T) {
|
|
var e *Event = nil
|
|
got := e.write()
|
|
|
|
var want *Event = nil
|
|
if got != nil {
|
|
t.Errorf("Event.write() = %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestEvent_WithNilEvent(t *testing.T) {
|
|
// coverage for nil Event receiver for all types
|
|
var e *Event = nil
|
|
|
|
fixtures := makeFieldFixtures()
|
|
types := map[string]func() *Event{
|
|
"Array": func() *Event {
|
|
arr := Arr()
|
|
return e.Array("k", arr)
|
|
},
|
|
"Bool": func() *Event {
|
|
return e.Bool("k", fixtures.Bools[0])
|
|
},
|
|
"Bools": func() *Event {
|
|
return e.Bools("k", fixtures.Bools)
|
|
},
|
|
"Fields": func() *Event {
|
|
return e.Fields(fixtures)
|
|
},
|
|
"Int": func() *Event {
|
|
return e.Int("k", fixtures.Ints[0])
|
|
},
|
|
"Ints": func() *Event {
|
|
return e.Ints("k", fixtures.Ints)
|
|
},
|
|
"Int8": func() *Event {
|
|
return e.Int8("k", fixtures.Ints8[0])
|
|
},
|
|
"Ints8": func() *Event {
|
|
return e.Ints8("k", fixtures.Ints8)
|
|
},
|
|
"Int16": func() *Event {
|
|
return e.Int16("k", fixtures.Ints16[0])
|
|
},
|
|
"Ints16": func() *Event {
|
|
return e.Ints16("k", fixtures.Ints16)
|
|
},
|
|
"Int32": func() *Event {
|
|
return e.Int32("k", fixtures.Ints32[0])
|
|
},
|
|
"Ints32": func() *Event {
|
|
return e.Ints32("k", fixtures.Ints32)
|
|
},
|
|
"Int64": func() *Event {
|
|
return e.Int64("k", fixtures.Ints64[0])
|
|
},
|
|
"Ints64": func() *Event {
|
|
return e.Ints64("k", fixtures.Ints64)
|
|
},
|
|
"Uint": func() *Event {
|
|
return e.Uint("k", fixtures.Uints[0])
|
|
},
|
|
"Uints": func() *Event {
|
|
return e.Uints("k", fixtures.Uints)
|
|
},
|
|
"Uint8": func() *Event {
|
|
return e.Uint8("k", fixtures.Uints8[0])
|
|
},
|
|
"Uints8": func() *Event {
|
|
return e.Uints8("k", fixtures.Uints8)
|
|
},
|
|
"Uint16": func() *Event {
|
|
return e.Uint16("k", fixtures.Uints16[0])
|
|
},
|
|
"Uints16": func() *Event {
|
|
return e.Uints16("k", fixtures.Uints16)
|
|
},
|
|
"Uint32": func() *Event {
|
|
return e.Uint32("k", fixtures.Uints32[0])
|
|
},
|
|
"Uints32": func() *Event {
|
|
return e.Uints32("k", fixtures.Uints32)
|
|
},
|
|
"Uint64": func() *Event {
|
|
return e.Uint64("k", fixtures.Uints64[0])
|
|
},
|
|
"Uints64": func() *Event {
|
|
return e.Uints64("k", fixtures.Uints64)
|
|
},
|
|
"Float64": func() *Event {
|
|
return e.Float64("k", fixtures.Floats64[0])
|
|
},
|
|
"Floats64": func() *Event {
|
|
return e.Floats64("k", fixtures.Floats64)
|
|
},
|
|
"Float32": func() *Event {
|
|
return e.Float32("k", fixtures.Floats32[0])
|
|
},
|
|
"Floats32": func() *Event {
|
|
return e.Floats32("k", fixtures.Floats32)
|
|
},
|
|
"RawCBOR": func() *Event {
|
|
return e.RawCBOR("k", fixtures.RawCBOR)
|
|
},
|
|
"RawJSON": func() *Event {
|
|
return e.RawJSON("k", fixtures.RawJSON)
|
|
},
|
|
"Str": func() *Event {
|
|
return e.Str("k", fixtures.Strings[0])
|
|
},
|
|
"Strs": func() *Event {
|
|
return e.Strs("k", fixtures.Strings)
|
|
},
|
|
"Stringers": func() *Event {
|
|
return e.Stringers("k", fixtures.Stringers)
|
|
},
|
|
"Err": func() *Event {
|
|
return e.Err(fixtures.Errs[0])
|
|
},
|
|
"Errs": func() *Event {
|
|
return e.Errs("k", fixtures.Errs)
|
|
},
|
|
"Ctx": func() *Event {
|
|
return e.Ctx(fixtures.Ctx)
|
|
},
|
|
"Time": func() *Event {
|
|
return e.Time("k", fixtures.Times[0])
|
|
},
|
|
"Times": func() *Event {
|
|
return e.Times("k", fixtures.Times)
|
|
},
|
|
"Dict": func() *Event {
|
|
d := Dict()
|
|
d.Str("greeting", "hello")
|
|
return e.Dict("k", d)
|
|
},
|
|
"Dur": func() *Event {
|
|
return e.Dur("k", fixtures.Durations[0])
|
|
},
|
|
"Durs": func() *Event {
|
|
return e.Durs("k", fixtures.Durations)
|
|
},
|
|
"Interface": func() *Event {
|
|
return e.Interface("k", fixtures.Interfaces[0])
|
|
},
|
|
"Interfaces": func() *Event {
|
|
return e.Interface("k", fixtures.Interfaces)
|
|
},
|
|
"Interface(Object)": func() *Event {
|
|
return e.Interface("k", fixtures.Objects[0])
|
|
},
|
|
"Interface(Objects)": func() *Event {
|
|
return e.Interface("k", fixtures.Objects)
|
|
},
|
|
"Object": func() *Event {
|
|
return e.Object("k", fixtures.Objects[0])
|
|
},
|
|
"EmbedObject": func() *Event {
|
|
return e.EmbedObject(fixtures.Objects[0])
|
|
},
|
|
"Timestamp": func() *Event {
|
|
return e.Timestamp()
|
|
},
|
|
"IPAddr": func() *Event {
|
|
return e.IPAddr("k", fixtures.IPAddrs[0])
|
|
},
|
|
"IPAddrs": func() *Event {
|
|
return e.IPAddrs("k", fixtures.IPAddrs)
|
|
},
|
|
"IPPrefix": func() *Event {
|
|
return e.IPPrefix("k", fixtures.IPPfxs[0])
|
|
},
|
|
"IPPrefixes": func() *Event {
|
|
return e.IPPrefixes("k", fixtures.IPPfxs)
|
|
},
|
|
"MACAddr": func() *Event {
|
|
return e.MACAddr("k", fixtures.MACAddr)
|
|
},
|
|
"Type": func() *Event {
|
|
return e.Type("k", fixtures.Type)
|
|
},
|
|
"Caller": func() *Event {
|
|
return e.Caller(1)
|
|
},
|
|
"CallerSkip": func() *Event {
|
|
return e.CallerSkipFrame(2)
|
|
},
|
|
"Stack": func() *Event {
|
|
return e.Stack()
|
|
},
|
|
}
|
|
|
|
for name := range types {
|
|
f := types[name]
|
|
if got := f(); got != nil {
|
|
t.Errorf("Event.Bool() = %v, want %v", got, nil)
|
|
}
|
|
}
|
|
|
|
e.Send()
|
|
e.Msg("nothing")
|
|
e.Msgf("what %s", "nothing")
|
|
|
|
got := e.write()
|
|
if got != nil {
|
|
t.Errorf("Event.write() = %v, want %v", got, e)
|
|
}
|
|
|
|
called := false
|
|
e.MsgFunc(func() string {
|
|
called = true
|
|
return "called"
|
|
})
|
|
if called {
|
|
t.Errorf("Event.MsgFunc() should not be called on nil Event")
|
|
}
|
|
}
|
|
|
|
func TestEvent_MsgFunc(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel)
|
|
|
|
called := false
|
|
e.MsgFunc(func() string {
|
|
called = true
|
|
return "called"
|
|
})
|
|
if !called {
|
|
t.Errorf("Event.MsgFunc() was not called on non-nil Event")
|
|
}
|
|
|
|
want := `{"message":"called"}`
|
|
got := strings.TrimSpace(buf.String())
|
|
if got != want {
|
|
t.Errorf("Event.MsgFunc() = %q, want %q", got, want)
|
|
}
|
|
}
|