mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 10:08:15 -05:00
74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package errors_test
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
. "github.com/v2fly/v2ray-core/v4/common/errors"
|
|
"github.com/v2fly/v2ray-core/v4/common/log"
|
|
)
|
|
|
|
func TestError(t *testing.T) {
|
|
err := New("TestError")
|
|
if v := GetSeverity(err); v != log.Severity_Info {
|
|
t.Error("severity: ", v)
|
|
}
|
|
|
|
err = New("TestError2").Base(io.EOF)
|
|
if v := GetSeverity(err); v != log.Severity_Info {
|
|
t.Error("severity: ", v)
|
|
}
|
|
|
|
err = New("TestError3").Base(io.EOF).AtWarning()
|
|
if v := GetSeverity(err); v != log.Severity_Warning {
|
|
t.Error("severity: ", v)
|
|
}
|
|
|
|
err = New("TestError4").Base(io.EOF).AtWarning()
|
|
err = New("TestError5").Base(err)
|
|
if v := GetSeverity(err); v != log.Severity_Warning {
|
|
t.Error("severity: ", v)
|
|
}
|
|
if v := err.Error(); !strings.Contains(v, "EOF") {
|
|
t.Error("error: ", v)
|
|
}
|
|
}
|
|
|
|
type e struct{}
|
|
|
|
func TestErrorMessage(t *testing.T) {
|
|
pwd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
moduleName, gmnErr := common.GetModuleName(pwd)
|
|
if gmnErr != nil {
|
|
t.Error(gmnErr)
|
|
}
|
|
|
|
data := []struct {
|
|
err error
|
|
msg string
|
|
}{
|
|
{
|
|
err: New("a").Base(New("b")).WithPathObj(e{}),
|
|
msg: moduleName + "/common/errors_test: a > b",
|
|
},
|
|
{
|
|
err: New("a").Base(New("b").WithPathObj(e{})),
|
|
msg: "a > " + moduleName + "/common/errors_test: b",
|
|
},
|
|
}
|
|
|
|
for _, d := range data {
|
|
if diff := cmp.Diff(d.msg, d.err.Error()); diff != "" {
|
|
t.Error(diff)
|
|
}
|
|
}
|
|
}
|