2017-05-19 19:59:10 +00:00
|
|
|
package zerolog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCtx(t *testing.T) {
|
|
|
|
log := New(ioutil.Discard)
|
|
|
|
ctx := log.WithContext(context.Background())
|
2017-05-20 07:22:37 +00:00
|
|
|
log2 := Ctx(ctx)
|
2017-08-30 05:53:32 +00:00
|
|
|
if !reflect.DeepEqual(log, *log2) {
|
2017-05-20 07:22:37 +00:00
|
|
|
t.Error("Ctx did not return the expected logger")
|
2017-05-19 19:59:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 08:48:00 +00:00
|
|
|
// update
|
|
|
|
log = log.Level(InfoLevel)
|
|
|
|
ctx = log.WithContext(ctx)
|
|
|
|
log2 = Ctx(ctx)
|
2017-08-30 05:53:32 +00:00
|
|
|
if !reflect.DeepEqual(log, *log2) {
|
2017-05-20 08:48:00 +00:00
|
|
|
t.Error("Ctx did not return the expected logger")
|
|
|
|
}
|
|
|
|
|
2017-05-20 07:22:37 +00:00
|
|
|
log2 = Ctx(context.Background())
|
2017-08-30 05:53:32 +00:00
|
|
|
if log2 != disabledLogger {
|
2017-05-20 07:22:37 +00:00
|
|
|
t.Error("Ctx did not return the expected logger")
|
2017-05-19 19:59:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-30 05:53:32 +00:00
|
|
|
|
|
|
|
func TestCtxDisabled(t *testing.T) {
|
|
|
|
ctx := disabledLogger.WithContext(context.Background())
|
|
|
|
if ctx != context.Background() {
|
|
|
|
t.Error("WithContext stored a disabled logger")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = New(ioutil.Discard).WithContext(ctx)
|
|
|
|
if reflect.DeepEqual(Ctx(ctx), disabledLogger) {
|
|
|
|
t.Error("WithContext did not store logger")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = disabledLogger.WithContext(ctx)
|
|
|
|
if !reflect.DeepEqual(Ctx(ctx), disabledLogger) {
|
|
|
|
t.Error("WithContext did not update logger pointer with disabled logger")
|
|
|
|
}
|
|
|
|
}
|