-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
206 lines (184 loc) · 3.25 KB
/
config_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
// Copyright (c) 2018 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package confi
import (
"reflect"
"testing"
"time"
)
type testConfig struct {
Foo struct {
Key1 bool
Key2 int
Key2b int8
Key3a int16
Key3 int32
Key4 int64
Key5 uint
Key5b uint8
Key6a uint16
Key6 uint32
Key7 uint64
Key8 float32
Key9 float64
Key10 string
Key11 []string
}
Bar int
Baz struct {
Quux testConfigQuux
Interval time.Duration
TestConfigEmbed
Embed1 struct {
TestConfigEmbed
}
Embed2 struct {
*TestConfigEmbed
}
}
Dummy struct{}
_ struct{}
Ignore struct {
A *int
B *struct{}
C func()
*int
_ *struct{}
d func()
}
Ext map[string]interface{}
}
type testConfigQuux struct {
Key_a string
Key_b bool
}
type TestConfigEmbed struct {
EmBedded bool
}
type testExtA struct{ Average int }
type testExtB struct{ Beverage int }
var testConfigTOML = `bar = 12345
[baz]
embedded = false
interval = "10h9m8.007006005s"
[baz.embed1]
embedded = false
[baz.embed2]
embedded = false
[baz.quux]
key_a = "true"
key_b = true
[ext.a]
average = 123
[ext.b]
beverage = 456
[foo]
key1 = true
key10 = "hello, world"
key11 = ["hello", "world"]
key2 = -10
key2b = -128
key3 = -11
key3a = -32768
key4 = -100000000000000
key5 = 10
key5b = 255
key6 = 11
key6a = 65535
key7 = 100000000000000
key8 = 1.5e+00
key9 = 1.0000000000005e+00
`
func newTestConfig() *testConfig {
c := new(testConfig)
c.Bar = 67890
c.Baz.Embed2.TestConfigEmbed = new(TestConfigEmbed)
c.Ext = map[string]interface{}{
"a": new(testExtA),
"b": new(testExtB),
}
return c
}
func testConfigValues(t *testing.T, c *testConfig) {
if !c.Foo.Key1 {
t.Error(c.Foo.Key1)
}
if c.Foo.Key2 != -10 {
t.Error(c.Foo.Key2)
}
if c.Foo.Key2b != -128 {
t.Error(c.Foo.Key2b)
}
if c.Foo.Key3a != -32768 {
t.Error(c.Foo.Key3a)
}
if c.Foo.Key3 != -11 {
t.Error(c.Foo.Key3)
}
if c.Foo.Key4 != -100000000000000 {
t.Error(c.Foo.Key4)
}
if c.Foo.Key5 != 10 {
t.Error(c.Foo.Key5)
}
if c.Foo.Key5b != 255 {
t.Error(c.Foo.Key5b)
}
if c.Foo.Key6a != 65535 {
t.Error(c.Foo.Key6a)
}
if c.Foo.Key6 != 11 {
t.Error(c.Foo.Key6)
}
if c.Foo.Key7 != 100000000000000 {
t.Error(c.Foo.Key7)
}
if c.Foo.Key8 != 1.5 {
t.Error(c.Foo.Key8)
}
if c.Foo.Key9 != 1.0000000000005 {
t.Error(c.Foo.Key9)
}
if c.Foo.Key10 != "hello, world" {
t.Error(c.Foo.Key10)
}
if !reflect.DeepEqual(c.Foo.Key11, []string{"hello", "world"}) {
t.Error()
}
if c.Bar != 12345 {
t.Error(c.Bar != 12345)
}
if c.Baz.Quux.Key_a != "true" {
t.Error(c.Baz.Quux.Key_a)
}
if !c.Baz.Quux.Key_b {
t.Error(c.Baz.Quux.Key_b)
}
if c.Baz.EmBedded {
t.Error(c.Baz.EmBedded)
}
if c.Baz.Embed1.EmBedded {
t.Error(c.Baz.Embed1.EmBedded)
}
if c.Baz.Embed2.EmBedded {
t.Error(c.Baz.Embed2.EmBedded)
}
if c.Baz.Interval != 10*time.Hour+9*time.Minute+8*time.Second+7*time.Millisecond+6*time.Microsecond+5*time.Nanosecond {
t.Error(c.Baz.Interval)
}
if a, ok := c.Ext["a"].(*testExtA); ok {
if a.Average != 123 {
t.Error(a.Average)
}
} else {
t.Error(c.Ext["a"])
}
if b, ok := c.Ext["b"].(*testExtB); ok {
if b.Beverage != 456 {
t.Error(b.Beverage)
}
} else {
t.Error(c.Ext["b"])
}
}