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 "encoding/json" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 23 "go.opentelemetry.io/otel/label" 24) 25 26func TestDefined(t *testing.T) { 27 for _, testcase := range []struct { 28 name string 29 k label.Key 30 want bool 31 }{ 32 { 33 name: "Key.Defined() returns true when len(v.Name) != 0", 34 k: label.Key("foo"), 35 want: true, 36 }, 37 { 38 name: "Key.Defined() returns false when len(v.Name) == 0", 39 k: label.Key(""), 40 want: false, 41 }, 42 } { 43 t.Run(testcase.name, func(t *testing.T) { 44 //func (k label.Key) Defined() bool { 45 have := testcase.k.Defined() 46 if have != testcase.want { 47 t.Errorf("Want: %v, but have: %v", testcase.want, have) 48 } 49 }) 50 } 51} 52 53func TestJSONValue(t *testing.T) { 54 var kvs interface{} = [2]label.KeyValue{ 55 label.String("A", "B"), 56 label.Int64("C", 1), 57 } 58 59 data, err := json.Marshal(kvs) 60 require.NoError(t, err) 61 require.Equal(t, 62 `[{"Key":"A","Value":{"Type":"STRING","Value":"B"}},{"Key":"C","Value":{"Type":"INT64","Value":1}}]`, 63 string(data)) 64} 65 66func TestEmit(t *testing.T) { 67 for _, testcase := range []struct { 68 name string 69 v label.Value 70 want string 71 }{ 72 { 73 name: `test Key.Emit() can emit a string representing self.BOOL`, 74 v: label.BoolValue(true), 75 want: "true", 76 }, 77 { 78 name: `test Key.Emit() can emit a string representing self.INT32`, 79 v: label.Int32Value(42), 80 want: "42", 81 }, 82 { 83 name: `test Key.Emit() can emit a string representing self.INT64`, 84 v: label.Int64Value(42), 85 want: "42", 86 }, 87 { 88 name: `test Key.Emit() can emit a string representing self.UINT32`, 89 v: label.Uint32Value(42), 90 want: "42", 91 }, 92 { 93 name: `test Key.Emit() can emit a string representing self.UINT64`, 94 v: label.Uint64Value(42), 95 want: "42", 96 }, 97 { 98 name: `test Key.Emit() can emit a string representing self.FLOAT32`, 99 v: label.Float32Value(42.1), 100 want: "42.1", 101 }, 102 { 103 name: `test Key.Emit() can emit a string representing self.FLOAT64`, 104 v: label.Float64Value(42.1), 105 want: "42.1", 106 }, 107 { 108 name: `test Key.Emit() can emit a string representing self.STRING`, 109 v: label.StringValue("foo"), 110 want: "foo", 111 }, 112 } { 113 t.Run(testcase.name, func(t *testing.T) { 114 //proto: func (v label.Value) Emit() string { 115 have := testcase.v.Emit() 116 if have != testcase.want { 117 t.Errorf("Want: %s, but have: %s", testcase.want, have) 118 } 119 }) 120 } 121} 122