1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/xerrors"
15 func TestNewEqual(t *testing.T) {
16 // Different allocations should not be equal.
17 if xerrors.New("abc") == xerrors.New("abc") {
18 t.Errorf(`New("abc") == New("abc")`)
20 if xerrors.New("abc") == xerrors.New("xyz") {
21 t.Errorf(`New("abc") == New("xyz")`)
24 // Same allocation should be equal to itself (not crash).
25 err := xerrors.New("jkl")
27 t.Errorf(`err != err`)
31 func TestErrorMethod(t *testing.T) {
32 err := xerrors.New("abc")
33 if err.Error() != "abc" {
34 t.Errorf(`New("abc").Error() = %q, want %q`, err.Error(), "abc")
38 func TestNewDetail(t *testing.T) {
39 got := fmt.Sprintf("%+v", xerrors.New("error"))
40 want := `(?s)error:.+errors_test.go:\d+`
41 ok, err := regexp.MatchString(want, got)
46 t.Errorf(`fmt.Sprintf("%%+v", New("error")) = %q, want %q"`, got, want)
51 err := xerrors.New("emit macho dwarf: elf header corrupted")
55 // Output: emit macho dwarf: elf header corrupted
58 // The fmt package's Errorf function lets us use the package's formatting
59 // features to create descriptive error messages.
60 func ExampleNew_errorf() {
61 const name, id = "bimmler", 17
62 err := fmt.Errorf("user %q (id %d) not found", name, id)
66 // Output: user "bimmler" (id 17) not found