@@ -246,6 +246,66 @@ func TestFieldsMapNilPnt(t *testing.T) {
246246 }
247247}
248248
249+ func TestFieldsSlice (t * testing.T ) {
250+ out := & bytes.Buffer {}
251+ log := New (out )
252+ log .Log ().Fields ([]interface {}{
253+ "nil" , nil ,
254+ "string" , "foo" ,
255+ "bytes" , []byte ("bar" ),
256+ "error" , errors .New ("some error" ),
257+ "bool" , true ,
258+ "int" , int (1 ),
259+ "int8" , int8 (2 ),
260+ "int16" , int16 (3 ),
261+ "int32" , int32 (4 ),
262+ "int64" , int64 (5 ),
263+ "uint" , uint (6 ),
264+ "uint8" , uint8 (7 ),
265+ "uint16" , uint16 (8 ),
266+ "uint32" , uint32 (9 ),
267+ "uint64" , uint64 (10 ),
268+ "float32" , float32 (11 ),
269+ "float64" , float64 (12 ),
270+ "ipv6" , net.IP {0x20 , 0x01 , 0x0d , 0xb8 , 0x85 , 0xa3 , 0x00 , 0x00 , 0x00 , 0x00 , 0x8a , 0x2e , 0x03 , 0x70 , 0x73 , 0x34 },
271+ "dur" , 1 * time .Second ,
272+ "time" , time.Time {},
273+ "obj" , obj {"a" , "b" , 1 },
274+ }).Msg ("" )
275+ if got , want := decodeIfBinaryToString (out .Bytes ()), `{"nil":null,"string":"foo","bytes":"bar","error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11,"float64":12,"ipv6":"2001:db8:85a3::8a2e:370:7334","dur":1000,"time":"0001-01-01T00:00:00Z","obj":{"Pub":"a","Tag":"b","priv":1}}` + "\n " ; got != want {
276+ t .Errorf ("invalid log output:\n got: %v\n want: %v" , got , want )
277+ }
278+ }
279+
280+ func TestFieldsSliceExtraneous (t * testing.T ) {
281+ out := & bytes.Buffer {}
282+ log := New (out )
283+ log .Log ().Fields ([]interface {}{
284+ "string" , "foo" ,
285+ "error" , errors .New ("some error" ),
286+ 32 , "valueForNonStringKey" ,
287+ "bool" , true ,
288+ "int" , int (1 ),
289+ "keyWithoutValue" ,
290+ }).Msg ("" )
291+ if got , want := decodeIfBinaryToString (out .Bytes ()), `{"string":"foo","error":"some error","bool":true,"int":1}` + "\n " ; got != want {
292+ t .Errorf ("invalid log output:\n got: %v\n want: %v" , got , want )
293+ }
294+ }
295+
296+ func TestFieldsNotMapSlice (t * testing.T ) {
297+ out := & bytes.Buffer {}
298+ log := New (out )
299+ log .Log ().
300+ Fields (obj {"a" , "b" , 1 }).
301+ Fields ("string" ).
302+ Fields (1 ).
303+ Msg ("" )
304+ if got , want := decodeIfBinaryToString (out .Bytes ()), `{}` + "\n " ; got != want {
305+ t .Errorf ("invalid log output:\n got: %v\n want: %v" , got , want )
306+ }
307+ }
308+
249309func TestFields (t * testing.T ) {
250310 out := & bytes.Buffer {}
251311 log := New (out )
@@ -336,7 +396,7 @@ func TestFieldsArraySingleElement(t *testing.T) {
336396 Floats32 ("float32" , []float32 {11 }).
337397 Floats64 ("float64" , []float64 {12 }).
338398 Durs ("dur" , []time.Duration {1 * time .Second }).
339- Times ("time" , []time.Time {time. Time {}}).
399+ Times ("time" , []time.Time {{}}).
340400 Msg ("" )
341401 if got , want := decodeIfBinaryToString (out .Bytes ()), `{"string":["foo"],"err":["some error"],"bool":[true],"int":[1],"int8":[2],"int16":[3],"int32":[4],"int64":[5],"uint":[6],"uint8":[7],"uint16":[8],"uint32":[9],"uint64":[10],"float32":[11],"float64":[12],"dur":[1000],"time":["0001-01-01T00:00:00Z"]}` + "\n " ; got != want {
342402 t .Errorf ("invalid log output:\n got: %v\n want: %v" , got , want )
@@ -363,7 +423,7 @@ func TestFieldsArrayMultipleElement(t *testing.T) {
363423 Floats32 ("float32" , []float32 {11 , 0 }).
364424 Floats64 ("float64" , []float64 {12 , 0 }).
365425 Durs ("dur" , []time.Duration {1 * time .Second , 0 }).
366- Times ("time" , []time.Time {time. Time {}, time. Time {}}).
426+ Times ("time" , []time.Time {{}, {}}).
367427 Msg ("" )
368428 if got , want := decodeIfBinaryToString (out .Bytes ()), `{"string":["foo","bar"],"err":["some error",null],"bool":[true,false],"int":[1,0],"int8":[2,0],"int16":[3,0],"int32":[4,0],"int64":[5,0],"uint":[6,0],"uint8":[7,0],"uint16":[8,0],"uint32":[9,0],"uint64":[10,0],"float32":[11,0],"float64":[12,0],"dur":[1000,0],"time":["0001-01-01T00:00:00Z","0001-01-01T00:00:00Z"]}` + "\n " ; got != want {
369429 t .Errorf ("invalid log output:\n got: %v\n want: %v" , got , want )
0 commit comments