11package log_test
22
33import (
4+ "encoding"
45 "fmt"
6+ "reflect"
57 "testing"
68 "time"
79
@@ -34,7 +36,7 @@ func TestValueBinding(t *testing.T) {
3436 if want , have := start .Add (time .Second ), timestamp ; want != have {
3537 t .Errorf ("output[1]: want %v, have %v" , want , have )
3638 }
37- if want , have := "value_test.go:29 " , fmt .Sprint (output [3 ]); want != have {
39+ if want , have := "value_test.go:31 " , fmt .Sprint (output [3 ]); want != have {
3840 t .Errorf ("output[3]: want %s, have %s" , want , have )
3941 }
4042
@@ -47,7 +49,7 @@ func TestValueBinding(t *testing.T) {
4749 if want , have := start .Add (2 * time .Second ), timestamp ; want != have {
4850 t .Errorf ("output[1]: want %v, have %v" , want , have )
4951 }
50- if want , have := "value_test.go:42 " , fmt .Sprint (output [3 ]); want != have {
52+ if want , have := "value_test.go:44 " , fmt .Sprint (output [3 ]); want != have {
5153 t .Errorf ("output[3]: want %s, have %s" , want , have )
5254 }
5355}
@@ -90,6 +92,43 @@ func TestValueBinding_loggingZeroKeyvals(t *testing.T) {
9092 }
9193}
9294
95+ func TestTimestampFormat (t * testing.T ) {
96+ t .Parallel ()
97+ 98+ start := time .Date (2015 , time .April , 25 , 0 , 0 , 0 , 0 , time .UTC )
99+ now := start
100+ mocktime := func () time.Time {
101+ now = now .Add (time .Second )
102+ return now
103+ }
104+ 105+ tv := log .TimestampFormat (mocktime , time .RFC822 )
106+ 107+ if want , have := now .Add (time .Second ).Format (time .RFC822 ), fmt .Sprint (tv ()); want != have {
108+ t .Errorf ("wrong time format: want %v, have %v" , want , have )
109+ }
110+ 111+ if want , have := now .Add (2 * time .Second ).Format (time .RFC822 ), fmt .Sprint (tv ()); want != have {
112+ t .Errorf ("wrong time format: want %v, have %v" , want , have )
113+ }
114+ 115+ mustMarshal := func (v interface {}) []byte {
116+ b , err := v .(encoding.TextMarshaler ).MarshalText ()
117+ if err != nil {
118+ t .Fatal ("error marshaling text:" , err )
119+ }
120+ return b
121+ }
122+ 123+ if want , have := now .Add (3 * time .Second ).AppendFormat (nil , time .RFC822 ), mustMarshal (tv ()); ! reflect .DeepEqual (want , have ) {
124+ t .Errorf ("wrong time format: want %s, have %s" , want , have )
125+ }
126+ 127+ if want , have := now .Add (4 * time .Second ).AppendFormat (nil , time .RFC822 ), mustMarshal (tv ()); ! reflect .DeepEqual (want , have ) {
128+ t .Errorf ("wrong time format: want %s, have %s" , want , have )
129+ }
130+ }
131+ 93132func BenchmarkValueBindingTimestamp (b * testing.B ) {
94133 logger := log .NewNopLogger ()
95134 lc := log .With (logger , "ts" , log .DefaultTimestamp )
0 commit comments