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
357 lines
7.9 KiB
Go
357 lines
7.9 KiB
Go
package json
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"math"
|
|
"testing"
|
|
|
|
"github.com/rs/zerolog/internal"
|
|
)
|
|
|
|
func TestAppendInts8(t *testing.T) {
|
|
doOne := func(vals []int8) {
|
|
want := make([]byte, 0)
|
|
want = append(want, '[')
|
|
for i, val := range vals {
|
|
want = append(want, []byte(fmt.Sprintf("%d", int8(val)))...)
|
|
if i < len(vals)-1 {
|
|
want = append(want, ',')
|
|
}
|
|
}
|
|
want = append(want, ']')
|
|
|
|
got := enc.AppendInts8([]byte{}, vals)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendInts8(%v)\ngot: %s\nwant: %s",
|
|
vals,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
|
|
array := make([]int8, 0)
|
|
for _, tc := range internal.IntegerTestCases {
|
|
if (tc.Val < math.MinInt8) || (tc.Val > math.MaxInt8) {
|
|
continue
|
|
}
|
|
array = append(array, int8(tc.Val))
|
|
}
|
|
|
|
doOne(array)
|
|
doOne(array[:1]) // single element
|
|
doOne(array[:0]) // edge case of zero length
|
|
}
|
|
func TestAppendUints8(t *testing.T) {
|
|
doOne := func(vals []uint8) {
|
|
want := make([]byte, 0)
|
|
want = append(want, '[')
|
|
for i, val := range vals {
|
|
want = append(want, []byte(fmt.Sprintf("%v", uint8(val)))...)
|
|
if i < len(vals)-1 {
|
|
want = append(want, ',')
|
|
}
|
|
}
|
|
want = append(want, ']')
|
|
|
|
got := enc.AppendUints8([]byte{}, vals)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendUints8(%v)\ngot: %s\nwant: %s",
|
|
vals,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
|
|
array := make([]uint8, 0)
|
|
for _, tc := range internal.UnsignedIntegerTestCases {
|
|
if tc.Val > math.MaxUint8 {
|
|
continue
|
|
}
|
|
array = append(array, uint8(tc.Val))
|
|
}
|
|
|
|
doOne(array)
|
|
doOne(array[:1]) // single element
|
|
doOne(array[:0]) // edge case of zero length
|
|
}
|
|
func TestAppendInts16(t *testing.T) {
|
|
doOne := func(vals []int16) {
|
|
want := make([]byte, 0)
|
|
want = append(want, '[')
|
|
for i, val := range vals {
|
|
want = append(want, []byte(fmt.Sprintf("%d", int16(val)))...)
|
|
if i < len(vals)-1 {
|
|
want = append(want, ',')
|
|
}
|
|
}
|
|
want = append(want, ']')
|
|
|
|
got := enc.AppendInts16([]byte{}, vals)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendInts16(%v)\ngot: %s\nwant: %s",
|
|
vals,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
|
|
array := make([]int16, 0)
|
|
for _, tc := range internal.IntegerTestCases {
|
|
if (tc.Val < math.MinInt16) || (tc.Val > math.MaxInt16) {
|
|
continue
|
|
}
|
|
array = append(array, int16(tc.Val))
|
|
}
|
|
|
|
doOne(array)
|
|
doOne(array[:1]) // single element
|
|
doOne(array[:0]) // edge case of zero length
|
|
}
|
|
func TestAppendUints16(t *testing.T) {
|
|
doOne := func(vals []uint16) {
|
|
want := make([]byte, 0)
|
|
want = append(want, '[')
|
|
for i, val := range vals {
|
|
want = append(want, []byte(fmt.Sprintf("%d", uint16(val)))...)
|
|
if i < len(vals)-1 {
|
|
want = append(want, ',')
|
|
}
|
|
}
|
|
want = append(want, ']')
|
|
|
|
got := enc.AppendUints16([]byte{}, vals)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendUints16(%v)\ngot: %s\nwant: %s",
|
|
vals,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
|
|
array := make([]uint16, 0)
|
|
for _, tc := range internal.UnsignedIntegerTestCases {
|
|
if tc.Val > math.MaxUint16 {
|
|
continue
|
|
}
|
|
array = append(array, uint16(tc.Val))
|
|
}
|
|
|
|
doOne(array)
|
|
doOne(array[:1]) // single element
|
|
doOne(array[:0]) // edge case of zero length
|
|
}
|
|
func TestAppendInts32(t *testing.T) {
|
|
doOne := func(vals []int32) {
|
|
want := make([]byte, 0)
|
|
want = append(want, '[')
|
|
for i, val := range vals {
|
|
want = append(want, []byte(fmt.Sprintf("%d", int32(val)))...)
|
|
if i < len(vals)-1 {
|
|
want = append(want, ',')
|
|
}
|
|
}
|
|
want = append(want, ']')
|
|
|
|
got := enc.AppendInts32([]byte{}, vals)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendInts32(%v)\ngot: %s\nwant: %s",
|
|
vals,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
|
|
array := make([]int32, 0)
|
|
for _, tc := range internal.IntegerTestCases {
|
|
if (tc.Val < math.MinInt32) || (tc.Val > math.MaxInt32) {
|
|
continue
|
|
}
|
|
array = append(array, int32(tc.Val))
|
|
}
|
|
|
|
doOne(array)
|
|
doOne(array[:1]) // single element
|
|
doOne(array[:0]) // edge case of zero length
|
|
}
|
|
func TestAppendUints32(t *testing.T) {
|
|
doOne := func(vals []uint32) {
|
|
want := make([]byte, 0)
|
|
want = append(want, '[')
|
|
for i, val := range vals {
|
|
want = append(want, []byte(fmt.Sprintf("%d", uint32(val)))...)
|
|
if i < len(vals)-1 {
|
|
want = append(want, ',')
|
|
}
|
|
}
|
|
want = append(want, ']')
|
|
|
|
got := enc.AppendUints32([]byte{}, vals)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendUints32(%v)\ngot: %s\nwant: %s",
|
|
vals,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
|
|
array := make([]uint32, 0)
|
|
for _, tc := range internal.UnsignedIntegerTestCases {
|
|
if tc.Val > math.MaxUint32 {
|
|
continue
|
|
}
|
|
array = append(array, uint32(tc.Val))
|
|
}
|
|
|
|
doOne(array)
|
|
doOne(array[:1]) // single element
|
|
doOne(array[:0]) // edge case of zero length
|
|
}
|
|
|
|
func TestAppendInt64(t *testing.T) {
|
|
doOne := func(vals []int64) {
|
|
want := make([]byte, 0)
|
|
want = append(want, '[')
|
|
for i, val := range vals {
|
|
want = append(want, []byte(fmt.Sprintf("%d", int64(val)))...)
|
|
if i < len(vals)-1 {
|
|
want = append(want, ',')
|
|
}
|
|
}
|
|
want = append(want, ']')
|
|
|
|
got := enc.AppendInts64([]byte{}, vals)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendInts64(%v)\ngot: %s\nwant: %s",
|
|
vals,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
|
|
array := make([]int64, 0)
|
|
for _, tc := range internal.IntegerTestCases {
|
|
array = append(array, int64(tc.Val))
|
|
}
|
|
|
|
doOne(array)
|
|
doOne(array[:1]) // single element
|
|
doOne(array[:0]) // edge case of zero length
|
|
}
|
|
func TestAppendUints64(t *testing.T) {
|
|
doOne := func(vals []uint64) {
|
|
want := make([]byte, 0)
|
|
want = append(want, '[')
|
|
for i, val := range vals {
|
|
want = append(want, []byte(fmt.Sprintf("%d", uint64(val)))...)
|
|
if i < len(vals)-1 {
|
|
want = append(want, ',')
|
|
}
|
|
}
|
|
want = append(want, ']')
|
|
|
|
got := enc.AppendUints64([]byte{}, vals)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendUints64(%v)\ngot: %s\nwant: %s",
|
|
vals,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
|
|
array := make([]uint64, 0)
|
|
for _, tc := range internal.UnsignedIntegerTestCases {
|
|
array = append(array, uint64(tc.Val))
|
|
}
|
|
|
|
doOne(array)
|
|
doOne(array[:1]) // single element
|
|
doOne(array[:0]) // edge case of zero length
|
|
}
|
|
|
|
func TestAppendInt(t *testing.T) {
|
|
for _, tc := range internal.IntegerTestCases {
|
|
want := []byte(fmt.Sprintf("%d", tc.Val))
|
|
got := enc.AppendInt([]byte{}, tc.Val)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendInt(0x%x)\ngot: %s\nwant: %s",
|
|
tc.Val,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
}
|
|
func TestAppendUint(t *testing.T) {
|
|
for _, tc := range internal.UnsignedIntegerTestCases {
|
|
want := []byte(fmt.Sprintf("%d", tc.Val))
|
|
got := enc.AppendUint([]byte{}, tc.Val)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendUint(0x%x)\ngot: %s\nwant: %s",
|
|
tc.Val,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAppendInts(t *testing.T) {
|
|
doOne := func(vals []int) {
|
|
want := make([]byte, 0)
|
|
want = append(want, '[')
|
|
for i, val := range vals {
|
|
want = append(want, []byte(fmt.Sprintf("%d", int(val)))...)
|
|
if i < len(vals)-1 {
|
|
want = append(want, ',')
|
|
}
|
|
}
|
|
want = append(want, ']')
|
|
|
|
got := enc.AppendInts([]byte{}, vals)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendInts(%v)\ngot: %s\nwant: %s",
|
|
vals,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
|
|
array := make([]int, 0)
|
|
for _, tc := range internal.IntegerTestCases {
|
|
array = append(array, int(tc.Val))
|
|
}
|
|
|
|
doOne(array)
|
|
doOne(array[:1]) // single element
|
|
doOne(array[:0]) // edge case of zero length
|
|
}
|
|
func TestAppendUints(t *testing.T) {
|
|
doOne := func(vals []uint) {
|
|
want := make([]byte, 0)
|
|
want = append(want, '[')
|
|
for i, val := range vals {
|
|
want = append(want, []byte(fmt.Sprintf("%d", uint(val)))...)
|
|
if i < len(vals)-1 {
|
|
want = append(want, ',')
|
|
}
|
|
}
|
|
want = append(want, ']')
|
|
|
|
got := enc.AppendUints([]byte{}, vals)
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("AppendUints(%v)\ngot: %s\nwant: %s",
|
|
vals,
|
|
string(got),
|
|
string(want))
|
|
}
|
|
}
|
|
|
|
array := make([]uint, 0)
|
|
for _, tc := range internal.UnsignedIntegerTestCases {
|
|
array = append(array, uint(tc.Val))
|
|
}
|
|
|
|
doOne(array)
|
|
doOne(array[:1]) // single element
|
|
doOne(array[:0]) // edge case of zero length
|
|
}
|