1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00
Files
opentelemetry-go/attribute/key_test.go
T

121 lines
2.9 KiB
Go
Raw Normal View History

2020-05-14 07:06:03 +08:00
// Copyright The OpenTelemetry Authors
2024-02-29 07:05:28 +01:00
// SPDX-License-Identifier: Apache-2.0
2020-05-14 07:06:03 +08:00
2021-02-18 12:59:37 -05:00
package attribute_test
2020-05-14 07:06:03 +08:00
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
2021-02-18 12:59:37 -05:00
"go.opentelemetry.io/otel/attribute"
2020-05-14 07:06:03 +08:00
)
func TestDefined(t *testing.T) {
for _, testcase := range []struct {
name string
2021-02-18 12:59:37 -05:00
k attribute.Key
2020-05-14 07:06:03 +08:00
want bool
}{
{
name: "Key.Defined() returns true when len(v.Name) != 0",
2021-02-18 12:59:37 -05:00
k: attribute.Key("foo"),
2020-05-14 07:06:03 +08:00
want: true,
},
{
name: "Key.Defined() returns false when len(v.Name) == 0",
2021-02-18 12:59:37 -05:00
k: attribute.Key(""),
2020-05-14 07:06:03 +08:00
want: false,
},
} {
t.Run(testcase.name, func(t *testing.T) {
2023-10-16 10:02:21 -07:00
// func (k attribute.Key) Defined() bool {
2020-05-14 07:06:03 +08:00
have := testcase.k.Defined()
if have != testcase.want {
t.Errorf("Want: %v, but have: %v", testcase.want, have)
}
})
}
}
func TestJSONValue(t *testing.T) {
2025-07-29 18:19:11 +10:00
var kvs any = [2]attribute.KeyValue{
2021-02-18 12:59:37 -05:00
attribute.String("A", "B"),
attribute.Int64("C", 1),
2020-05-14 07:06:03 +08:00
}
data, err := json.Marshal(kvs)
require.NoError(t, err)
require.JSONEq(t,
2020-05-14 07:06:03 +08:00
`[{"Key":"A","Value":{"Type":"STRING","Value":"B"}},{"Key":"C","Value":{"Type":"INT64","Value":1}}]`,
string(data))
}
func TestEmit(t *testing.T) {
for _, testcase := range []struct {
name string
2021-02-18 12:59:37 -05:00
v attribute.Value
2020-05-14 07:06:03 +08:00
want string
}{
{
name: `test Key.Emit() can emit a string representing self.BOOL`,
2021-02-18 12:59:37 -05:00
v: attribute.BoolValue(true),
2020-05-14 07:06:03 +08:00
want: "true",
},
{
name: `test Key.Emit() can emit a string representing self.BOOLSLICE`,
v: attribute.BoolSliceValue([]bool{true, false, true}),
want: `[true false true]`,
},
{
name: `test Key.Emit() can emit a string representing self.INT64SLICE`,
v: attribute.Int64SliceValue([]int64{1, 42}),
want: `[1,42]`,
},
2020-05-14 07:06:03 +08:00
{
name: `test Key.Emit() can emit a string representing self.INT64`,
2021-02-18 12:59:37 -05:00
v: attribute.Int64Value(42),
2020-05-14 07:06:03 +08:00
want: "42",
},
{
name: `test Key.Emit() can representing an int value`,
v: attribute.IntValue(7),
want: "7",
},
{
name: `test Key.Emit() can represent an []int value`,
v: attribute.IntSliceValue([]int{1, 2, 3}),
want: `[1,2,3]`,
},
{
name: `test Key.Emit() can emit a string representing self.FLOAT64SLICE`,
v: attribute.Float64SliceValue([]float64{1.0, 42.5}),
want: `[1,42.5]`,
},
2020-05-14 07:06:03 +08:00
{
name: `test Key.Emit() can emit a string representing self.FLOAT64`,
2021-02-18 12:59:37 -05:00
v: attribute.Float64Value(42.1),
2020-05-14 07:06:03 +08:00
want: "42.1",
},
{
name: `test Key.Emit() can emit a string representing self.STRING`,
2021-02-18 12:59:37 -05:00
v: attribute.StringValue("foo"),
2020-05-14 07:06:03 +08:00
want: "foo",
},
{
name: `test Key.Emit() can emit a string representing self.STRINGSLICE`,
v: attribute.StringSliceValue([]string{"foo", "bar"}),
want: `["foo","bar"]`,
},
2020-05-14 07:06:03 +08:00
} {
t.Run(testcase.name, func(t *testing.T) {
2023-10-16 10:02:21 -07:00
// proto: func (v attribute.Value) Emit() string {
2020-05-14 07:06:03 +08:00
have := testcase.v.Emit()
if have != testcase.want {
t.Errorf("Want: %s, but have: %s", testcase.want, have)
}
})
}
}