-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patherrors_test.go
144 lines (130 loc) · 2.82 KB
/
errors_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
143
144
// Copyright (C) 2017 Kale Blankenship. All rights reserved.
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details
package tftp // import "pack.ag/tftp"
import "testing"
func TestIsUnexpectedDatagram(t *testing.T) {
cases := []struct {
name string
err error
expected bool
}{
{
name: "true",
err: &errUnexpectedDatagram{},
expected: true,
},
{
name: "true, wrapped",
err: wrapError(&errUnexpectedDatagram{}, "testing"),
expected: true,
},
{
name: "false",
err: errBlockSequence,
expected: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := IsUnexpectedDatagram(c.err)
if result != c.expected {
t.Errorf("expected to IsUnexpectedDatagram %t, but it wasn't", c.expected)
}
})
}
}
func TestIsRemoteError(t *testing.T) {
cases := []struct {
name string
err error
expected bool
}{
{
name: "true",
err: &errRemoteError{},
expected: true,
},
{
name: "true, wrapped",
err: wrapError(&errRemoteError{}, "testing"),
expected: true,
},
{
name: "false",
err: errBlockSequence,
expected: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := IsRemoteError(c.err)
if result != c.expected {
t.Errorf("expected to IsUnexpectedDatagram %t, but it wasn't", c.expected)
}
})
}
}
func TestIsOptionParsingError(t *testing.T) {
cases := []struct {
name string
err error
expected bool
}{
{
name: "true",
err: &errParsingOption{},
expected: true,
},
{
name: "true, wrapped",
err: wrapError(&errParsingOption{}, "testing"),
expected: true,
},
{
name: "false",
err: errBlockSequence,
expected: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := IsOptionParsingError(c.err)
if result != c.expected {
t.Errorf("expected to IsUnexpectedDatagram %t, but it wasn't", c.expected)
}
})
}
}
func TestErrorStrings(t *testing.T) {
dg := datagram{}
dg.writeAck(68)
cases := []struct {
name string
err error
expected string
}{
{
name: "unexpected datagram",
err: &errUnexpectedDatagram{dg: dg.String()},
expected: `unexpected datagram: ACK[Block: 68]`,
},
{
name: "remote error",
err: &errRemoteError{dg: dg.String()},
expected: `remote error: ACK[Block: 68]`,
},
{
name: "parse error",
err: &errParsingOption{option: "timeout", value: "a"},
expected: `error parsing "a" for option "timeout"`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if c.err.Error() != c.expected {
t.Errorf("Expected %q to be %q", c.err.Error(), c.expected)
}
})
}
}