-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
126 lines (112 loc) · 2.58 KB
/
Copy patherrors_test.go
File metadata and controls
126 lines (112 loc) · 2.58 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package faultline
import (
"fmt"
"testing"
)
func TestNew(t *testing.T) {
tests := []struct {
msg string
want string
}{
{"", ""},
{"hello", "hello"},
{"foo bar", "foo bar"},
}
for _, tt := range tests {
got := New(tt.msg)
if got.Error() != tt.want {
t.Errorf("New(%q): got %q, want %q", tt.msg, got.Error(), tt.want)
}
}
}
func TestNewHasStack(t *testing.T) {
err := New("test")
type stackTracer interface {
StackTrace() StackTrace
}
st, ok := err.(stackTracer)
if !ok {
t.Fatal("New error does not implement stackTracer")
}
trace := st.StackTrace()
if len(trace) == 0 {
t.Fatal("StackTrace() returned empty slice")
}
}
func TestErrorf(t *testing.T) {
tests := []struct {
format string
args []interface{}
want string
}{
{"hello %s", []interface{}{"world"}, "hello world"},
{"count: %d", []interface{}{42}, "count: 42"},
{"no args", nil, "no args"},
}
for _, tt := range tests {
got := Errorf(tt.format, tt.args...)
if got.Error() != tt.want {
t.Errorf("Errorf(%q, %v): got %q, want %q", tt.format, tt.args, got.Error(), tt.want)
}
}
}
func TestErrorfHasStack(t *testing.T) {
err := Errorf("test %d", 1)
type stackTracer interface {
StackTrace() StackTrace
}
st, ok := err.(stackTracer)
if !ok {
t.Fatal("Errorf error does not implement stackTracer")
}
trace := st.StackTrace()
if len(trace) == 0 {
t.Fatal("StackTrace() returned empty slice")
}
}
func TestFundamentalCauseIsNil(t *testing.T) {
err := New("test")
type causer interface {
Cause() error
}
c, ok := err.(causer)
if !ok {
t.Fatal("fundamental does not implement causer")
}
if c.Cause() != nil {
t.Error("fundamental.Cause() should be nil")
}
}
func TestFundamentalUnwrapIsNil(t *testing.T) {
err := New("test")
type unwrapper interface {
Unwrap() error
}
u, ok := err.(unwrapper)
if !ok {
t.Fatal("fundamental does not implement unwrapper")
}
if u.Unwrap() != nil {
t.Error("fundamental.Unwrap() should be nil")
}
}
func TestNewFormat(t *testing.T) {
err := New("test message")
// %s
if got := fmt.Sprintf("%s", err); got != "test message" {
t.Errorf("%%s: got %q, want %q", got, "test message")
}
// %v
if got := fmt.Sprintf("%v", err); got != "test message" {
t.Errorf("%%v: got %q, want %q", got, "test message")
}
// %q
if got := fmt.Sprintf("%q", err); got != `"test message"` {
t.Errorf("%%q: got %q, want %q", got, `"test message"`)
}
// %+v should contain the message and stack trace
plusV := fmt.Sprintf("%+v", err)
if len(plusV) <= len("test message") {
t.Errorf("%%+v output should include stack trace, got %q", plusV)
}
}