...

Source file src/go.opentelemetry.io/otel/attribute/value_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  	"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 TestValue(t *testing.T) {
    27  	k := attribute.Key("test")
    28  	for _, testcase := range []struct {
    29  		name      string
    30  		value     attribute.Value
    31  		wantType  attribute.Type
    32  		wantValue interface{}
    33  	}{
    34  		{
    35  			name:      "Key.Bool() correctly returns keys's internal bool value",
    36  			value:     k.Bool(true).Value,
    37  			wantType:  attribute.BOOL,
    38  			wantValue: true,
    39  		},
    40  		{
    41  			name:      "Key.BoolSlice() correctly returns keys's internal []bool value",
    42  			value:     k.BoolSlice([]bool{true, false, true}).Value,
    43  			wantType:  attribute.BOOLSLICE,
    44  			wantValue: []bool{true, false, true},
    45  		},
    46  		{
    47  			name:      "Key.Int64() correctly returns keys's internal int64 value",
    48  			value:     k.Int64(42).Value,
    49  			wantType:  attribute.INT64,
    50  			wantValue: int64(42),
    51  		},
    52  		{
    53  			name:      "Key.Int64Slice() correctly returns keys's internal []int64 value",
    54  			value:     k.Int64Slice([]int64{42, -3, 12}).Value,
    55  			wantType:  attribute.INT64SLICE,
    56  			wantValue: []int64{42, -3, 12},
    57  		},
    58  		{
    59  			name:      "Key.Int() correctly returns keys's internal signed integral value",
    60  			value:     k.Int(42).Value,
    61  			wantType:  attribute.INT64,
    62  			wantValue: int64(42),
    63  		},
    64  		{
    65  			name:      "Key.IntSlice() correctly returns keys's internal []int64 value",
    66  			value:     k.IntSlice([]int{42, -3, 12}).Value,
    67  			wantType:  attribute.INT64SLICE,
    68  			wantValue: []int64{42, -3, 12},
    69  		},
    70  		{
    71  			name:      "Key.Float64() correctly returns keys's internal float64 value",
    72  			value:     k.Float64(42.1).Value,
    73  			wantType:  attribute.FLOAT64,
    74  			wantValue: 42.1,
    75  		},
    76  		{
    77  			name:      "Key.Float64Slice() correctly returns keys's internal []float64 value",
    78  			value:     k.Float64Slice([]float64{42, -3, 12}).Value,
    79  			wantType:  attribute.FLOAT64SLICE,
    80  			wantValue: []float64{42, -3, 12},
    81  		},
    82  		{
    83  			name:      "Key.String() correctly returns keys's internal string value",
    84  			value:     k.String("foo").Value,
    85  			wantType:  attribute.STRING,
    86  			wantValue: "foo",
    87  		},
    88  		{
    89  			name:      "Key.StringSlice() correctly returns keys's internal []string value",
    90  			value:     k.StringSlice([]string{"forty-two", "negative three", "twelve"}).Value,
    91  			wantType:  attribute.STRINGSLICE,
    92  			wantValue: []string{"forty-two", "negative three", "twelve"},
    93  		},
    94  	} {
    95  		t.Logf("Running test case %s", testcase.name)
    96  		if testcase.value.Type() != testcase.wantType {
    97  			t.Errorf("wrong value type, got %#v, expected %#v", testcase.value.Type(), testcase.wantType)
    98  		}
    99  		if testcase.wantType == attribute.INVALID {
   100  			continue
   101  		}
   102  		got := testcase.value.AsInterface()
   103  		if diff := cmp.Diff(testcase.wantValue, got); diff != "" {
   104  			t.Errorf("+got, -want: %s", diff)
   105  		}
   106  	}
   107  }
   108  
   109  func TestSetComparability(t *testing.T) {
   110  	pairs := [][2]attribute.KeyValue{
   111  		{
   112  			attribute.Bool("Bool", true),
   113  			attribute.Bool("Bool", true),
   114  		},
   115  		{
   116  			attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
   117  			attribute.BoolSlice("BoolSlice", []bool{true, false, true}),
   118  		},
   119  		{
   120  			attribute.Int("Int", 34),
   121  			attribute.Int("Int", 34),
   122  		},
   123  		{
   124  			attribute.IntSlice("IntSlice", []int{312, 1, -2}),
   125  			attribute.IntSlice("IntSlice", []int{312, 1, -2}),
   126  		},
   127  		{
   128  			attribute.Int64("Int64", 98),
   129  			attribute.Int64("Int64", 98),
   130  		},
   131  		{
   132  			attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
   133  			attribute.Int64Slice("Int64Slice", []int64{12, 1298, -219, 2}),
   134  		},
   135  		{
   136  			attribute.Float64("Float64", 19.09),
   137  			attribute.Float64("Float64", 19.09),
   138  		},
   139  		{
   140  			attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
   141  			attribute.Float64Slice("Float64Slice", []float64{12398.1, -37.1713873737, 3}),
   142  		},
   143  		{
   144  			attribute.String("String", "string value"),
   145  			attribute.String("String", "string value"),
   146  		},
   147  		{
   148  			attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
   149  			attribute.StringSlice("StringSlice", []string{"one", "two", "three"}),
   150  		},
   151  	}
   152  
   153  	for _, p := range pairs {
   154  		s0, s1 := attribute.NewSet(p[0]), attribute.NewSet(p[1])
   155  		m := map[attribute.Set]struct{}{s0: {}}
   156  		_, ok := m[s1]
   157  		assert.Truef(t, ok, "%s not comparable", p[0].Value.Type())
   158  	}
   159  }
   160  
   161  func TestAsSlice(t *testing.T) {
   162  	bs1 := []bool{true, false, true}
   163  	kv := attribute.BoolSlice("BoolSlice", bs1)
   164  	bs2 := kv.Value.AsBoolSlice()
   165  	assert.Equal(t, bs1, bs2)
   166  
   167  	i64s1 := []int64{12, 1298, -219, 2}
   168  	kv = attribute.Int64Slice("Int64Slice", i64s1)
   169  	i64s2 := kv.Value.AsInt64Slice()
   170  	assert.Equal(t, i64s1, i64s2)
   171  
   172  	is1 := []int{12, 1298, -219, 2}
   173  	kv = attribute.IntSlice("IntSlice", is1)
   174  	i64s2 = kv.Value.AsInt64Slice()
   175  	assert.Equal(t, i64s1, i64s2)
   176  
   177  	fs1 := []float64{12398.1, -37.1713873737, 3}
   178  	kv = attribute.Float64Slice("Float64Slice", fs1)
   179  	fs2 := kv.Value.AsFloat64Slice()
   180  	assert.Equal(t, fs1, fs2)
   181  
   182  	ss1 := []string{"one", "two", "three"}
   183  	kv = attribute.StringSlice("StringSlice", ss1)
   184  	ss2 := kv.Value.AsStringSlice()
   185  	assert.Equal(t, ss1, ss2)
   186  }
   187  

View as plain text