tlog.app/go/errors@v0.9.0/errors_test.go (about) 1 //nolint:errorlint 2 package errors 3 4 import ( 5 "os" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "tlog.app/go/loc" 10 ) 11 12 func TestError(t *testing.T) { 13 assert.Equal(t, "qwe", New("qwe").Error()) 14 assert.Equal(t, nomessage, New("").Error()) 15 assert.Equal(t, "qwe", Wrap(New("qwe"), "").Error()) 16 } 17 18 type testWrapper struct { //nolint:errname 19 err error 20 } 21 22 func TestWrapperError(t *testing.T) { 23 assert.EqualError(t, New("qwe"), "qwe") 24 assert.EqualError(t, NewNoCaller("qwe %v %v", 1, 2), "qwe 1 2") 25 assert.EqualError(t, Wrap(New("qwe %v %v", 1, 2), "context %v %v", "a", "b"), "context a b: qwe 1 2") 26 assert.EqualError(t, WrapNoCaller(New("qwe %v %v", 1, 2), "context %v %v", "a", "b"), "context a b: qwe 1 2") 27 assert.EqualError(t, Wrap(New("qwe %v %v", 1, 2), "context").(interface{ Unwrap() error }).Unwrap(), "qwe 1 2") 28 assert.EqualError(t, Unwrap(Wrap(New("qwe %v %v", 1, 2), "context")), "qwe 1 2") 29 } 30 31 //nolint:goerr113 32 func TestUnwrap(t *testing.T) { 33 mid := Wrap(os.ErrNotExist, "middle") 34 err := Wrap(mid, "global") 35 36 assert.True(t, os.ErrNotExist == Unwrap(mid)) 37 38 assert.True(t, mid == Unwrap(err)) 39 40 assert.True(t, mid == Unwrap(testWrapper{mid})) 41 42 assert.NoError(t, Unwrap(os.ErrNotExist)) 43 } 44 45 func TestWrapNil(t *testing.T) { 46 var err error 47 48 assert.NoError(t, Wrap(err, "qwe")) 49 assert.NoError(t, WrapNoCaller(err, "qwe")) 50 assert.NoError(t, WrapDepth(err, 0, "qwe")) 51 assert.NoError(t, WrapStack(err, 0, 0, "qwe")) 52 assert.NoError(t, WrapCaller(err, loc.FuncEntry(0), "qwe")) 53 assert.NoError(t, WrapCallers(err, loc.Callers(0, 1), "qwe")) 54 } 55 56 func (w testWrapper) Error() string { return "none" } 57 58 func (w testWrapper) Unwrap() error { return w.err }