From 54c7f90b7fb6e62db3e3d44bb38cd87aee6c74df Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 15 Feb 2023 10:39:51 +0800 Subject: [PATCH] test: add examples for xerror package --- xerror/xerror_example_test.go | 131 ++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/xerror/xerror_example_test.go b/xerror/xerror_example_test.go index 20ca29a..1c4ebbd 100644 --- a/xerror/xerror_example_test.go +++ b/xerror/xerror_example_test.go @@ -1,11 +1,142 @@ package xerror import ( + "errors" "fmt" "reflect" "strconv" + "strings" ) +func ExampleNew() { + err := New("error") + fmt.Println(err.Error()) + + // Output: + // error +} + +func ExampleWrap() { + err := New("wrong password") + wrapErr := Wrap(err, "error") + + fmt.Println(wrapErr.Error()) + + // Output: + // error: wrong password +} + +func ExampleXError_Wrap() { + err1 := New("error").With("level", "high") + err2 := err1.Wrap(errors.New("invalid username")) + + fmt.Println(err2.Error()) + + // Output: + // error: invalid username +} + +func ExampleXError_Unwrap() { + err1 := New("error").With("level", "high") + err2 := err1.Wrap(errors.New("invalid username")) + + err := err2.Unwrap() + + fmt.Println(err.Error()) + + // Output: + // invalid username +} + +func ExampleXError_StackTrace() { + err := New("error") + + stacks := err.Stacks() + + fmt.Println(stacks[0].Func) + fmt.Println(stacks[0].Line) + + containFile := strings.Contains(stacks[0].File, "xerror_example_test.go") + fmt.Println(containFile) + + // Output: + // github.com/duke-git/lancet/v2/xerror.ExampleXError_StackTrace + // 49 + // true +} + +func ExampleXError_With() { + err := New("error").With("level", "high") + + errLevel := err.Values()["level"] + + fmt.Println(errLevel) + + // Output: + // high +} + +func ExampleXError_Id() { + err1 := New("error").Id("e001") + err2 := New("error").Id("e001") + err3 := New("error").Id("e003") + + equal := err1.Is(err2) + notEqual := err1.Is(err3) + + fmt.Println(equal) + fmt.Println(notEqual) + + // Output: + // true + // false +} + +func ExampleXError_Is() { + err1 := New("error").Id("e001") + err2 := New("error").Id("e001") + err3 := New("error").Id("e003") + + equal := err1.Is(err2) + notEqual := err1.Is(err3) + + fmt.Println(equal) + fmt.Println(notEqual) + + // Output: + // true + // false +} + +func ExampleXError_Values() { + err := New("error").With("level", "high") + + errLevel := err.Values()["level"] + + fmt.Println(errLevel) + + // Output: + // high +} + +func ExampleXError_Info() { + cause := errors.New("error") + err := Wrap(cause, "invalid username").Id("e001").With("level", "high") + + errInfo := err.Info() + + fmt.Println(errInfo.Id) + fmt.Println(errInfo.Cause) + fmt.Println(errInfo.Values["level"]) + fmt.Println(errInfo.Message) + + // Output: + // e001 + // error + // high + // invalid username +} + func ExampleTryUnwrap() { result1 := TryUnwrap(strconv.Atoi("42")) fmt.Println(result1)