tlog.app/go/errors@v0.9.0/fmt_test.go (about)

     1  package errors
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestErrorFormatCaller(t *testing.T) {
    12  	err := New("inner")
    13  
    14  	assert.Equal(t, "inner", fmt.Sprintf("%v", err)) //nolint:perfsprint
    15  	assert.Equal(t, "inner (fmt_test.go:12)", fmt.Sprintf("%+v", err))
    16  	assert.Regexp(t, "inner at (((github.com/)?tlog-dev/)?errors/)?fmt_test.go:12", fmt.Sprintf("% +v", err))
    17  
    18  	// more
    19  	err = Wrap(
    20  		Wrap(os.ErrNotExist, "middle"),
    21  		"global")
    22  
    23  	assert.Equal(t, "global: middle: file does not exist", fmt.Sprintf("%v", err)) //nolint:perfsprint
    24  	assert.Equal(t, "global (fmt_test.go:19): middle (fmt_test.go:20): file does not exist", fmt.Sprintf("%+v", err))
    25  	assert.Regexp(t, `global at (((github.com/)?tlog-dev/)?errors/)?fmt_test.go:19
    26  middle at (((github.com/)?tlog-dev/)?errors/)?fmt_test.go:20
    27  file does not exist`, fmt.Sprintf("% +v", err))
    28  
    29  	// one more
    30  	err = Wrap(
    31  		Wrap(
    32  			New("inner"),
    33  			"middle"),
    34  		"global")
    35  
    36  	assert.Equal(t, "global: middle: inner", fmt.Sprintf("%v", err)) //nolint:perfsprint
    37  	assert.Equal(t, "global (fmt_test.go:30): middle (fmt_test.go:31): inner (fmt_test.go:32)", fmt.Sprintf("%+v", err))
    38  	assert.Regexp(t, `global at (((github.com/)?tlog-dev/)?errors/)?fmt_test.go:30
    39  middle at (((github.com/)?tlog-dev/)?errors/)?fmt_test.go:31
    40  inner at (((github.com/)?tlog-dev/)?errors/)?fmt_test.go:32`, fmt.Sprintf("% +v", err))
    41  
    42  	// with no messages
    43  	err = Wrap(
    44  		Wrap(
    45  			New(""),
    46  			""),
    47  		"")
    48  
    49  	assert.Equal(t, nomessage, fmt.Sprintf("%v", err)) //nolint:perfsprint
    50  	assert.Equal(t, fmt.Sprintf("%v (fmt_test.go:43): %[1]v (fmt_test.go:44): %[1]v (fmt_test.go:45)", nomessage), fmt.Sprintf("%+v", err))
    51  	assert.Regexp(t, `\(no message\) at (((github.com/)?tlog-dev/)?errors/)?fmt_test.go:43
    52  \(no message\) at (((github.com/)?tlog-dev/)?errors/)?fmt_test.go:44
    53  \(no message\) at (((github.com/)?tlog-dev/)?errors/)?fmt_test.go:45`, fmt.Sprintf("% +v", err))
    54  }
    55  
    56  func TestErrorFormat(t *testing.T) {
    57  	err := NewNoCaller("inner")
    58  
    59  	assert.Equal(t, "inner", fmt.Sprintf("%v", err)) //nolint:perfsprint
    60  	assert.Equal(t, "inner", fmt.Sprintf("%+v", err))
    61  	assert.Equal(t, "inner", fmt.Sprintf("% +v", err))
    62  
    63  	// more
    64  	err = WrapNoCaller(WrapNoCaller(os.ErrNotExist, "middle"), "global")
    65  
    66  	assert.Equal(t, "global: middle: file does not exist", fmt.Sprintf("%v", err)) //nolint:perfsprint
    67  	assert.Equal(t, "global: middle: file does not exist", fmt.Sprintf("%+v", err))
    68  	assert.Equal(t, "global\nmiddle\nfile does not exist", fmt.Sprintf("% +v", err))
    69  
    70  	// one more
    71  	err = WrapNoCaller(WrapNoCaller(NewNoCaller("inner"), "middle"), "global")
    72  
    73  	assert.Equal(t, "global: middle: inner", fmt.Sprintf("%v", err)) //nolint:perfsprint
    74  	assert.Equal(t, "global: middle: inner", fmt.Sprintf("%+v", err))
    75  	assert.Equal(t, "global\nmiddle\ninner", fmt.Sprintf("% +v", err))
    76  }