-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror_test.go
140 lines (131 loc) · 2.84 KB
/
error_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package errors
import (
"testing"
)
func TestKind_String(t *testing.T) {
kind := Permission
const expected = "2"
if kind.String() != expected {
t.Fatalf("expected %s, got %s", expected, kind.String())
}
}
func TestDoesNotChangePreviousError(t *testing.T) {
err := New("new error", Permission)
err2 := New("wrapped context", Op("op"), err)
expected := "op" + separator + "wrapped context" + separator + "new error"
if err2.Error() != expected {
t.Fatalf("Expected %q, got %q", expected, err2)
}
kind := err.(kinder).Kind()
if kind != Permission {
t.Fatalf("Expected kind %v, got %v", Permission, kind)
}
l3op := Op("someFunc")
err3 := New("level3 err", l3op, Internal, err2)
e := err3.(*withOp)
if e.op != l3op {
t.Fatalf("Expected op %v, got %v", l3op, e.op)
}
}
func TestError_Error(t *testing.T) {
err1 := New("new error", Permission)
err2 := New("wrapped context", Op("I will NOT modify err"), err1)
l3op := Op("someFunc")
err3 := New("level3 err", l3op, Internal, err2)
type fields struct {
op Op
Error *Error
}
tests := []struct {
name string
err error
want string
}{
{
"level1 Error()",
err1,
"new error",
},
{
"level2 Error()",
err2,
"I will NOT modify err: wrapped context: new error",
},
{
"level3 Error()",
err3,
"someFunc: level3 err: I will NOT modify err: wrapped context: new error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.err.Error(); got != tt.want {
t.Errorf("Error.Error() = %v, want %v", got, tt.want)
}
})
}
}
func TestStr(t *testing.T) {
type args struct {
text string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"correct call",
args{text: "test"},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Str(tt.args.text); (err != nil) != tt.wantErr {
t.Errorf("Str() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_stringerr_Error(t *testing.T) {
type fields struct {
msg string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "new stringerr error",
fields: fields{"test new error"},
want: "test new error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &stringerr{
msg: tt.fields.msg,
}
if got := e.Error(); got != tt.want {
t.Errorf("stringerr.Error() = %v, want %v", got, tt.want)
}
})
}
}
func TestErrorf(t *testing.T) {
tests := []struct {
err error
want string
}{
{Errorf("read error without format specifiers"), "read error without format specifiers"},
{Errorf("read error with %d format specifier", 1), "read error with 1 format specifier"},
}
for _, tt := range tests {
got := tt.err.Error()
if got != tt.want {
t.Errorf("Errorf(%v): got: %q, want %q", tt.err, got, tt.want)
}
}
}