...

Source file src/go.opentelemetry.io/otel/internal/attribute/attribute_test.go

Documentation: go.opentelemetry.io/otel/internal/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
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  var wrapFloat64SliceValue = func(v interface{}) interface{} {
    23  	if vi, ok := v.([]float64); ok {
    24  		return Float64SliceValue(vi)
    25  	}
    26  	return nil
    27  }
    28  
    29  var wrapInt64SliceValue = func(v interface{}) interface{} {
    30  	if vi, ok := v.([]int64); ok {
    31  		return Int64SliceValue(vi)
    32  	}
    33  	return nil
    34  }
    35  
    36  var wrapBoolSliceValue = func(v interface{}) interface{} {
    37  	if vi, ok := v.([]bool); ok {
    38  		return BoolSliceValue(vi)
    39  	}
    40  	return nil
    41  }
    42  
    43  var wrapStringSliceValue = func(v interface{}) interface{} {
    44  	if vi, ok := v.([]string); ok {
    45  		return StringSliceValue(vi)
    46  	}
    47  	return nil
    48  }
    49  
    50  var (
    51  	wrapAsBoolSlice    = func(v interface{}) interface{} { return AsBoolSlice(v) }
    52  	wrapAsInt64Slice   = func(v interface{}) interface{} { return AsInt64Slice(v) }
    53  	wrapAsFloat64Slice = func(v interface{}) interface{} { return AsFloat64Slice(v) }
    54  	wrapAsStringSlice  = func(v interface{}) interface{} { return AsStringSlice(v) }
    55  )
    56  
    57  func TestSliceValue(t *testing.T) {
    58  	type args struct {
    59  		v interface{}
    60  	}
    61  	tests := []struct {
    62  		name string
    63  		args args
    64  		want interface{}
    65  		fn   func(interface{}) interface{}
    66  	}{
    67  		{
    68  			name: "Float64SliceValue() two items",
    69  			args: args{v: []float64{1, 2.3}}, want: [2]float64{1, 2.3}, fn: wrapFloat64SliceValue,
    70  		},
    71  		{
    72  			name: "Int64SliceValue() two items",
    73  			args: args{[]int64{1, 2}}, want: [2]int64{1, 2}, fn: wrapInt64SliceValue,
    74  		},
    75  		{
    76  			name: "BoolSliceValue() two items",
    77  			args: args{v: []bool{true, false}}, want: [2]bool{true, false}, fn: wrapBoolSliceValue,
    78  		},
    79  		{
    80  			name: "StringSliceValue() two items",
    81  			args: args{[]string{"123", "2"}}, want: [2]string{"123", "2"}, fn: wrapStringSliceValue,
    82  		},
    83  		{
    84  			name: "AsBoolSlice() two items",
    85  			args: args{[2]bool{true, false}}, want: []bool{true, false}, fn: wrapAsBoolSlice,
    86  		},
    87  		{
    88  			name: "AsInt64Slice() two items",
    89  			args: args{[2]int64{1, 3}}, want: []int64{1, 3}, fn: wrapAsInt64Slice,
    90  		},
    91  		{
    92  			name: "AsFloat64Slice() two items",
    93  			args: args{[2]float64{1.2, 3.1}}, want: []float64{1.2, 3.1}, fn: wrapAsFloat64Slice,
    94  		},
    95  		{
    96  			name: "AsStringSlice() two items",
    97  			args: args{[2]string{"1234", "12"}}, want: []string{"1234", "12"}, fn: wrapAsStringSlice,
    98  		},
    99  	}
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			if got := tt.fn(tt.args.v); !reflect.DeepEqual(got, tt.want) {
   103  				t.Errorf("got %v, want %v", got, tt.want)
   104  			}
   105  		})
   106  	}
   107  }
   108  

View as plain text