github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/f/error_test.go (about)

     1  package f_test
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/angenalZZZ/gofunc/f"
     6  	"testing"
     7  )
     8  
     9  func TestErrorsToString(t *testing.T) {
    10  	t.Parallel()
    11  	customErr := &f.Error{Name: "Custom Error Name", Err: fmt.Errorf("stdlib error")}
    12  	customErrWithCustomErrorMessage := &f.Error{Name: "Custom Error Name 2", Err: fmt.Errorf("Bad stuff happened"), CustomErrorMessageExists: true}
    13  
    14  	var tests = []struct {
    15  		param1   f.Errors
    16  		expected string
    17  	}{
    18  		{f.Errors{}, ""},
    19  		{f.Errors{fmt.Errorf("Error 1")}, "Error 1"},
    20  		{f.Errors{fmt.Errorf("Error 1"), fmt.Errorf("Error 2")}, "Error 1;Error 2"},
    21  		{f.Errors{customErr, fmt.Errorf("Error 2")}, "Custom Error Name: stdlib error;Error 2"},
    22  		{f.Errors{fmt.Errorf("Error 123"), customErrWithCustomErrorMessage}, "Error 123;Bad stuff happened"},
    23  	}
    24  	for _, test := range tests {
    25  		actual := test.param1.Error()
    26  		if actual != test.expected {
    27  			t.Errorf("Expected Error() to return '%v', got '%v'", test.expected, actual)
    28  		}
    29  	}
    30  }