-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbind_defaults_test.go
81 lines (76 loc) · 2.93 KB
/
bind_defaults_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
package setdefault
import (
"reflect"
"testing"
)
type TestStruct struct {
StringField string `default:"default string value"`
IntField int `default:"123"`
Int8Field int8 `default:"123"`
Int16Field int16 `default:"123"`
Int32Field int32 `default:"123"`
Int64Field int64 `default:"123"`
UintField uint `default:"123"`
Uint8Field uint8 `default:"123"`
Uint16Field uint16 `default:"123"`
Uint32Field uint32 `default:"123"`
Uint64Field uint64 `default:"123"`
Float32Field float32 `default:"123.456"`
Float64Field float64 `default:"123.456"`
BoolField bool `default:"true"`
InvalidType int `default:"invalid value"`
UnsettableField int `default:"123"`
}
func TestBindDefault(t *testing.T) {
testStruct := TestStruct{UnsettableField: 123}
reflect.ValueOf(&testStruct).Elem().FieldByName("UnsettableField").Set(reflect.ValueOf(0).Convert(reflect.TypeOf(testStruct.UnsettableField)))
Bind(&testStruct)
if testStruct.StringField != "default string value" {
t.Errorf("Expected default value of 'default string value', but got '%s'", testStruct.StringField)
}
if testStruct.IntField != 123 {
t.Errorf("Expected default value of 123, but got '%d'", testStruct.IntField)
}
if testStruct.Int8Field != 123 {
t.Errorf("Expected default value of 123, but got '%d'", testStruct.Int8Field)
}
if testStruct.Int16Field != 123 {
t.Errorf("Expected default value of 123, but got '%d'", testStruct.Int16Field)
}
if testStruct.Int32Field != 123 {
t.Errorf("Expected default value of 123, but got '%d'", testStruct.Int32Field)
}
if testStruct.Int64Field != 123 {
t.Errorf("Expected default value of 123, but got '%d'", testStruct.Int64Field)
}
if testStruct.UintField != 123 {
t.Errorf("Expected default value of 123, but got '%d'", testStruct.UintField)
}
if testStruct.Uint8Field != 123 {
t.Errorf("Expected default value of 123, but got '%d'", testStruct.Uint8Field)
}
if testStruct.Uint16Field != 123 {
t.Errorf("Expected Uint16Field to be 123, but got %v", testStruct.Uint16Field)
}
if testStruct.Uint32Field != 123 {
t.Errorf("Expected Uint32Field to be 123, but got %v", testStruct.Uint32Field)
}
if testStruct.Uint64Field != 123 {
t.Errorf("Expected Uint64Field to be 123, but got %v", testStruct.Uint64Field)
}
if testStruct.Float32Field != 123.456 {
t.Errorf("Expected Float32Field to be 123.456, but got %v", testStruct.Float32Field)
}
if testStruct.Float64Field != 123.456 {
t.Errorf("Expected Float32Field to be 123.456, but got %v", testStruct.Float64Field)
}
if testStruct.BoolField != true {
t.Errorf("Expected BoolField != to be true, but got %v", testStruct.BoolField)
}
if testStruct.InvalidType != 0 {
t.Errorf("Expected BoolField != to be 0, but got %v", testStruct.InvalidType)
}
if testStruct.UnsettableField != 123 {
t.Errorf("Expected UnsettableField != to be 123, but got %v", testStruct.UnsettableField)
}
}