-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaulter_test.go
147 lines (132 loc) · 4.04 KB
/
defaulter_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
package config
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type structComplex struct {
Untouched string
Discard stringDefaultable `default:"-"`
Default stringDefaultable
PTRUntouched *stringDefaultable
PTRTouched *stringDefaultable
Simple structSimple
unexported stringDefaultable
}
type structSimple struct {
Different stringDefaultable
String string
}
func (sc *structSimple) SetDefault() { sc.Different = "people" }
type stringDefaultable string
func (s *stringDefaultable) SetDefault() { *s = "world" }
func Test_SetDefault(t *testing.T) {
sc := structComplex{
PTRTouched: new(stringDefaultable),
unexported: "",
}
defaultable := stringDefaultable("world")
expectedSC := structComplex{
PTRTouched: &defaultable,
Default: "world",
Simple: structSimple{
Different: "people",
},
}
require.NoError(t, SetDefault(&sc))
assert.Equal(t, expectedSC, sc)
assert.Error(t, SetDefault(nil))
}
func Test_tryToSetDefault(t *testing.T) {
var (
zeroString = ""
expectedZeroString = zeroString
nonZeroString = "hello"
expectedNonZeroString = nonZeroString
zeroCustomString = stringDefaultable("")
expectedZeroCustomString = stringDefaultable("world")
nonZerocustomString = stringDefaultable("hello")
expectedNonZeroCustomString = nonZerocustomString
)
tests := map[string]struct {
value reflect.Value
expectedValue reflect.Value
}{
"invalid value": {
value: reflect.ValueOf(nil),
}, "zero string": {
value: reflect.ValueOf(&zeroString),
expectedValue: reflect.ValueOf(&expectedZeroString),
}, "non-zero string": {
value: reflect.ValueOf(&nonZeroString),
expectedValue: reflect.ValueOf(&expectedNonZeroString),
}, "zero defaultable string": {
value: reflect.ValueOf(&zeroCustomString),
expectedValue: reflect.ValueOf(&expectedZeroCustomString),
}, "non-zero defaultable string": {
value: reflect.ValueOf(&nonZerocustomString),
expectedValue: reflect.ValueOf(&expectedNonZeroCustomString),
},
}
for name, test := range tests {
// rescope test as we're gonna run them in parallel
var test = test
t.Run(name, func(t *testing.T) {
var value = reflect.Indirect(test.value)
tryToSetDefault(&value)
if test.expectedValue.IsValid() {
assert.Equal(t, test.expectedValue.Interface(), test.value.Interface())
}
})
}
}
func Test_isZeroValue(t *testing.T) {
tests := map[string]struct {
value reflect.Value
isExpectedToBeZero bool
}{
"invalid value": {
value: reflect.ValueOf(nil),
isExpectedToBeZero: false,
}, "empty string": {
value: reflect.ValueOf(""),
isExpectedToBeZero: true,
}, "non empty string": {
value: reflect.ValueOf("hello"),
isExpectedToBeZero: false,
}, "empty int": {
value: reflect.ValueOf(0),
isExpectedToBeZero: true,
}, "non empty int": {
value: reflect.ValueOf(42),
isExpectedToBeZero: false,
}, "empty struct": {
value: reflect.ValueOf(struct{ Name string }{}),
isExpectedToBeZero: true,
}, "non empty struct": {
value: reflect.ValueOf(struct{ Name string }{Name: "John Doe"}),
isExpectedToBeZero: false,
}, "empty array": {
value: reflect.ValueOf(([]string)(nil)),
isExpectedToBeZero: true,
}, "non empty array": {
value: reflect.ValueOf([]string{"hello", "world"}),
isExpectedToBeZero: false,
}, "empty map": {
value: reflect.ValueOf((map[string]string)(nil)),
isExpectedToBeZero: true,
}, "non empty map": {
value: reflect.ValueOf(map[string]string{"hello": "world"}),
isExpectedToBeZero: false,
},
}
for name, test := range tests {
// rescope test as we're gonna run them in parallel
var test = test
t.Run(name, func(t *testing.T) {
isZero := isZeroValue(&test.value)
assert.Equal(t, test.isExpectedToBeZero, isZero)
})
}
}