github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/status/status_ext_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 status_test
    20  
    21  import (
    22  	"errors"
    23  	"testing"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  	"github.com/hxx258456/ccgo/grpc/codes"
    27  	"github.com/hxx258456/ccgo/grpc/internal/grpctest"
    28  	"github.com/hxx258456/ccgo/grpc/status"
    29  	"github.com/hxx258456/ccgo/grpc/test/grpc_testing"
    30  )
    31  
    32  type s struct {
    33  	grpctest.Tester
    34  }
    35  
    36  func Test(t *testing.T) {
    37  	grpctest.RunSubTests(t, s{})
    38  }
    39  
    40  func errWithDetails(t *testing.T, s *status.Status, details ...proto.Message) error {
    41  	t.Helper()
    42  	res, err := s.WithDetails(details...)
    43  	if err != nil {
    44  		t.Fatalf("(%v).WithDetails(%v) = %v, %v; want _, <nil>", s, details, res, err)
    45  	}
    46  	return res.Err()
    47  }
    48  
    49  func (s) TestErrorIs(t *testing.T) {
    50  	// Test errors.
    51  	testErr := status.Error(codes.Internal, "internal server error")
    52  	testErrWithDetails := errWithDetails(t, status.New(codes.Internal, "internal server error"), &grpc_testing.Empty{})
    53  
    54  	// Test cases.
    55  	testCases := []struct {
    56  		err1, err2 error
    57  		want       bool
    58  	}{
    59  		{err1: testErr, err2: nil, want: false},
    60  		{err1: testErr, err2: status.Error(codes.Internal, "internal server error"), want: true},
    61  		{err1: testErr, err2: status.Error(codes.Internal, "internal error"), want: false},
    62  		{err1: testErr, err2: status.Error(codes.Unknown, "internal server error"), want: false},
    63  		{err1: testErr, err2: errors.New("non-grpc error"), want: false},
    64  		{err1: testErrWithDetails, err2: status.Error(codes.Internal, "internal server error"), want: false},
    65  		{err1: testErrWithDetails, err2: errWithDetails(t, status.New(codes.Internal, "internal server error"), &grpc_testing.Empty{}), want: true},
    66  		{err1: testErrWithDetails, err2: errWithDetails(t, status.New(codes.Internal, "internal server error"), &grpc_testing.Empty{}, &grpc_testing.Empty{}), want: false},
    67  	}
    68  
    69  	for _, tc := range testCases {
    70  		isError, ok := tc.err1.(interface{ Is(target error) bool })
    71  		if !ok {
    72  			t.Errorf("(%v) does not implement is", tc.err1)
    73  			continue
    74  		}
    75  
    76  		is := isError.Is(tc.err2)
    77  		if is != tc.want {
    78  			t.Errorf("(%v).Is(%v) = %t; want %t", tc.err1, tc.err2, is, tc.want)
    79  		}
    80  	}
    81  }