You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
900 B
Go
36 lines
900 B
Go
2 years ago
|
package logx
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestWithFields(t *testing.T) {
|
||
|
ctx := WithFields(context.Background(), Field("a", 1), Field("b", 2))
|
||
|
vals := ctx.Value(fieldsContextKey)
|
||
|
assert.NotNil(t, vals)
|
||
|
fields, ok := vals.([]LogField)
|
||
|
assert.True(t, ok)
|
||
|
assert.EqualValues(t, []LogField{Field("a", 1), Field("b", 2)}, fields)
|
||
|
}
|
||
|
|
||
|
func TestWithFieldsAppend(t *testing.T) {
|
||
|
var dummyKey struct{}
|
||
|
ctx := context.WithValue(context.Background(), dummyKey, "dummy")
|
||
|
ctx = WithFields(ctx, Field("a", 1), Field("b", 2))
|
||
|
ctx = WithFields(ctx, Field("c", 3), Field("d", 4))
|
||
|
vals := ctx.Value(fieldsContextKey)
|
||
|
assert.NotNil(t, vals)
|
||
|
fields, ok := vals.([]LogField)
|
||
|
assert.True(t, ok)
|
||
|
assert.Equal(t, "dummy", ctx.Value(dummyKey))
|
||
|
assert.EqualValues(t, []LogField{
|
||
|
Field("a", 1),
|
||
|
Field("b", 2),
|
||
|
Field("c", 3),
|
||
|
Field("d", 4),
|
||
|
}, fields)
|
||
|
}
|