-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexit_error_test.go
91 lines (87 loc) · 1.97 KB
/
exit_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
package cmd_toolkit_test
import (
cmdtoolkit "github.com/majohn-r/cmd-toolkit"
"testing"
)
func TestExitError_Error(t *testing.T) {
tests := map[string]struct {
e *cmdtoolkit.ExitError
want string
}{
"user": {
e: cmdtoolkit.NewExitUserError("cmdX"),
want: `command "cmdX" terminated with an error (user error)`,
},
"code": {
e: cmdtoolkit.NewExitProgrammingError("cmdY"),
want: `command "cmdY" terminated with an error (programming error)`,
},
"system": {
e: cmdtoolkit.NewExitSystemError("cmdZ"),
want: `command "cmdZ" terminated with an error (system call failed)`,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := tt.e.Error(); got != tt.want {
t.Errorf("Error() = %v, want %v", got, tt.want)
}
})
}
}
func TestExitError_Status(t *testing.T) {
tests := map[string]struct {
e *cmdtoolkit.ExitError
want int
}{
"user": {
e: cmdtoolkit.NewExitUserError("cmdX"),
want: 1,
},
"code": {
e: cmdtoolkit.NewExitProgrammingError("cmdY"),
want: 2,
},
"system": {
e: cmdtoolkit.NewExitSystemError("cmdZ"),
want: 3,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := tt.e.Status(); got != tt.want {
t.Errorf("Status() = %v, want %v", got, tt.want)
}
})
}
}
func TestToErrorInterface(t *testing.T) {
tests := map[string]struct {
e *cmdtoolkit.ExitError
wantErr bool
}{
"nil": {
e: nil,
wantErr: false,
},
"user": {
e: cmdtoolkit.NewExitUserError("cmdX"),
wantErr: true,
},
"code": {
e: cmdtoolkit.NewExitProgrammingError("cmdY"),
wantErr: true,
},
"system": {
e: cmdtoolkit.NewExitSystemError("cmdZ"),
wantErr: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if err := cmdtoolkit.ToErrorInterface(tt.e); (err != nil) != tt.wantErr {
t.Errorf("ToErrorInterface() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}