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