-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrors_test.go
77 lines (70 loc) · 1.61 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
package scpi
import (
"errors"
"testing"
)
func TestInvalidProtocolError(t *testing.T) {
err := InvalidProtocolError("foo")
if got, want := err.Error(), "invalid protocol foo"; got != want {
t.Fatalf("got %s, want %s", got, want)
}
}
func TestInvalidFormatError(t *testing.T) {
err := InvalidFormatError("foo")
if got, want := err.Error(), "invalid format: foo"; got != want {
t.Fatalf("got %s, want %s", got, want)
}
}
func TestCommandError(t *testing.T) {
err := &CommandError{
cmd: "foo",
code: -101,
msg: "invalid character",
}
if got, want := err.Code(), -101; got != want {
t.Fatalf("got %d, want %d", got, want)
}
if got, want := err.Error(), "'foo' returned -101: invalid character"; got != want {
t.Fatalf("got %s, want %s", got, want)
}
}
func TestConfirmError(t *testing.T) {
tests := map[string]struct {
in map[string]string
want error
}{
"NoError": {
in: map[string]string{
"cmd": "*CLS",
"errRes": "+0,\"No error\"",
},
want: nil,
},
"InvalidFormat": {
in: map[string]string{
"cmd": "foo",
"errRes": "foo, bar, baz",
},
want: InvalidFormatError("foo, bar, baz"),
},
"CommandError": {
in: map[string]string{
"cmd": "foo",
"errRes": "-101,\"Invalid character\"",
},
want: &CommandError{
cmd: "foo",
code: -101,
msg: "invalid character",
},
},
}
for n, tt := range tests {
t.Run(n, func(t *testing.T) {
err := confirmError(tt.in["cmd"], tt.in["errRes"])
if got, want := err, tt.want; !(errors.Is(got, want) || errors.As(got, &want)) {
t.Fatalf("got %+v, want %+v", got, want)
}
})
}
}