1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package attribute_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
)
func TestValue(t *testing.T) {
k := attribute.Key("test")
for _, testcase := range []struct {
name string
value attribute.Value
wantType attribute.Type
wantValue interface{}
}{
{
name: "Key.Bool() correctly returns keys's internal bool value",
value: k.Bool(true).Value,
wantType: attribute.BOOL,
wantValue: true,
},
{
name: "Key.BoolSlice() correctly returns keys's internal []bool value",
value: k.BoolSlice([]bool{true, false, true}).Value,
wantType: attribute.BOOLSLICE,
wantValue: []bool{true, false, true},
},
{
name: "Key.Int64() correctly returns keys's internal int64 value",
value: k.Int64(42).Value,
wantType: attribute.INT64,
wantValue: int64(42),
},
{
name: "Key.Int64() correctly returns negative keys's internal int64 value",
value: k.Int64(-42).Value,
wantType: attribute.INT64,
wantValue: int64(-42),
},
{
name: "Key.Int64Slice() correctly returns keys's internal []int64 value",
value: k.Int64Slice([]int64{42, -3, 12}).Value,
wantType: attribute.INT64SLICE,
wantValue: []int64{42, -3, 12},
},
{
name: "Key.Int() correctly returns keys's internal signed integral value",
value: k.Int(42).Value,
wantType: attribute.INT64,
wantValue: int64(42),
},
{
name: "Key.IntSlice() correctly returns keys's internal []int64 value",
value: k.IntSlice([]int{42, -3, 12}).Value,
wantType: attribute.INT64SLICE,
wantValue: []int64{42, -3, 12},
},
{
name: "Key.Float64() correctly returns keys's internal float64 value",
value: k.Float64(42.1).Value,
wantType: attribute.FLOAT64,
wantValue: 42.1,
},
{
name: "Key.Float64Slice() correctly returns keys's internal []float64 value",
value: k.Float64Slice([]float64{42, -3, 12}).Value,
wantType: attribute.FLOAT64SLICE,
wantValue: []float64{42, -3, 12},
},
{
name: "Key.String() correctly returns keys's internal string value",
value: k.String("foo").Value,
wantType: attribute.STRING,
wantValue: "foo",
},
{
name: "Key.StringSlice() correctly returns keys's internal []string value",
value: k.StringSlice([]string{"forty-two", "negative three", "twelve"}).Value,
wantType: attribute.STRINGSLICE,
wantValue: []string{"forty-two", "negative three", "twelve"},
},
} {
t.Logf("Running test case %s", testcase.name)
if testcase.value.Type() != testcase.wantType {
t.Errorf("wrong value type, got %#v, expected %#v", testcase.value.Type(), testcase.wantType)
}
if testcase.wantType == attribute.INVALID {
continue
}
got := testcase.value.AsInterface()
if diff := cmp.Diff(testcase.wantValue, got); diff != "" {
t.Errorf("+got, -want: %s", diff)
}
}
}
func TestEquivalence(t *testing.T) {
pairs := [][2]attribute.KeyValue{
{
attribute.Bool("Bool", true),
attribute.Bool("Bool", true),
},
{
attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
},
{
attribute.Int("Int", 34),
attribute.Int("Int", 34),
},
{
attribute.IntSlice("IntSlice", []int{312, 1, -2}),
attribute.IntSlice("IntSlice", []int{312, 1, -2}),
},
{
attribute.Int64("Int64", 98),
attribute.Int64("Int64", 98),
},
{
attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
},
{
attribute.Float64("Float64", 19.09),
attribute.Float64("Float64", 19.09),
},
{
attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
},
{
attribute.String("String", "string value"),
attribute.String("String", "string value"),
},
{
attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
},
}
t.Run("Distinct", func(t *testing.T) {
for _, p := range pairs {
s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
m := map[attribute.Distinct]struct{}{s0.Equivalent(): {}}
_, ok := m[s1.Equivalent()]
assert.Truef(t, ok, "Distinct comparison of %s type: not equivalent", p[0].Value.Type())
assert.Truef(
t,
ok,
"Distinct comparison of %s type: not equivalent: %s != %s",
p[0].Value.Type(),
s0.Encoded(attribute.DefaultEncoder()),
s1.Encoded(attribute.DefaultEncoder()),
)
}
})
t.Run("Set", func(t *testing.T) {
// Maintain backwards compatibility.
for _, p := range pairs {
s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
m := map[attribute.Set]struct{}{s0: {}}
_, ok := m[s1]
assert.Truef(
t,
ok,
"Set comparison of %s type: not equivalent: %s != %s",
p[0].Value.Type(),
s0.Encoded(attribute.DefaultEncoder()),
s1.Encoded(attribute.DefaultEncoder()),
)
}
})
}
func TestAsSlice(t *testing.T) {
bs1 := []bool{true, false, true}
kv := attribute.BoolSlice("BoolSlice", bs1)
bs2 := kv.Value.AsBoolSlice()
assert.Equal(t, bs1, bs2)
i64s1 := []int64{12, 1298, -219, 2}
kv = attribute.Int64Slice("Int64Slice", i64s1)
i64s2 := kv.Value.AsInt64Slice()
assert.Equal(t, i64s1, i64s2)
is1 := []int{12, 1298, -219, 2}
kv = attribute.IntSlice("IntSlice", is1)
i64s2 = kv.Value.AsInt64Slice()
assert.Equal(t, i64s1, i64s2)
fs1 := []float64{12398.1, -37.1713873737, 3}
kv = attribute.Float64Slice("Float64Slice", fs1)
fs2 := kv.Value.AsFloat64Slice()
assert.Equal(t, fs1, fs2)
ss1 := []string{"one", "two", "three"}
kv = attribute.StringSlice("StringSlice", ss1)
ss2 := kv.Value.AsStringSlice()
assert.Equal(t, ss1, ss2)
}
|