-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patherrors_new_test.go
120 lines (95 loc) · 2.69 KB
/
errors_new_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
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
package errors
import (
"testing"
)
func TestNewPlain(t *testing.T) {
err := NewPlain("error")
t.Parallel()
t.Run("error_message", func(t *testing.T) {
checkErrorMessage(t, err, "error")
})
t.Run("format", func(t *testing.T) {
checkFormat(t, err, map[string][]string{
"%s": {"error"},
"%q": {`"error"`},
"%v": {"error"},
"%+v": {"error"},
})
})
}
func TestNew(t *testing.T) {
err := New("error")
origErr := err.(*withStack).error
t.Parallel()
t.Run("error_message", func(t *testing.T) {
checkErrorMessage(t, err, "error")
})
t.Run("unwrap", func(t *testing.T) {
checkUnwrap(t, err, origErr)
})
t.Run("format", func(t *testing.T) {
checkFormat(t, err, map[string][]string{
"%s": {"error"},
"%q": {`"error"`},
"%v": {"error"},
"%+v": {"error", "emperror.dev/errors.TestNew\n\t.+/errors_new_test.go:27"},
})
})
}
func TestNewWithDetails(t *testing.T) {
details := []interface{}{"key", "value"}
err := NewWithDetails("error", details...)
origErr := err.(*withDetails).error
t.Parallel()
t.Run("error_message", func(t *testing.T) {
checkErrorMessage(t, err, "error")
})
t.Run("unwrap", func(t *testing.T) {
checkUnwrap(t, err, origErr)
})
t.Run("format", func(t *testing.T) {
checkFormat(t, err, map[string][]string{
"%s": {"error"},
"%q": {`"error"`},
"%v": {"error"},
"%+v": {"error", "emperror.dev/errors.TestNewWithDetails\n\t.+/errors_new_test.go:52"},
})
})
t.Run("details", func(t *testing.T) {
d := err.(*withDetails).Details()
for i, detail := range d {
if got, want := detail, details[i]; got != want {
t.Errorf("error detail does not match the expected one\nactual: %+v\nexpected: %+v", got, want)
}
}
})
t.Run("details_missing_value", func(t *testing.T) {
details := []interface{}{"key", nil}
err := NewWithDetails("error", "key")
d := err.(*withDetails).Details()
for i, detail := range d {
if got, want := detail, details[i]; got != want {
t.Errorf("error detail does not match the expected one\nactual: %+v\nexpected: %+v", got, want)
}
}
})
}
func TestErrorf(t *testing.T) {
err := Errorf("error: %s", "something went wrong")
origErr := err.(*withStack).error
t.Parallel()
t.Run("error_message", func(t *testing.T) {
checkErrorMessage(t, err, "error: something went wrong")
})
t.Run("unwrap", func(t *testing.T) {
checkUnwrap(t, err, origErr)
})
t.Run("format", func(t *testing.T) {
checkFormat(t, err, map[string][]string{
"%s": {"error: something went wrong"},
"%q": {`"error: something went wrong"`},
"%v": {"error: something went wrong"},
"%+v": {"error: something went wrong", "emperror.dev/errors.TestErrorf\n\t.+/errors_new_test.go:99"},
})
})
}