-
Notifications
You must be signed in to change notification settings - Fork 11
/
errors_test.go
99 lines (85 loc) · 1.81 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
package gounit
import (
"reflect"
"testing"
)
func TestError_Format(t *testing.T) {
type args struct {
args []interface{}
}
tests := []struct {
name string
init func(t *testing.T) GenericError
inspect func(r GenericError, t *testing.T) //inspects receiver after test run
args func(t *testing.T) args
wantErr bool
inspectErr func(err error, t *testing.T) //use for more precise error evaluation after test
}{
{
name: "success",
init: func(*testing.T) GenericError {
return GenericError("%d - %s = 0")
},
args: func(*testing.T) args {
return args{
args: []interface{}{1, "1"},
}
},
inspectErr: func(err error, t *testing.T) {
if err.Error() != "1 - 1 = 0" {
t.Errorf("unexpected result: %v", err)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tArgs := tt.args(t)
receiver := tt.init(t)
err := receiver.Format(tArgs.args...)
if tt.inspect != nil {
tt.inspect(receiver, t)
}
if tt.inspectErr != nil {
tt.inspectErr(err, t)
}
})
}
}
func TestNewError(t *testing.T) {
type args struct {
code int
format string
}
tests := []struct {
name string
args func(t *testing.T) args
want1 GenericError
}{
{
name: "success",
args: func(*testing.T) args {
return args{
code: 1,
format: "format",
}
},
want1: GenericError("format"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tArgs := tt.args(t)
got1 := GenericError(tArgs.format)
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("NewError got1 = %v, want1: %v", got1, tt.want1)
}
})
}
}
func TestCommandLineError_Error(t *testing.T) {
s := CommandLineError("command line").Error()
if s != "command line" {
t.Fatalf("want: \"command line\", got: %q", s)
}
}