-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_stack_trace_test.go
55 lines (41 loc) · 1.17 KB
/
error_stack_trace_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
package xerror
import (
"fmt"
"strings"
"testing"
"github.com/matryer/is"
)
func (x *x) SetStackPrinter(p func(error) string) I {
x.stackPrinter = p
return x
}
func TestNewErrorWithStackTraceWithReplacedPrinter(t *testing.T) {
is := is.New(t)
err := New("fake db update failed").WithStackTrace()
is.True(err != nil)
xerr, ok := err.(*x)
is.True(ok)
xerr.SetStackPrinter(func(e error) string {
return fmt.Sprintf("%v\nstack-goes-here", e)
})
is.Equal(xerr.Error(), "fake db update failed\nstack-goes-here")
}
func TestNewErrorWithoutStackTraceWithReplacedPrinter(t *testing.T) {
is := is.New(t)
err := New("fake db update failed")
is.True(err != nil)
xerr, ok := err.(*x)
is.True(ok)
xerr.SetStackPrinter(func(e error) string {
return fmt.Sprintf("%v\nstack-goes-here", e)
})
is.Equal(xerr.Error(), "fake db update failed")
}
// basic test to check stack trace output, if necessary might
// improve using regex pattern matching
func TestNewErrorWithStackTraceWithoutReplacedPrinter(t *testing.T) {
is := is.New(t)
err := New("fake db update failed").WithStackTrace()
is.True(err != nil)
is.True(strings.Contains(err.Error(), "/xerror.(*x).format"))
}