forked from koding/multiconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface_test.go
162 lines (134 loc) · 3.49 KB
/
interface_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
package multiconfig
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestInterfaceLoader(t *testing.T) {
t.Run("Should be a noop if nothing implements the interface", func(t *testing.T) {
loader := InterfaceLoader{}
conf := &TestConfig{}
require.NoError(t, loader.Load(conf))
require.Equal(t, &TestConfig{}, conf)
})
t.Run("Should invoke ApplyDefaults if top struct implements interface", func(t *testing.T) {
loader := InterfaceLoader{}
conf := &Config1{}
expected := &Config1{
Input: "default string",
Result: 42,
Flag: true,
}
require.NoError(t, loader.Load(conf))
require.Equal(t, expected, conf)
})
t.Run("Should return an error when trying to load into a non-pointer", func(t *testing.T) {
loader := InterfaceLoader{}
conf := Config1{}
require.EqualError(t, loader.Load(conf), "cannot load into a value: target must be a pointer")
})
t.Run("Should return an error if the input is nil", func(t *testing.T) {
loader := InterfaceLoader{}
var conf *Config1
require.EqualError(t, loader.Load(conf), "cannot load into a nil pointer")
})
t.Run("Should invoke ApplyDefaults on nested struct", func(t *testing.T) {
type NestedConfig struct {
Output string
Config1
}
loader := &InterfaceLoader{}
conf := &NestedConfig{}
expected := &NestedConfig{
Output: "",
Config1: Config1{
Input: "default string",
Result: 42,
Flag: true,
},
}
require.NoError(t, loader.Load(conf))
require.Equal(t, expected, conf)
})
t.Run("Should invoke ApplyDefaults on nested struct-pointer", func(t *testing.T) {
type NestedConfig struct {
Output string
Config1 *Config1
}
loader := &InterfaceLoader{}
conf := &NestedConfig{}
expected := &NestedConfig{
Output: "",
Config1: &Config1{
Input: "default string",
Result: 42,
Flag: true,
},
}
require.NoError(t, loader.Load(conf))
require.Equal(t, expected, conf)
})
t.Run("Should invoke ApplyDefaults recursively depth first", func(t *testing.T) {
loader := &InterfaceLoader{}
conf := &Config2{}
expected := &Config2{
Output: "my output",
Config1: Config1{
Input: "my input",
Result: 42,
Flag: true,
},
}
require.NoError(t, loader.Load(conf))
require.Equal(t, expected, conf)
})
// Not sure why anyone would do this, but someone will try so let's try to support it anyway
t.Run("Should work when ApplyDefaults is implemented on a non-struct type", func(t *testing.T) {
loader := &InterfaceLoader{}
conf := new(IntWithDefault)
expected := IntWithDefault(42)
require.NoError(t, loader.Load(conf))
require.Equal(t, &expected, conf)
})
t.Run("Should work when ApplyDefaults is implemented on a nested non-struct type", func(t *testing.T) {
loader := &InterfaceLoader{}
conf := &Config3{}
expected := &Config3{
Config1: Config1{
Input: "default string",
Result: 42,
Flag: true,
},
IntWithDefault: 42,
}
require.NoError(t, loader.Load(conf))
require.Equal(t, expected, conf)
})
}
type TestConfig struct {
pField string //nolint:unused
Input string
Result int
Flag bool
}
type Config1 TestConfig
func (c *Config1) ApplyDefaults() {
c.Input = "default string"
c.Result = 42
c.Flag = true
}
type Config2 struct {
Output string
Config1
}
func (c *Config2) ApplyDefaults() {
c.Output = "my output"
c.Config1.Input = "my input"
}
type IntWithDefault int
func (i *IntWithDefault) ApplyDefaults() {
*i = IntWithDefault(42)
}
type Config3 struct {
Config1
IntWithDefault
}