github.com/searKing/golang/go@v1.2.117/error/cause/cause_test.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cause_test
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"testing"
    11  
    12  	"github.com/searKing/golang/go/error/cause"
    13  )
    14  
    15  func TestWithError(t *testing.T) {
    16  	tests := []struct {
    17  		cause error
    18  		err   error
    19  		want  string
    20  	}{
    21  		{nil, nil, ""},
    22  		{io.EOF, nil, ""},
    23  		{nil, io.EOF, io.EOF.Error()},
    24  		{io.EOF, fmt.Errorf("read error"), "read error: EOF"},
    25  		{cause.WithError(io.EOF, fmt.Errorf("read error")), fmt.Errorf("client error"), "client error: read error: EOF"},
    26  	}
    27  
    28  	for _, tt := range tests {
    29  		var got string
    30  		err := cause.WithError(tt.cause, tt.err)
    31  		if err != nil {
    32  			got = err.Error()
    33  		}
    34  		if got != tt.want {
    35  			t.Errorf("WithError(%v, %q): got: %q, want %q", tt.cause, tt.err, got, tt.want)
    36  		}
    37  	}
    38  }