forked from facebookarchive/stackerr
/
stackerr_test.go
82 lines (72 loc) · 1.51 KB
/
stackerr_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package stackerr_test
import (
"errors"
"fmt"
"regexp"
"strings"
"testing"
"github.com/facebookgo/stackerr"
)
func TestNew(t *testing.T) {
const errStr = "foo bar baz"
e := stackerr.New(errStr)
matches := []string{
errStr,
"stackerr_test.go:15 +TestNew$",
}
match(t, e.Error(), matches)
}
func TestNewf(t *testing.T) {
const fmtStr = "%s 42"
const errStr = "foo bar baz"
e := stackerr.Newf(fmtStr, errStr)
matches := []string{
fmt.Sprintf(fmtStr, errStr),
"stackerr_test.go:26 +TestNewf$",
}
match(t, e.Error(), matches)
}
func TestWrap(t *testing.T) {
const errStr = "foo bar baz"
e := stackerr.Wrap(errors.New(errStr))
matches := []string{
errStr,
"stackerr_test.go:36 +TestWrap$",
}
match(t, e.Error(), matches)
}
func TestNilWrap(t *testing.T) {
if stackerr.WrapSkip(nil, 1) != nil {
t.Fatal("did not get nil error")
}
}
func TestDoubleWrap(t *testing.T) {
e := stackerr.New("")
if stackerr.WrapSkip(e, 1) != e {
t.Fatal("double wrap failure")
}
}
func TestLog(t *testing.T) {
t.Log(stackerr.New("hello"))
}
func TestUnderlying(t *testing.T) {
e1 := errors.New("")
e2 := stackerr.Wrap(e1)
errs := stackerr.Underlying(e2)
if len(errs) != 2 || errs[0] != e2 || errs[1] != e1 {
t.Fatal("failed Underlying")
}
}
func match(t testing.TB, s string, matches []string) {
lines := strings.Split(s, "\n")
for i, m := range matches {
if !regexp.MustCompile(m).MatchString(lines[i]) {
t.Fatalf(
"did not find expected match \"%s\" on line %d in:\n%s",
m,
i,
s,
)
}
}
}