2017-02-21 17:14:07 -05:00
|
|
|
package errors_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2019-02-10 09:02:28 -05:00
|
|
|
"strings"
|
2017-02-21 17:14:07 -05:00
|
|
|
"testing"
|
|
|
|
|
2018-11-15 05:17:20 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2017-02-21 17:14:07 -05:00
|
|
|
. "v2ray.com/core/common/errors"
|
2017-12-10 17:33:23 -05:00
|
|
|
"v2ray.com/core/common/log"
|
2017-02-21 17:14:07 -05:00
|
|
|
)
|
|
|
|
|
2017-04-06 15:13:17 -04:00
|
|
|
func TestError(t *testing.T) {
|
2017-02-21 17:14:07 -05:00
|
|
|
err := New("TestError")
|
2019-02-10 09:02:28 -05:00
|
|
|
if v := GetSeverity(err); v != log.Severity_Info {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
2017-02-21 17:14:07 -05:00
|
|
|
|
2017-04-06 09:13:09 -04:00
|
|
|
err = New("TestError2").Base(io.EOF)
|
2019-02-10 09:02:28 -05:00
|
|
|
if v := GetSeverity(err); v != log.Severity_Info {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
2017-02-21 17:14:07 -05:00
|
|
|
|
2017-04-06 09:13:09 -04:00
|
|
|
err = New("TestError3").Base(io.EOF).AtWarning()
|
2019-02-10 09:02:28 -05:00
|
|
|
if v := GetSeverity(err); v != log.Severity_Warning {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
2017-02-21 17:14:07 -05:00
|
|
|
|
2017-04-06 09:13:09 -04:00
|
|
|
err = New("TestError4").Base(io.EOF).AtWarning()
|
|
|
|
err = New("TestError5").Base(err)
|
2019-02-10 09:02:28 -05:00
|
|
|
if v := GetSeverity(err); v != log.Severity_Warning {
|
|
|
|
t.Error("severity: ", v)
|
|
|
|
}
|
|
|
|
if v := err.Error(); !strings.Contains(v, "EOF") {
|
|
|
|
t.Error("error: ", v)
|
|
|
|
}
|
2017-02-21 17:14:07 -05:00
|
|
|
}
|
2017-04-06 15:13:17 -04:00
|
|
|
|
2018-09-30 12:39:53 -04:00
|
|
|
type e struct{}
|
|
|
|
|
2017-04-06 15:13:17 -04:00
|
|
|
func TestErrorMessage(t *testing.T) {
|
|
|
|
data := []struct {
|
|
|
|
err error
|
|
|
|
msg string
|
|
|
|
}{
|
|
|
|
{
|
2018-09-30 12:39:53 -04:00
|
|
|
err: New("a").Base(New("b")).WithPathObj(e{}),
|
|
|
|
msg: "v2ray.com/core/common/errors_test: a > b",
|
2017-04-06 15:13:17 -04:00
|
|
|
},
|
|
|
|
{
|
2018-09-30 12:39:53 -04:00
|
|
|
err: New("a").Base(New("b").WithPathObj(e{})),
|
|
|
|
msg: "a > v2ray.com/core/common/errors_test: b",
|
2017-04-06 15:13:17 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range data {
|
2018-11-15 05:17:20 -05:00
|
|
|
if diff := cmp.Diff(d.msg, d.err.Error()); diff != "" {
|
|
|
|
t.Error(diff)
|
2018-07-13 08:36:09 -04:00
|
|
|
}
|
2017-04-06 15:13:17 -04:00
|
|
|
}
|
|
|
|
}
|