gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/attributes/attributes_test.go (about)

     1  /*
     2   *
     3   * Copyright 2019 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package attributes_test
    20  
    21  import (
    22  	"fmt"
    23  	"testing"
    24  
    25  	"gitee.com/ks-custle/core-gm/grpc/attributes"
    26  )
    27  
    28  type stringVal struct {
    29  	s string
    30  }
    31  
    32  func (s stringVal) Equal(o interface{}) bool {
    33  	os, ok := o.(stringVal)
    34  	return ok && s.s == os.s
    35  }
    36  
    37  func ExampleAttributes() {
    38  	type keyOne struct{}
    39  	type keyTwo struct{}
    40  	a := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"})
    41  	fmt.Println("Key one:", a.Value(keyOne{}))
    42  	fmt.Println("Key two:", a.Value(keyTwo{}))
    43  	// Output:
    44  	// Key one: 1
    45  	// Key two: {two}
    46  }
    47  
    48  func ExampleAttributes_WithValue() {
    49  	type keyOne struct{}
    50  	type keyTwo struct{}
    51  	a := attributes.New(keyOne{}, 1)
    52  	a = a.WithValue(keyTwo{}, stringVal{s: "two"})
    53  	fmt.Println("Key one:", a.Value(keyOne{}))
    54  	fmt.Println("Key two:", a.Value(keyTwo{}))
    55  	// Output:
    56  	// Key one: 1
    57  	// Key two: {two}
    58  }
    59  
    60  // Test that two attributes with the same content are Equal.
    61  func TestEqual(t *testing.T) {
    62  	type keyOne struct{}
    63  	type keyTwo struct{}
    64  	a1 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"})
    65  	a2 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"})
    66  	if !a1.Equal(a2) {
    67  		t.Fatalf("%+v.Equals(%+v) = false; want true", a1, a2)
    68  	}
    69  	if !a2.Equal(a1) {
    70  		t.Fatalf("%+v.Equals(%+v) = false; want true", a2, a1)
    71  	}
    72  }
    73  
    74  // Test that two attributes with different content are not Equal.
    75  func TestNotEqual(t *testing.T) {
    76  	type keyOne struct{}
    77  	type keyTwo struct{}
    78  	a1 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"})
    79  	a2 := attributes.New(keyOne{}, 2).WithValue(keyTwo{}, stringVal{s: "two"})
    80  	a3 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "one"})
    81  	if a1.Equal(a2) {
    82  		t.Fatalf("%+v.Equals(%+v) = true; want false", a1, a2)
    83  	}
    84  	if a2.Equal(a1) {
    85  		t.Fatalf("%+v.Equals(%+v) = true; want false", a2, a1)
    86  	}
    87  	if a3.Equal(a1) {
    88  		t.Fatalf("%+v.Equals(%+v) = true; want false", a3, a1)
    89  	}
    90  }