1// Copyright The OpenTelemetry Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package label_test 16 17import ( 18 "reflect" 19 "testing" 20 "unsafe" 21 22 "github.com/google/go-cmp/cmp" 23 24 "go.opentelemetry.io/otel/label" 25) 26 27func TestValue(t *testing.T) { 28 k := label.Key("test") 29 bli := getBitlessInfo(42) 30 for _, testcase := range []struct { 31 name string 32 value label.Value 33 wantType label.Type 34 wantValue interface{} 35 }{ 36 { 37 name: "Key.Bool() correctly returns keys's internal bool value", 38 value: k.Bool(true).Value, 39 wantType: label.BOOL, 40 wantValue: true, 41 }, 42 { 43 name: "Key.Array([]bool) correctly return key's internal bool values", 44 value: k.Array([]bool{true, false}).Value, 45 wantType: label.ARRAY, 46 wantValue: [2]bool{true, false}, 47 }, 48 { 49 name: "Key.Int64() correctly returns keys's internal int64 value", 50 value: k.Int64(42).Value, 51 wantType: label.INT64, 52 wantValue: int64(42), 53 }, 54 { 55 name: "Key.Uint64() correctly returns keys's internal uint64 value", 56 value: k.Uint64(42).Value, 57 wantType: label.UINT64, 58 wantValue: uint64(42), 59 }, 60 { 61 name: "Key.Float64() correctly returns keys's internal float64 value", 62 value: k.Float64(42.1).Value, 63 wantType: label.FLOAT64, 64 wantValue: 42.1, 65 }, 66 { 67 name: "Key.Int32() correctly returns keys's internal int32 value", 68 value: k.Int32(42).Value, 69 wantType: label.INT32, 70 wantValue: int32(42), 71 }, 72 { 73 name: "Key.Uint32() correctly returns keys's internal uint32 value", 74 value: k.Uint32(42).Value, 75 wantType: label.UINT32, 76 wantValue: uint32(42), 77 }, 78 { 79 name: "Key.Float32() correctly returns keys's internal float32 value", 80 value: k.Float32(42.1).Value, 81 wantType: label.FLOAT32, 82 wantValue: float32(42.1), 83 }, 84 { 85 name: "Key.String() correctly returns keys's internal string value", 86 value: k.String("foo").Value, 87 wantType: label.STRING, 88 wantValue: "foo", 89 }, 90 { 91 name: "Key.Int() correctly returns keys's internal signed integral value", 92 value: k.Int(bli.intValue).Value, 93 wantType: bli.signedType, 94 wantValue: bli.signedValue, 95 }, 96 { 97 name: "Key.Uint() correctly returns keys's internal unsigned integral value", 98 value: k.Uint(bli.uintValue).Value, 99 wantType: bli.unsignedType, 100 wantValue: bli.unsignedValue, 101 }, 102 { 103 name: "Key.Array([]int64) correctly returns keys's internal int64 values", 104 value: k.Array([]int64{42, 43}).Value, 105 wantType: label.ARRAY, 106 wantValue: [2]int64{42, 43}, 107 }, 108 { 109 name: "KeyArray([]uint64) correctly returns keys's internal uint64 values", 110 value: k.Array([]uint64{42, 43}).Value, 111 wantType: label.ARRAY, 112 wantValue: [2]uint64{42, 43}, 113 }, 114 { 115 name: "Key.Array([]float64) correctly returns keys's internal float64 values", 116 value: k.Array([]float64{42, 43}).Value, 117 wantType: label.ARRAY, 118 wantValue: [2]float64{42, 43}, 119 }, 120 { 121 name: "Key.Array([]int32) correctly returns keys's internal int32 values", 122 value: k.Array([]int32{42, 43}).Value, 123 wantType: label.ARRAY, 124 wantValue: [2]int32{42, 43}, 125 }, 126 { 127 name: "Key.Array([]uint32) correctly returns keys's internal uint32 values", 128 value: k.Array([]uint32{42, 43}).Value, 129 wantType: label.ARRAY, 130 wantValue: [2]uint32{42, 43}, 131 }, 132 { 133 name: "Key.Array([]float32) correctly returns keys's internal float32 values", 134 value: k.Array([]float32{42, 43}).Value, 135 wantType: label.ARRAY, 136 wantValue: [2]float32{42, 43}, 137 }, 138 { 139 name: "Key.Array([]string) correctly return key's internal string values", 140 value: k.Array([]string{"foo", "bar"}).Value, 141 wantType: label.ARRAY, 142 wantValue: [2]string{"foo", "bar"}, 143 }, 144 { 145 name: "Key.Array([]int) correctly returns keys's internal signed integral values", 146 value: k.Array([]int{42, 43}).Value, 147 wantType: label.ARRAY, 148 wantValue: [2]int{42, 43}, 149 }, 150 { 151 name: "Key.Array([]uint) correctly returns keys's internal unsigned integral values", 152 value: k.Array([]uint{42, 43}).Value, 153 wantType: label.ARRAY, 154 wantValue: [2]uint{42, 43}, 155 }, 156 { 157 name: "Key.Array([][]int) refuses to create multi-dimensional array", 158 value: k.Array([][]int{{1, 2}, {3, 4}}).Value, 159 wantType: label.INVALID, 160 wantValue: nil, 161 }, 162 } { 163 t.Logf("Running test case %s", testcase.name) 164 if testcase.value.Type() != testcase.wantType { 165 t.Errorf("wrong value type, got %#v, expected %#v", testcase.value.Type(), testcase.wantType) 166 } 167 if testcase.wantType == label.INVALID { 168 continue 169 } 170 got := testcase.value.AsInterface() 171 if diff := cmp.Diff(testcase.wantValue, got); diff != "" { 172 t.Errorf("+got, -want: %s", diff) 173 } 174 } 175} 176 177type bitlessInfo struct { 178 intValue int 179 uintValue uint 180 signedType label.Type 181 unsignedType label.Type 182 signedValue interface{} 183 unsignedValue interface{} 184} 185 186func getBitlessInfo(i int) bitlessInfo { 187 if unsafe.Sizeof(i) == 4 { 188 return bitlessInfo{ 189 intValue: i, 190 uintValue: uint(i), 191 signedType: label.INT32, 192 unsignedType: label.UINT32, 193 signedValue: int32(i), 194 unsignedValue: uint32(i), 195 } 196 } 197 return bitlessInfo{ 198 intValue: i, 199 uintValue: uint(i), 200 signedType: label.INT64, 201 unsignedType: label.UINT64, 202 signedValue: int64(i), 203 unsignedValue: uint64(i), 204 } 205} 206 207func TestAsArrayValue(t *testing.T) { 208 v := label.ArrayValue([]uint{1, 2, 3}).AsArray() 209 // Ensure the returned dynamic type is stable. 210 if got, want := reflect.TypeOf(v).Kind(), reflect.Array; got != want { 211 t.Errorf("AsArray() returned %T, want %T", got, want) 212 } 213} 214