This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror_test.go
142 lines (137 loc) · 3.33 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
141
142
package uuid
import (
"testing"
)
func TestParseError(t *testing.T) {
type testrow struct {
testName string
typeName string
methodName string
input []byte
isText bool
detailFmt string
detailArgs []interface{}
expected string
}
data := []testrow{
{
testName: "empty",
expected: `uuid: failed to parse`,
},
{
testName: "top-level function",
methodName: "Func",
expected: `uuid.Func: failed to parse`,
},
{
testName: "method",
typeName: "Class",
methodName: "Func",
expected: `uuid.Class.Func: failed to parse`,
},
{
testName: "basic text",
typeName: "Class",
methodName: "Func",
input: []byte(`some text`),
isText: true,
expected: `uuid.Class.Func: failed to parse "some text"`,
},
{
testName: "basic bytes",
typeName: "Class",
methodName: "Func",
input: []byte{0xde, 0xad, 0xca, 0xfe},
isText: false,
expected: `uuid.Class.Func: failed to parse [de ad ca fe]`,
},
{
testName: "detail",
typeName: "Class",
methodName: "Func",
input: []byte{0xde, 0xad, 0xca, 0xfe},
isText: false,
detailFmt: "%q %#02x",
detailArgs: []interface{}{"string", 64},
expected: `uuid.Class.Func: failed to parse [de ad ca fe]: "string" 0x40`,
},
{
testName: "detail no input",
typeName: "Class",
methodName: "Func",
detailFmt: "%q %#02x",
detailArgs: []interface{}{"string", 64},
expected: `uuid.Class.Func: failed to parse: "string" 0x40`,
},
}
for _, row := range data {
t.Run(row.testName, func(t *testing.T) {
err := makeParseError(row.typeName, row.methodName, row.input, row.isText)
if row.detailFmt != "" {
err = err.detailf(row.detailFmt, row.detailArgs...)
}
actual := err.Error()
if row.expected != actual {
t.Errorf("wrong error message: expected %q, got %q", row.expected, actual)
}
})
}
}
func TestTypeError(t *testing.T) {
type testrow struct {
testName string
typeName string
methodName string
input interface{}
dummies []interface{}
expected string
}
data := []testrow{
{
testName: "empty",
expected: `uuid: wrong type`,
},
{
testName: "top-level function",
methodName: "Func",
expected: `uuid.Func: wrong type`,
},
{
testName: "method",
typeName: "Class",
methodName: "Func",
expected: `uuid.Class.Func: wrong type`,
},
{
testName: "basic type",
typeName: "Class",
methodName: "Func",
input: uint64(0),
expected: `uuid.Class.Func: wrong type uint64`,
},
{
testName: "custom type",
typeName: "Class",
methodName: "Func",
input: &TypeError{},
expected: `uuid.Class.Func: wrong type *uuid.TypeError`,
},
{
testName: "dummy list",
typeName: "Class",
methodName: "Func",
input: uint64(0),
dummies: []interface{}{new(bool), new(string), []byte(nil), &TypeError{}},
expected: `uuid.Class.Func: wrong type uint64: expected one of *bool *string []uint8 *uuid.TypeError`,
},
}
for _, row := range data {
t.Run(row.testName, func(t *testing.T) {
err := makeTypeError(row.typeName, row.methodName, row.input, row.dummies...)
actual := err.Error()
if row.expected != actual {
t.Errorf("wrong error message: expected %q, got %q", row.expected, actual)
}
})
}
}