...

Source file src/github.com/go-errors/errors/error_1_13_test.go

Documentation: github.com/go-errors/errors

     1  // +build go1.13
     2  
     3  package errors
     4  
     5  import (
     6  	"io"
     7  	"testing"
     8  )
     9  
    10  // This test should work only for go 1.13 and latter
    11  func TestIs113(t *testing.T) {
    12  	custErr := errorWithCustomIs{
    13  		Key: "TestForFun",
    14  		Err: io.EOF,
    15  	}
    16  
    17  	shouldMatch := errorWithCustomIs{
    18  		Key: "TestForFun",
    19  	}
    20  
    21  	shouldNotMatch := errorWithCustomIs{Key: "notOk"}
    22  
    23  	if !Is(custErr, shouldMatch) {
    24  		t.Errorf("custErr is not a TestForFun customError")
    25  	}
    26  
    27  	if Is(custErr, shouldNotMatch) {
    28  		t.Errorf("custErr is a notOk customError")
    29  	}
    30  
    31  	if !Is(custErr, New(shouldMatch)) {
    32  		t.Errorf("custErr is not a New(TestForFun customError)")
    33  	}
    34  
    35  	if Is(custErr, New(shouldNotMatch)) {
    36  		t.Errorf("custErr is a New(notOk customError)")
    37  	}
    38  
    39  	if !Is(New(custErr), shouldMatch) {
    40  		t.Errorf("New(custErr) is not a TestForFun customError")
    41  	}
    42  
    43  	if Is(New(custErr), shouldNotMatch) {
    44  		t.Errorf("New(custErr) is a notOk customError")
    45  	}
    46  
    47  	if !Is(New(custErr), New(shouldMatch)) {
    48  		t.Errorf("New(custErr) is not a New(TestForFun customError)")
    49  	}
    50  
    51  	if Is(New(custErr), New(shouldNotMatch)) {
    52  		t.Errorf("New(custErr) is a New(notOk customError)")
    53  	}
    54  }
    55  
    56  type errorWithCustomIs struct {
    57  	Key string
    58  	Err error
    59  }
    60  
    61  func (ewci errorWithCustomIs) Error() string {
    62  	return "[" + ewci.Key + "]: " + ewci.Err.Error()
    63  }
    64  
    65  func (ewci errorWithCustomIs) Is(target error) bool {
    66  	matched, ok := target.(errorWithCustomIs)
    67  	return ok && matched.Key == ewci.Key
    68  }
    69  

View as plain text