-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclitools_test.go
194 lines (189 loc) · 4.81 KB
/
clitools_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package gottings
import (
"fmt"
"reflect"
"testing"
)
func TestLoadOptions(t *testing.T) {
type Config struct {
Int int
NullInt NullInt
Int8 int8
NullInt8 NullInt8
Int16 int16
NullInt16 NullInt16
Int32 int32
NullInt32 NullInt32
Int64 int64
NullInt64 NullInt64
Float32 float32
NullFloat32 NullFloat32
Float64 float64
NullFloat64 NullFloat64
Bool bool
NullBool NullBool
String string
NullString NullString
}
type Config2 struct {
Int *int
NullInt *NullInt
Int8 *int8
NullInt8 *NullInt8
Int16 *int16
NullInt16 *NullInt16
Int32 *int32
NullInt32 *NullInt32
Int64 *int64
NullInt64 *NullInt64
Float32 *float32
NullFloat32 *NullFloat32
Float64 *float64
NullFloat64 *NullFloat64
Bool *bool
NullBool *NullBool
String *string
NullString *NullString
}
expected := Config{
Int: 1,
NullInt: NullInt{Int: 1, Valid: true},
Int8: 8,
NullInt8: NullInt8{Int8: 8, Valid: true},
Int16: 16,
NullInt16: NullInt16{Int16: 16, Valid: true},
Int32: 32,
NullInt32: NullInt32{Int32: 32, Valid: true},
Int64: 64,
NullInt64: NullInt64{Int64: 64, Valid: true},
Float32: 3.2,
NullFloat32: NullFloat32{Float32: 3.2, Valid: true},
Float64: 6.4,
NullFloat64: NullFloat64{Float64: 6.4, Valid: true},
Bool: true,
NullBool: NullBool{Bool: true, Valid: true},
String: "string",
NullString: NullString{String: "string", Valid: true},
}
var i int = 1
var i8 int8 = 8
var i16 int16 = 16
var i32 int32 = 32
var i64 int64 = 64
var Bool bool = true
var f64 float64 = 6.4
var f32 float32 = 3.2
var String string = "string"
expected2 := Config2{
Int: &i,
NullInt: &NullInt{Int: 1, Valid: true},
Int8: &i8,
NullInt8: &NullInt8{Int8: 8, Valid: true},
Int16: &i16,
NullInt16: &NullInt16{Int16: 16, Valid: true},
Int32: &i32,
NullInt32: &NullInt32{Int32: 32, Valid: true},
Int64: &i64,
NullInt64: &NullInt64{Int64: 64, Valid: true},
Float32: &f32,
NullFloat32: &NullFloat32{Float32: 3.2, Valid: true},
Float64: &f64,
NullFloat64: &NullFloat64{Float64: 6.4, Valid: true},
Bool: &Bool,
NullBool: &NullBool{Bool: true, Valid: true},
String: &String,
NullString: &NullString{String: "string", Valid: true},
}
config := Config{}
t.Run("explicit type in map", func(t *testing.T) {
err := LoadOptions(map[string]interface{}{
"Int": 1,
"NullInt": 1,
"Int8": int8(8),
"NullInt8": int8(8),
"Int16": int16(16),
"NullInt16": int16(16),
"Int32": int32(32),
"NullInt32": int32(32),
"Int64": int64(64),
"NullInt64": int64(64),
"Float32": float32(3.2),
"NullFloat32": float32(3.2),
"Float64": float64(6.4),
"NullFloat64": float64(6.4),
"Bool": true,
"NullBool": true,
"String": "string",
"NullString": "string",
}, &config)
if err != nil {
t.Fatalf("error not expected received: %s", err)
}
if reflect.DeepEqual(config, expected) {
t.Fatalf("result %v and expected %v not equal", config, expected)
}
},
)
t.Run("non explicit type in map", func(t *testing.T) {
err := LoadOptions(map[string]interface{}{
"Int": 1,
"NullInt": 1,
"Int8": 8,
"NullInt8": 8,
"Int16": 16,
"NullInt16": 16,
"Int32": 32,
"NullInt32": 32,
"Int64": 64,
"NullInt64": 64,
"Float32": 3.2,
"NullFloat32": 3.2,
"Float64": 6.4,
"NullFloat64": 6.4,
"Bool": true,
"NullBool": true,
"String": "string",
"NullString": "string",
}, &config)
if err != nil {
t.Fatalf("error not expected received: %s", err)
}
if reflect.DeepEqual(config, expected) {
t.Fatalf("result %v and expected %v not equal", config, expected)
}
})
config2 := Config2{}
t.Run("explicit type in map - pointers", func(t *testing.T) {
err := LoadOptions(map[string]interface{}{
"Int": &i,
"NullInt": &i,
"Int8": &i8,
"NullInt8": &i8,
"Int16": &i16,
"NullInt16": &i16,
"Int32": &i32,
"NullInt32": &i32,
"Int64": &i64,
"NullInt64": &i64,
"Float32": &f32,
"NullFloat32": &f32,
"Float64": &f64,
"NullFloat64": &f64,
"Bool": &Bool,
"NullBool": &Bool,
"String": &String,
"NullString": &String,
}, &config)
if err != nil {
t.Fatalf("error not expected received: %s", err)
}
if reflect.DeepEqual(config2, expected2) {
t.Fatalf("result %v and expected %v not equal", config2, expected2)
}
})
}
func TestType(t *testing.T) {
var a int = 2
fmt.Printf("%T\n", a)
fmt.Printf("%T\n", &a)
}