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