1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package attribute_test
16
17 import (
18 "testing"
19
20 "github.com/google/go-cmp/cmp"
21 "github.com/stretchr/testify/assert"
22
23 "go.opentelemetry.io/otel/attribute"
24 )
25
26 func TestKeyValueConstructors(t *testing.T) {
27 tt := []struct {
28 name string
29 actual attribute.KeyValue
30 expected attribute.KeyValue
31 }{
32 {
33 name: "Bool",
34 actual: attribute.Bool("k1", true),
35 expected: attribute.KeyValue{
36 Key: "k1",
37 Value: attribute.BoolValue(true),
38 },
39 },
40 {
41 name: "Int64",
42 actual: attribute.Int64("k1", 123),
43 expected: attribute.KeyValue{
44 Key: "k1",
45 Value: attribute.Int64Value(123),
46 },
47 },
48 {
49 name: "Float64",
50 actual: attribute.Float64("k1", 123.5),
51 expected: attribute.KeyValue{
52 Key: "k1",
53 Value: attribute.Float64Value(123.5),
54 },
55 },
56 {
57 name: "String",
58 actual: attribute.String("k1", "123.5"),
59 expected: attribute.KeyValue{
60 Key: "k1",
61 Value: attribute.StringValue("123.5"),
62 },
63 },
64 {
65 name: "Int",
66 actual: attribute.Int("k1", 123),
67 expected: attribute.KeyValue{
68 Key: "k1",
69 Value: attribute.IntValue(123),
70 },
71 },
72 }
73
74 for _, test := range tt {
75 t.Run(test.name, func(t *testing.T) {
76 if diff := cmp.Diff(test.actual, test.expected, cmp.AllowUnexported(attribute.Value{})); diff != "" {
77 t.Fatal(diff)
78 }
79 })
80 }
81 }
82
83 func TestKeyValueValid(t *testing.T) {
84 tests := []struct {
85 desc string
86 valid bool
87 kv attribute.KeyValue
88 }{
89 {
90 desc: "uninitialized KeyValue should be invalid",
91 valid: false,
92 kv: attribute.KeyValue{},
93 },
94 {
95 desc: "empty key value should be invalid",
96 valid: false,
97 kv: attribute.Key("").Bool(true),
98 },
99 {
100 desc: "INVALID value type should be invalid",
101 valid: false,
102 kv: attribute.KeyValue{
103 Key: attribute.Key("valid key"),
104
105 Value: attribute.Value{},
106 },
107 },
108 {
109 desc: "non-empty key with BOOL type Value should be valid",
110 valid: true,
111 kv: attribute.Bool("bool", true),
112 },
113 {
114 desc: "non-empty key with INT64 type Value should be valid",
115 valid: true,
116 kv: attribute.Int64("int64", 0),
117 },
118 {
119 desc: "non-empty key with FLOAT64 type Value should be valid",
120 valid: true,
121 kv: attribute.Float64("float64", 0),
122 },
123 {
124 desc: "non-empty key with STRING type Value should be valid",
125 valid: true,
126 kv: attribute.String("string", ""),
127 },
128 }
129
130 for _, test := range tests {
131 if got, want := test.kv.Valid(), test.valid; got != want {
132 t.Error(test.desc)
133 }
134 }
135 }
136
137 func TestIncorrectCast(t *testing.T) {
138 testCases := []struct {
139 name string
140 val attribute.Value
141 }{
142 {
143 name: "Float64",
144 val: attribute.Float64Value(1.0),
145 },
146 {
147 name: "Int64",
148 val: attribute.Int64Value(2),
149 },
150 {
151 name: "String",
152 val: attribute.BoolValue(true),
153 },
154 {
155 name: "Float64Slice",
156 val: attribute.Float64SliceValue([]float64{1.0}),
157 },
158 {
159 name: "Int64Slice",
160 val: attribute.Int64SliceValue([]int64{2}),
161 },
162 {
163 name: "StringSlice",
164 val: attribute.BoolSliceValue([]bool{true}),
165 },
166 }
167 for _, tt := range testCases {
168 t.Run(tt.name, func(t *testing.T) {
169 assert.NotPanics(t, func() {
170 tt.val.AsBool()
171 tt.val.AsBoolSlice()
172 tt.val.AsFloat64()
173 tt.val.AsFloat64Slice()
174 tt.val.AsInt64()
175 tt.val.AsInt64Slice()
176 tt.val.AsInterface()
177 tt.val.AsString()
178 tt.val.AsStringSlice()
179 })
180 })
181 }
182 }
183
View as plain text