-
Notifications
You must be signed in to change notification settings - Fork 31
/
wrap_test.go
68 lines (58 loc) · 2.18 KB
/
wrap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package errorx
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestEnsureStackTrace(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
err := EnsureStackTrace(testType.New("good"))
require.True(t, IsOfType(err, testType))
output := fmt.Sprintf("%+v", err)
require.Contains(t, output, "good", output)
require.Contains(t, output, "TestEnsureStackTrace", output)
})
t.Run("NoTrace", func(t *testing.T) {
err := EnsureStackTrace(testTypeSilent.New("average"))
require.True(t, IsOfType(err, testType))
output := fmt.Sprintf("%+v", err)
require.Contains(t, output, "average", output)
require.Contains(t, output, "TestEnsureStackTrace", output)
})
t.Run("Raw", func(t *testing.T) {
err := EnsureStackTrace(errors.New("bad"))
output := fmt.Sprintf("%+v", err)
require.Contains(t, output, "bad", output)
require.Contains(t, output, "TestEnsureStackTrace", output)
})
}
func TestDecorateMany(t *testing.T) {
t.Run("Single", func(t *testing.T) {
err := DecorateMany("ouch!", testType.NewWithNoMessage())
require.Equal(t, "ouch!, cause: foo.bar", err.Error())
require.True(t, IsOfType(err, testType))
require.Equal(t, testType, err.(*Error).Type())
})
t.Run("SingleEmpty", func(t *testing.T) {
require.Nil(t, DecorateMany("ouch!", nil))
})
t.Run("ManyEmpty", func(t *testing.T) {
require.Nil(t, DecorateMany("ouch!", nil, nil))
require.Nil(t, DecorateMany("ouch!", nil, nil, nil))
})
t.Run("ManySame", func(t *testing.T) {
err := DecorateMany("ouch!", testType.NewWithNoMessage(), nil, testType.New("bad"))
require.Equal(t, "ouch!, cause: foo.bar (hidden: foo.bar: bad)", err.Error())
require.True(t, IsOfType(err, testType))
require.Equal(t, testType, err.(*Error).Type())
})
t.Run("ManyDifferent", func(t *testing.T) {
err := DecorateMany("ouch!", testTypeBar1.NewWithNoMessage(), testTypeBar2.New("bad"), nil)
require.Equal(t, "synthetic.wrap: ouch!, cause: foo.bar1 (hidden: foo.bar2: bad)", err.Error())
require.False(t, IsOfType(err, testTypeBar1))
require.False(t, IsOfType(err, testTypeBar2))
require.NotEqual(t, testTypeBar1, err.(*Error).Type())
require.NotEqual(t, testTypeBar2, err.(*Error).Type())
})
}