-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptionsDef_test.go
173 lines (170 loc) · 3.95 KB
/
optionsDef_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package call
import (
"reflect"
"testing"
)
func TestOptionGetIndex(t *testing.T) {
type args struct {
v []reflect.Value
args []string
}
tests := []struct {
name string
args args
want []reflect.Value
wantErr bool
wantErrStr string
}{
{
name: "no value",
args: args{
v: []reflect.Value{},
args: []string{"1"},
},
want: nil,
wantErr: true,
wantErrStr: "no value",
},
{
name: "index is empty",
args: args{
v: []reflect.Value{reflect.ValueOf("test")},
args: nil,
},
want: nil,
wantErr: true,
wantErrStr: "index is empty",
},
{
name: "not related type for index",
args: args{
v: []reflect.Value{reflect.ValueOf("test")},
args: []string{"1"},
},
want: nil,
wantErr: true,
wantErrStr: "not related type for index",
},
{
name: "index is not a number",
args: args{
v: []reflect.Value{reflect.ValueOf([]string{"test"})},
args: []string{"test"},
},
want: nil,
wantErr: true,
wantErrStr: `index is not a number; strconv.Atoi: parsing "test": invalid syntax`,
},
{
name: "index out of range",
args: args{
v: []reflect.Value{reflect.ValueOf([]string{"test"})},
args: []string{"20"},
},
want: nil,
wantErr: true,
wantErrStr: `index out of range`,
},
{
name: "correct value",
args: args{
v: []reflect.Value{reflect.ValueOf([]string{"test"})},
args: []string{"0"},
},
want: []reflect.Value{reflect.ValueOf("test")},
},
{
name: "correct value",
args: args{
v: []reflect.Value{reflect.ValueOf([]any{"test", 123, 123.123})},
args: []string{"0", "2"},
},
want: []reflect.Value{reflect.ValueOf("test"), reflect.ValueOf(123.123)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := OptionGetIndex(tt.args.v, tt.args.args...)
if (err != nil) != tt.wantErr {
t.Errorf("OptionGetIndex() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil && err.Error() != tt.wantErrStr {
t.Errorf("OptionGetIndex() error = %v, wantErrStr %v", err, tt.wantErrStr)
return
}
if tt.wantErr == false {
for i := range got {
if !reflect.DeepEqual(got[i].Interface(), tt.want[i].Interface()) {
t.Errorf("OptionGetIndex() = %v, want %v", got, tt.want)
}
}
}
})
}
}
func TestOptionVariadic(t *testing.T) {
type args struct {
v []reflect.Value
}
tests := []struct {
name string
args args
want []reflect.Value
wantErr bool
wantErrStr string
}{
{
name: "not related type for variadic",
args: args{
v: nil,
},
want: nil,
wantErr: true,
wantErrStr: "no value",
},
{
name: "not related type for variadic",
args: args{
v: []reflect.Value{reflect.ValueOf("test")},
},
want: nil,
wantErr: true,
wantErrStr: "not related type for variadic",
},
{
name: "return directly",
args: args{
v: []reflect.Value{reflect.ValueOf("test"), reflect.ValueOf("test2")},
},
want: []reflect.Value{reflect.ValueOf("test"), reflect.ValueOf("test2")},
},
{
name: "works well",
args: args{
v: []reflect.Value{reflect.ValueOf([]any{"test", 123, 123.123})},
},
want: []reflect.Value{reflect.ValueOf("test"), reflect.ValueOf(123), reflect.ValueOf(123.123)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := OptionVariadic(tt.args.v)
if (err != nil) != tt.wantErr {
t.Errorf("OptionVariadic() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil && err.Error() != tt.wantErrStr {
t.Errorf("OptionVariadic() error = %v, wantErrStr %v", err, tt.wantErrStr)
return
}
if tt.wantErr == false {
for i := range got {
if !reflect.DeepEqual(got[i].Interface(), tt.want[i].Interface()) {
t.Errorf("OptionVariadic() = %v, want %v", got, tt.want)
}
}
}
})
}
}