github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/common/errors/errors_test.go (about)

     1  package errors_test
     2  
     3  import (
     4  	"io"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  	. "github.com/xmplusdev/xmcore/common/errors"
    10  	"github.com/xmplusdev/xmcore/common/log"
    11  )
    12  
    13  func TestError(t *testing.T) {
    14  	err := New("TestError")
    15  	if v := GetSeverity(err); v != log.Severity_Info {
    16  		t.Error("severity: ", v)
    17  	}
    18  
    19  	err = New("TestError2").Base(io.EOF)
    20  	if v := GetSeverity(err); v != log.Severity_Info {
    21  		t.Error("severity: ", v)
    22  	}
    23  
    24  	err = New("TestError3").Base(io.EOF).AtWarning()
    25  	if v := GetSeverity(err); v != log.Severity_Warning {
    26  		t.Error("severity: ", v)
    27  	}
    28  
    29  	err = New("TestError4").Base(io.EOF).AtWarning()
    30  	err = New("TestError5").Base(err)
    31  	if v := GetSeverity(err); v != log.Severity_Warning {
    32  		t.Error("severity: ", v)
    33  	}
    34  	if v := err.Error(); !strings.Contains(v, "EOF") {
    35  		t.Error("error: ", v)
    36  	}
    37  }
    38  
    39  type e struct{}
    40  
    41  func TestErrorMessage(t *testing.T) {
    42  	data := []struct {
    43  		err error
    44  		msg string
    45  	}{
    46  		{
    47  			err: New("a").Base(New("b")).WithPathObj(e{}),
    48  			msg: "common/errors_test: a > b",
    49  		},
    50  		{
    51  			err: New("a").Base(New("b").WithPathObj(e{})),
    52  			msg: "a > common/errors_test: b",
    53  		},
    54  	}
    55  
    56  	for _, d := range data {
    57  		if diff := cmp.Diff(d.msg, d.err.Error()); diff != "" {
    58  			t.Error(diff)
    59  		}
    60  	}
    61  }