github.com/msales/pkg/v3@v3.24.0/log/logger_internal_test.go (about) 1 package log 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestWithLogger(t *testing.T) { 11 ctx := WithLogger(context.Background(), Null) 12 13 got := ctx.Value(ctxKey) 14 15 assert.Equal(t, Null, got) 16 } 17 18 func TestWithLogger_NilLogger(t *testing.T) { 19 ctx := WithLogger(context.Background(), nil) 20 21 got := ctx.Value(ctxKey) 22 23 assert.Equal(t, Null, got) 24 } 25 26 func TestFromContext(t *testing.T) { 27 ctx := context.WithValue(context.Background(), ctxKey, Null) 28 29 got, ok := FromContext(ctx) 30 31 assert.True(t, ok) 32 assert.Equal(t, Null, got) 33 } 34 35 func TestFromContext_NotSet(t *testing.T) { 36 ctx := context.Background() 37 38 got, ok := FromContext(ctx) 39 40 assert.False(t, ok) 41 assert.Nil(t, got) 42 } 43 44 func TestWithLoggerFunc(t *testing.T) { 45 tests := []struct { 46 ctx context.Context 47 expect Logger 48 }{ 49 {context.Background(), Null}, 50 } 51 52 for _, tt := range tests { 53 withLogger(tt.ctx, func(l Logger) { 54 assert.Equal(t, tt.expect, l) 55 }) 56 } 57 }