2019-06-29 18:17:33 +08:00
|
|
|
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gerror_test
|
|
|
|
|
|
|
|
import (
|
2019-06-30 22:21:08 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-06-29 18:17:33 +08:00
|
|
|
"testing"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/errors/gerror"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-06-29 18:17:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func nilError() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Nil(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t.Assert(gerror.New(""), nil)
|
|
|
|
t.Assert(gerror.Wrap(nilError(), "test"), nil)
|
2019-06-29 18:17:33 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Wrap(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-09 10:40:26 +08:00
|
|
|
func Test_Cause(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-09 10:40:26 +08:00
|
|
|
err := errors.New("1")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gerror.Cause(err), err)
|
2019-07-09 10:40:26 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-09 10:40:26 +08:00
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gerror.Cause(err), "1")
|
2019-07-09 10:40:26 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-09 10:40:26 +08:00
|
|
|
err := gerror.New("1")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gerror.Cause(err), "1")
|
2019-07-09 10:40:26 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-09 10:40:26 +08:00
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gerror.Cause(err), "1")
|
2019-07-09 10:40:26 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-30 22:21:08 +08:00
|
|
|
func Test_Format(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(fmt.Sprintf("%s", err), "3: 2: 1")
|
|
|
|
t.Assert(fmt.Sprintf("%v", err), "3: 2: 1")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(fmt.Sprintf("%s", err), "3: 2: 1")
|
|
|
|
t.Assert(fmt.Sprintf("%v", err), "3: 2: 1")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(fmt.Sprintf("%-s", err), "3")
|
|
|
|
t.Assert(fmt.Sprintf("%-v", err), "3")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Stack(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := errors.New("1")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(fmt.Sprintf("%+v", err), "1")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-30 22:21:08 +08:00
|
|
|
//fmt.Printf("%+v", err)
|
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := gerror.New("1")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(fmt.Sprintf("%+v", err), "1")
|
2019-06-30 22:21:08 +08:00
|
|
|
//fmt.Printf("%+v", err)
|
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-29 23:35:32 +08:00
|
|
|
err := gerror.New("1")
|
2019-06-30 22:21:08 +08:00
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-30 22:21:08 +08:00
|
|
|
//fmt.Printf("%+v", err)
|
2019-06-29 18:17:33 +08:00
|
|
|
})
|
|
|
|
}
|