github.com/cockroachdb/errors@v1.11.1/errbase/encode_test.go (about)

     1  // Copyright 2019 The Cockroach 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
    12  // implied. See the License for the specific language governing
    13  // permissions and limitations under the License.
    14  
    15  package errbase_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/cockroachdb/errors/errbase"
    21  	"github.com/cockroachdb/errors/testutils"
    22  )
    23  
    24  type myE struct{ marker string }
    25  
    26  func (e *myE) Error() string { return "woo" }
    27  
    28  func (e *myE) ErrorKeyMarker() string { return e.marker }
    29  
    30  var _ errbase.TypeKeyMarker = (*myE)(nil)
    31  
    32  // This test shows how the extended type marker changes the visible
    33  // type and thus the identity of an error.
    34  func TestTypeName(t *testing.T) {
    35  	err1 := &myE{"woo"}
    36  	err2 := &myE{""}
    37  
    38  	tn1 := errbase.GetTypeMark(err1)
    39  	tn2 := errbase.GetTypeMark(err2)
    40  
    41  	tt := testutils.T{T: t}
    42  
    43  	tt.Check(!tn1.Equals(tn2))
    44  	tt.CheckEqual(tn1.FamilyName, tn2.FamilyName)
    45  	tt.Check(tn1.Extension != tn2.Extension)
    46  }