knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/observability/attributekey/key_test.go (about)

     1  /*
     2  Copyright 2025 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package attributekey
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	"github.com/google/go-cmp/cmp/cmpopts"
    24  	"go.opentelemetry.io/otel/attribute"
    25  )
    26  
    27  func TestWith(t *testing.T) {
    28  	cases := []struct {
    29  		name      string
    30  		got       attribute.KeyValue
    31  		want      attribute.KeyValue
    32  		wantPanic bool
    33  	}{{
    34  		name: "string",
    35  		got:  String("key").With("val"),
    36  		want: attribute.String("key", "val"),
    37  	}, {
    38  		name: "bool",
    39  		got:  Bool("key").With(true),
    40  		want: attribute.Bool("key", true),
    41  	}, {
    42  		name: "int",
    43  		got:  Int("key").With(1),
    44  		want: attribute.Int("key", 1),
    45  	}, {
    46  		name: "int64",
    47  		got:  Int64("key").With(1),
    48  		want: attribute.Int64("key", 1),
    49  	}, {
    50  		name: "float64",
    51  		got:  Float64("key").With(1),
    52  		want: attribute.Float64("key", 1),
    53  	}, {
    54  		name: "string slice",
    55  		got:  Type[[]string]("key").With([]string{"1"}),
    56  		want: attribute.StringSlice("key", []string{"1"}),
    57  	}, {
    58  		name: "bool slice",
    59  		got:  Type[[]bool]("key").With([]bool{true}),
    60  		want: attribute.BoolSlice("key", []bool{true}),
    61  	}, {
    62  		name: "float64 slice",
    63  		got:  Type[[]float64]("key").With([]float64{1}),
    64  		want: attribute.Float64Slice("key", []float64{1}),
    65  	}, {
    66  		name: "int64 slice",
    67  		got:  Type[[]int64]("key").With([]int64{1}),
    68  		want: attribute.Int64Slice("key", []int64{1}),
    69  	}, {
    70  		name: "int slice",
    71  		got:  Type[[]int]("key").With([]int{1}),
    72  		want: attribute.IntSlice("key", []int{1}),
    73  	}}
    74  
    75  	for _, tc := range cases {
    76  		t.Run(tc.name, func(t *testing.T) {
    77  			if diff := diff(tc.want, tc.got); diff != "" {
    78  				t.Errorf("mismatch (-want +got):\n%s", diff)
    79  			}
    80  		})
    81  	}
    82  }
    83  
    84  func diff(want, got any) string {
    85  	return cmp.Diff(want, got, cmpopts.EquateComparable(attribute.KeyValue{}))
    86  }