Debian Codegraph @

attribute/value_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package attribute_test
5
6 import (
7 "testing"
8
9 "github.com/google/go-cmp/cmp"
10 "github.com/stretchr/testify/assert"
11
12 "go.opentelemetry.io/otel/attribute"
13 )
14
15 func TestValue(t *testing.T) {
16 k := attribute.Key("test")
17 for _, testcase := range []struct {
18 name string
19 value attribute.Value
20 wantType attribute.Type
21 wantValue any
22 }{
23 {
24 name: "Key.Bool() correctly returns keys's internal bool value",
25 value: k.Bool(true).Value,
26 wantType: attribute.BOOL,
27 wantValue: true,
28 },
29 {
30 name: "Key.BoolSlice() correctly returns keys's internal []bool value",
31 value: k.BoolSlice([]bool{true, false, true}).Value,
32 wantType: attribute.BOOLSLICE,
33 wantValue: []bool{true, false, true},
34 },
35 {
36 name: "Key.Int64() correctly returns keys's internal int64 value",
37 value: k.Int64(42).Value,
38 wantType: attribute.INT64,
39 wantValue: int64(42),
40 },
41 {
42 name: "Key.Int64() correctly returns negative keys's internal int64 value",
43 value: k.Int64(-42).Value,
44 wantType: attribute.INT64,
45 wantValue: int64(-42),
46 },
47 {
48 name: "Key.Int64Slice() correctly returns keys's internal []int64 value",
49 value: k.Int64Slice([]int64{42, -3, 12}).Value,
50 wantType: attribute.INT64SLICE,
51 wantValue: []int64{42, -3, 12},
52 },
53 {
54 name: "Key.Int() correctly returns keys's internal signed integral value",
55 value: k.Int(42).Value,
56 wantType: attribute.INT64,
57 wantValue: int64(42),
58 },
59 {
60 name: "Key.IntSlice() correctly returns keys's internal []int64 value",
61 value: k.IntSlice([]int{42, -3, 12}).Value,
62 wantType: attribute.INT64SLICE,
63 wantValue: []int64{42, -3, 12},
64 },
65 {
66 name: "Key.Float64() correctly returns keys's internal float64 value",
67 value: k.Float64(42.1).Value,
68 wantType: attribute.FLOAT64,
69 wantValue: 42.1,
70 },
71 {
72 name: "Key.Float64Slice() correctly returns keys's internal []float64 value",
73 value: k.Float64Slice([]float64{42, -3, 12}).Value,
74 wantType: attribute.FLOAT64SLICE,
75 wantValue: []float64{42, -3, 12},
76 },
77 {
78 name: "Key.String() correctly returns keys's internal string value",
79 value: k.String("foo").Value,
80 wantType: attribute.STRING,
81 wantValue: "foo",
82 },
83 {
84 name: "Key.StringSlice() correctly returns keys's internal []string value",
85 value: k.StringSlice([]string{"forty-two", "negative three", "twelve"}).Value,
86 wantType: attribute.STRINGSLICE,
87 wantValue: []string{"forty-two", "negative three", "twelve"},
88 },
89 {
90 name: "empty value",
91 value: attribute.Value{},
92 wantType: attribute.EMPTY,
93 wantValue: nil,
94 },
95 } {
96 t.Logf("Running test case %s", testcase.name)
97 if testcase.value.Type() != testcase.wantType {
98 t.Errorf("wrong value type, got %#v, expected %#v", testcase.value.Type(), testcase.wantType)
99 }
100 got := testcase.value.AsInterface()
101 if diff := cmp.Diff(testcase.wantValue, got); diff != "" {
102 t.Errorf("+got, -want: %s", diff)
103 }
104 }
105 }
106
107 func TestEquivalence(t *testing.T) {
108 pairs := [][2]attribute.KeyValue{
109 {
110 attribute.Bool("Bool", true),
111 attribute.Bool("Bool", true),
112 },
113 {
114 attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
115 attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
116 },
117 {
118 attribute.Int("Int", 34),
119 attribute.Int("Int", 34),
120 },
121 {
122 attribute.IntSlice("IntSlice", []int{312, 1, -2}),
123 attribute.IntSlice("IntSlice", []int{312, 1, -2}),
124 },
125 {
126 attribute.Int64("Int64", 98),
127 attribute.Int64("Int64", 98),
128 },
129 {
130 attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
131 attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
132 },
133 {
134 attribute.Float64("Float64", 19.09),
135 attribute.Float64("Float64", 19.09),
136 },
137 {
138 attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
139 attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
140 },
141 {
142 attribute.String("String", "string value"),
143 attribute.String("String", "string value"),
144 },
145 {
146 attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
147 attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
148 },
149 {
150 attribute.KeyValue{Key: "Empty"},
151 attribute.KeyValue{Key: "Empty"},
152 },
153 }
154
155 t.Run("Distinct", func(t *testing.T) {
156 for _, p := range pairs {
157 s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
158 m := map[attribute.Distinct]struct{}{s0.Equivalent(): {}}
159 _, ok := m[s1.Equivalent()]
160 assert.Truef(
161 t,
162 ok,
163 "Distinct comparison of %s type: not equivalent: %s != %s",
164 p[0].Value.Type(),
165 s0.Encoded(attribute.DefaultEncoder()),
166 s1.Encoded(attribute.DefaultEncoder()),
167 )
168 }
169 })
170
171 t.Run("Equality operator", func(t *testing.T) {
172 // Maintain backwards compatibility.
173 for _, p := range pairs {
174 if p[0] != p[1] {
175 t.Errorf("Expected %v to be equal to %v", p[0], p[1])
176 }
177 }
178 })
179
180 t.Run("Set", func(t *testing.T) {
181 // Maintain backwards compatibility.
182 for _, p := range pairs {
183 s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
184 m := map[attribute.Set]struct{}{s0: {}}
185 _, ok := m[s1]
186 assert.Truef(
187 t,
188 ok,
189 "Set comparison of %s type: not equivalent: %s != %s",
190 p[0].Value.Type(),
191 s0.Encoded(attribute.DefaultEncoder()),
192 s1.Encoded(attribute.DefaultEncoder()),
193 )
194 }
195 })
196 }
197
198 func TestNotEquivalence(t *testing.T) {
199 pairs := [][2]attribute.KeyValue{
200 {
201 attribute.Int("Key", 0),
202 attribute.Bool("Key", false),
203 },
204 {
205 attribute.Bool("Bool", true),
206 attribute.Bool("Bool", false),
207 },
208 {
209 attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
210 attribute.BoolSlice("BoolSlice", []bool{true, true, true}),
211 },
212 {
213 attribute.Int("Int", 34),
214 attribute.Int("Int", 32),
215 },
216 {
217 attribute.IntSlice("IntSlice", []int{312, 1, -2}),
218 attribute.IntSlice("IntSlice", []int{312, 2, -2}),
219 },
220 {
221 attribute.Int64("Int64", 98),
222 attribute.Int64("Int64", 97),
223 },
224 {
225 attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
226 attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 1}),
227 },
228 {
229 attribute.Float64("Float64", 19.09),
230 attribute.Float64("Float64", 22.09),
231 },
232 {
233 attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
234 attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 5}),
235 },
236 {
237 attribute.String("String", "string value"),
238 attribute.String("String", "another value"),
239 },
240 {
241 attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
242 attribute.StringSlice("StringSlice", []string{"one", "two"}),
243 },
244 {
245 attribute.KeyValue{Key: "Empty"},
246 attribute.String("Empty", ""),
247 },
248 }
249
250 t.Run("Distinct", func(t *testing.T) {
251 for _, p := range pairs {
252 s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
253 m := map[attribute.Distinct]struct{}{s0.Equivalent(): {}}
254 _, ok := m[s1.Equivalent()]
255 assert.Falsef(
256 t,
257 ok,
258 "Distinct comparison of %s type: equivalent: %s == %s",
259 p[0].Value.Type(),
260 s0.Encoded(attribute.DefaultEncoder()),
261 s1.Encoded(attribute.DefaultEncoder()),
262 )
263 }
264 })
265
266 t.Run("Equality operator", func(t *testing.T) {
267 // Maintain backwards compatibility.
268 for _, p := range pairs {
269 if p[0] == p[1] {
270 t.Errorf("Expected %v to not be equal to %v", p[0], p[1])
271 }
272 }
273 })
274
275 t.Run("Set", func(t *testing.T) {
276 // Maintain backwards compatibility.
277 for _, p := range pairs {
278 s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
279 m := map[attribute.Set]struct{}{s0: {}}
280 _, ok := m[s1]
281 assert.Falsef(
282 t,
283 ok,
284 "Set comparison of %s type: equivalent: %s == %s",
285 p[0].Value.Type(),
286 s0.Encoded(attribute.DefaultEncoder()),
287 s1.Encoded(attribute.DefaultEncoder()),
288 )
289 }
290 })
291 }
292
293 func TestAsSlice(t *testing.T) {
294 bs1 := []bool{true, false, true}
295 kv := attribute.BoolSlice("BoolSlice", bs1)
296 bs2 := kv.Value.AsBoolSlice()
297 assert.Equal(t, bs1, bs2)
298
299 i64s1 := []int64{12, 1298, -219, 2}
300 kv = attribute.Int64Slice("Int64Slice", i64s1)
301 i64s2 := kv.Value.AsInt64Slice()
302 assert.Equal(t, i64s1, i64s2)
303
304 is1 := []int{12, 1298, -219, 2}
305 kv = attribute.IntSlice("IntSlice", is1)
306 i64s2 = kv.Value.AsInt64Slice()
307 assert.Equal(t, i64s1, i64s2)
308
309 fs1 := []float64{12398.1, -37.1713873737, 3}
310 kv = attribute.Float64Slice("Float64Slice", fs1)
311 fs2 := kv.Value.AsFloat64Slice()
312 assert.Equal(t, fs1, fs2)
313
314 ss1 := []string{"one", "two", "three"}
315 kv = attribute.StringSlice("StringSlice", ss1)
316 ss2 := kv.Value.AsStringSlice()
317 assert.Equal(t, ss1, ss2)
318 }