forked from mirusck/datafiller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datafiller_test.go
296 lines (250 loc) · 6.15 KB
/
datafiller_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package datafiller
import (
"encoding/json"
"fmt"
"math/rand"
"reflect"
"testing"
"time"
)
// Actual tests
func TestUnassignedInt(t *testing.T) {
var i int
Fill(&i)
if reflect.ValueOf(i).Kind() != reflect.Int {
t.Errorf("Type error: %v, want %v", reflect.ValueOf(i).Kind(), reflect.Int)
}
fmt.Println(i)
}
func TestSimpleTypes(t *testing.T) {
// var BoolV bool
var IntV int
var Int8V int8
var Int16V int16
var Int32V int32
var Int64V int64
var UintV uint
var Uint8V uint8
var Uint16V uint16
var Uint32V uint32
var Uint64V uint64
var Float32V float32
var Float64V float64
var Complex64V complex64
var Complex128V complex128
tests := []struct {
value interface{}
expectedType reflect.Kind
}{
// TODO(tvi): Fix.
// {&BoolV, reflect.Bool},
{&IntV, reflect.Int},
{&Int8V, reflect.Int8},
{&Int16V, reflect.Int16},
{&Int32V, reflect.Int32},
{&Int64V, reflect.Int64},
{&UintV, reflect.Uint},
{&Uint8V, reflect.Uint8},
{&Uint16V, reflect.Uint16},
{&Uint32V, reflect.Uint32},
{&Uint64V, reflect.Uint64},
{&Float32V, reflect.Float32},
{&Float64V, reflect.Float64},
{&Complex64V, reflect.Complex64},
{&Complex128V, reflect.Complex128},
}
for _, test := range tests {
// Type level checking
testValue := reflect.Indirect(reflect.ValueOf(test.value))
if testValue.Kind() != test.expectedType {
t.Errorf("Type error: %v, want %v", testValue.Kind(), test.expectedType)
}
Fill(test.value)
testValue = reflect.Indirect(reflect.ValueOf(test.value))
if testValue.Kind() != test.expectedType {
t.Errorf("Type error: %v, want %v", testValue.Kind(), test.expectedType)
}
// Value level checking
ifc := testValue.Interface()
fmt.Printf("Type: %v; \t\t Value: %v \n", testValue.Kind(), ifc)
// TODO(tvi): Figure out mock testing.
zero := reflect.Zero(testValue.Type())
if reflect.DeepEqual(ifc, zero.Interface()) {
t.Errorf("Changed value is zero-value (type: %v): value %v, do not want %v", testValue.Type(), zero.Interface(), ifc)
}
}
}
// var ArrayV array
// var ChanV chan
// var FuncV func
// var InterfaceV interface
// var MapV map
// var PtrV ptr
// var SliceV slice
// var StringV string
// var StructV struct
func TestComplexTypes(t *testing.T) {
}
// var InvalidV invalid
// var UintptrV uintptr
// var UnsafePointerV unsafepointer
func TestPointerTypes(t *testing.T) {
}
type PFF struct {
PFQ string
}
type PFE struct {
PFF *PFF
}
func TestPointerTypesFilling(t *testing.T) {
i := PFE{}
Fill(&i)
if i.PFF == nil || i.PFF.PFQ == "" {
t.Errorf("Fill error: Pointer not filled: %v", i.PFF)
}
}
func TestSimpleValues(t *testing.T) {
// var BoolV bool
var IntV int
var Int8V int8
var Int16V int16
var Int32V int32
var Int64V int64
var UintV uint
var Uint8V uint8
var Uint16V uint16
var Uint32V uint32
var Uint64V uint64
var Float32V float32
var Float64V float64
// var Complex64V complex64
// var Complex128V complex128
tests := []struct {
value interface{}
expectedValue interface{}
}{
{&IntV, int(56)},
{&Int8V, int8(56)},
{&Int16V, int16(56)},
{&Int32V, int32(56)},
{&Int64V, int64(56)},
{&UintV, uint(56)},
{&Uint8V, uint8(56)},
{&Uint16V, uint16(56)},
{&Uint32V, uint32(56)},
{&Uint64V, uint64(56)},
{&Float32V, float32(0.91889215)},
{&Float64V, float64(0.9188921451568604)},
// {&Complex64V, complex(0.91889215, 0.23150717)},
// {&Complex128V, complex(0.9188921451568604, 0.23150716722011566)},
}
for _, test := range tests {
f := NewFiller(Seed(7))
f.Fill(test.value)
testValue := reflect.Indirect(reflect.ValueOf(test.value))
ifc := testValue.Interface()
fmt.Printf("Type: %v; \t\t Value: %v \n", testValue.Kind(), ifc)
if !reflect.DeepEqual(ifc, test.expectedValue) {
t.Errorf("Value mismatch (type: %v): value %v, want %v", testValue.Type(), ifc, test.expectedValue)
}
}
}
func clear(r *rand.Rand, s rand.Source) string {
newRnd := rand.New(s)
// rnd := reflect.Indirect(reflect.ValueOf(r))
// rnd.Set(reflect.Indirect(reflect.ValueOf(newRnd)))
// rnd := reflect.Indirect(reflect.ValueOf(r))
// rnd.Set(reflect.Indirect(reflect.ValueOf(newRnd)))
reflect.ValueOf(r).Set(reflect.ValueOf(newRnd))
// r = rand.New(s)
return ""
}
func TestSimpleValuesGenerate(t *testing.T) {
var Complex64V complex64
var Complex128V complex128
// s := rand.NewSource(7)
// r := rand.New(s)
// TODO(tvi): Hack the rand num generator.
r1 := rand.New(rand.NewSource(7))
r2 := rand.New(rand.NewSource(7))
tests := []struct {
value interface{}
expectedValue interface{}
// dummy string
}{
// {&Complex64V, complex(r.Float32(), r.Float32()), clear(r,s)},
// {&Complex128V, complex(r.Float64(), r.Float64()), clear(r,s)},
{&Complex64V, complex(r1.Float32(), r1.Float32())},
{&Complex128V, complex(r2.Float64(), r2.Float64())},
}
for _, test := range tests {
f := NewFiller(Seed(7))
f.Fill(test.value)
testValue := reflect.Indirect(reflect.ValueOf(test.value))
ifc := testValue.Interface()
fmt.Printf("Type: %v; \t\t Value: %v \n", testValue.Kind(), ifc)
if !reflect.DeepEqual(ifc, test.expectedValue) {
t.Errorf("Value mismatch (type: %v): value %v, want %v", testValue.Type(), ifc, test.expectedValue)
}
}
}
// Tests for debugging
func TestDebugSimpleInt(t *testing.T) {
i := 1
Fill(&i)
fmt.Println(i)
}
type S struct {
A string
B []struct {
Q []struct {
W int
}
C string
D string
E int
}
}
func TestDebugSimpleStruct(t *testing.T) {
i := S{}
Fill(&i)
b, err := json.Marshal(i)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(b))
fmt.Println(i)
}
type A struct {
T time.Time
Q string
}
func TestDebugSimpleTimeStruct(t *testing.T) {
i := A{}
Fill(&i)
b, err := json.Marshal(i)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(b))
fmt.Println(i)
}
type D struct {
Q string `json:"-"`
A string `json:"myName,omitempty"`
B string `datafiller:"-"`
C string `datafiller:"name,omitempty"`
}
func TestDebugSimpleTaggedStruct(t *testing.T) {
i := D{}
Fill(&i)
b, err := json.Marshal(i)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(b))
fmt.Println(i)
}