Debian Codegraph @

attribute/key_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package attribute_test
5
6 import (
7 "encoding/json"
8 "testing"
9
10 "github.com/stretchr/testify/require"
11
12 "go.opentelemetry.io/otel/attribute"
13 )
14
15 func TestDefined(t *testing.T) {
16 for _, testcase := range []struct {
17 name string
18 k attribute.Key
19 want bool
20 }{
21 {
22 name: "Key.Defined() returns true when len(v.Name) != 0",
23 k: attribute.Key("foo"),
24 want: true,
25 },
26 {
27 name: "Key.Defined() returns false when len(v.Name) == 0",
28 k: attribute.Key(""),
29 want: false,
30 },
31 } {
32 t.Run(testcase.name, func(t *testing.T) {
33 // func (k attribute.Key) Defined() bool {
34 have := testcase.k.Defined()
35 if have != testcase.want {
36 t.Errorf("Want: %v, but have: %v", testcase.want, have)
37 }
38 })
39 }
40 }
41
42 func TestJSONValue(t *testing.T) {
43 var kvs any = [2]attribute.KeyValue{
44 attribute.String("A", "B"),
45 attribute.Int64("C", 1),
46 }
47
48 data, err := json.Marshal(kvs)
49 require.NoError(t, err)
50 require.JSONEq(t,
51 `[{"Key":"A","Value":{"Type":"STRING","Value":"B"}},{"Key":"C","Value":{"Type":"INT64","Value":1}}]`,
52 string(data))
53 }
54
55 func TestEmit(t *testing.T) {
56 for _, testcase := range []struct {
57 name string
58 v attribute.Value
59 want string
60 }{
61 {
62 name: `test Key.Emit() can emit a string representing self.BOOL`,
63 v: attribute.BoolValue(true),
64 want: "true",
65 },
66 {
67 name: `test Key.Emit() can emit a string representing self.BOOLSLICE`,
68 v: attribute.BoolSliceValue([]bool{true, false, true}),
69 want: `[true false true]`,
70 },
71 {
72 name: `test Key.Emit() can emit a string representing self.INT64SLICE`,
73 v: attribute.Int64SliceValue([]int64{1, 42}),
74 want: `[1,42]`,
75 },
76 {
77 name: `test Key.Emit() can emit a string representing self.INT64`,
78 v: attribute.Int64Value(42),
79 want: "42",
80 },
81 {
82 name: `test Key.Emit() can representing an int value`,
83 v: attribute.IntValue(7),
84 want: "7",
85 },
86 {
87 name: `test Key.Emit() can represent an []int value`,
88 v: attribute.IntSliceValue([]int{1, 2, 3}),
89 want: `[1,2,3]`,
90 },
91 {
92 name: `test Key.Emit() can emit a string representing self.FLOAT64SLICE`,
93 v: attribute.Float64SliceValue([]float64{1.0, 42.5}),
94 want: `[1,42.5]`,
95 },
96 {
97 name: `test Key.Emit() can emit a string representing self.FLOAT64`,
98 v: attribute.Float64Value(42.1),
99 want: "42.1",
100 },
101 {
102 name: `test Key.Emit() can emit a string representing self.STRING`,
103 v: attribute.StringValue("foo"),
104 want: "foo",
105 },
106 {
107 name: `test Key.Emit() can emit a string representing self.STRINGSLICE`,
108 v: attribute.StringSliceValue([]string{"foo", "bar"}),
109 want: `["foo","bar"]`,
110 },
111 {
112 name: `test Key.Emit() can emit a string representing self.EMPTY`,
113 v: attribute.Value{},
114 want: "",
115 },
116 } {
117 t.Run(testcase.name, func(t *testing.T) {
118 // proto: func (v attribute.Value) Emit() string {
119 have := testcase.v.Emit()
120 if have != testcase.want {
121 t.Errorf("Want: %s, but have: %s", testcase.want, have)
122 }
123 })
124 }
125 }