...

Source file src/go.opentelemetry.io/otel/attribute/key_test.go

Documentation: go.opentelemetry.io/otel/attribute

     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  
    15  package attribute_test
    16  
    17  import (
    18  	"encoding/json"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"go.opentelemetry.io/otel/attribute"
    24  )
    25  
    26  func TestDefined(t *testing.T) {
    27  	for _, testcase := range []struct {
    28  		name string
    29  		k    attribute.Key
    30  		want bool
    31  	}{
    32  		{
    33  			name: "Key.Defined() returns true when len(v.Name) != 0",
    34  			k:    attribute.Key("foo"),
    35  			want: true,
    36  		},
    37  		{
    38  			name: "Key.Defined() returns false when len(v.Name) == 0",
    39  			k:    attribute.Key(""),
    40  			want: false,
    41  		},
    42  	} {
    43  		t.Run(testcase.name, func(t *testing.T) {
    44  			// func (k attribute.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  
    53  func TestJSONValue(t *testing.T) {
    54  	var kvs interface{} = [2]attribute.KeyValue{
    55  		attribute.String("A", "B"),
    56  		attribute.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  
    66  func TestEmit(t *testing.T) {
    67  	for _, testcase := range []struct {
    68  		name string
    69  		v    attribute.Value
    70  		want string
    71  	}{
    72  		{
    73  			name: `test Key.Emit() can emit a string representing self.BOOL`,
    74  			v:    attribute.BoolValue(true),
    75  			want: "true",
    76  		},
    77  		{
    78  			name: `test Key.Emit() can emit a string representing self.INT64`,
    79  			v:    attribute.Int64Value(42),
    80  			want: "42",
    81  		},
    82  		{
    83  			name: `test Key.Emit() can emit a string representing self.FLOAT64`,
    84  			v:    attribute.Float64Value(42.1),
    85  			want: "42.1",
    86  		},
    87  		{
    88  			name: `test Key.Emit() can emit a string representing self.STRING`,
    89  			v:    attribute.StringValue("foo"),
    90  			want: "foo",
    91  		},
    92  	} {
    93  		t.Run(testcase.name, func(t *testing.T) {
    94  			// proto: func (v attribute.Value) Emit() string {
    95  			have := testcase.v.Emit()
    96  			if have != testcase.want {
    97  				t.Errorf("Want: %s, but have: %s", testcase.want, have)
    98  			}
    99  		})
   100  	}
   101  }
   102  

View as plain text