-
Notifications
You must be signed in to change notification settings - Fork 41
/
conf_test.go
245 lines (237 loc) · 5.43 KB
/
conf_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
package goconf
import (
"testing"
"time"
)
var (
conf *Config
)
func init() {
file := "./examples/conf_test.txt"
conf = New()
if err := conf.Parse(file); err != nil {
panic(err)
}
}
type TestConfig struct {
ID int `goconf:"core:id"`
Col string `goconf:"core:col"`
Ignore int `goconf:"-"`
Arr []string `goconf:"core:arr:,"`
Arr1 []int `goconf:"core:arr1:,"`
Test time.Duration `goconf:"core:t_1:time"`
Buf int `goconf:"core:buf:memory"`
M map[int]string `goconf:"core:map:,"`
}
func TestSection(t *testing.T) {
section := "core"
core := conf.Get(section)
if core == nil {
t.Errorf("not found section:\"%s\"", section)
t.FailNow()
}
section = "test"
test := conf.Get(section)
if test == nil {
t.Errorf("not found section:\"%s\"", section)
t.FailNow()
}
section = "test1"
test1 := conf.Get(section)
if test1 == nil {
t.Errorf("not found section:\"%s\"", section)
t.FailNow()
}
key := "id"
if id, err := core.Int(key); err != nil {
t.Errorf("core.Int(\"%s\") failed (%s)", key, err.Error())
t.FailNow()
} else {
if id != 1 {
t.Errorf("%s not equals 1", key)
t.FailNow()
}
}
key = "col"
if col, err := core.String(key); err != nil {
t.Errorf("core.String(\"%s\") failed (%s)", key, err.Error())
t.FailNow()
} else {
if col != "goconf" {
t.Errorf("%s not equals \"goconf\"", key)
t.FailNow()
}
}
key = "f"
if f, err := core.Float(key); err != nil {
t.Errorf("core.Float(\"%s\") failed (%s)", key, err.Error())
t.FailNow()
} else {
if f != 1.23 {
t.Errorf("%s not equals 1.23", key)
t.FailNow()
}
}
key = "b"
if b, err := core.Bool(key); err != nil {
t.Errorf("core.Bool(\"%s\") failed (%s)", key, err.Error())
t.FailNow()
} else {
if !b {
t.Errorf("%s not equals true", key)
t.FailNow()
}
}
key = "buf"
if buf, err := core.MemSize(key); err != nil {
t.Errorf("core.MemSize(\"%s\") failed (%s)", key, err.Error())
t.FailNow()
} else {
if buf != 1*1024*1024*1024 {
t.Errorf("%s not equals 1*1024*1024*1024", key)
t.FailNow()
}
}
key = "sleep"
if sleep, err := core.Duration(key); err != nil {
t.Errorf("core.Duration(\"%s\") failed (%s)", key, err.Error())
t.FailNow()
} else {
if sleep != 10*time.Second {
t.Errorf("%s not equals 10*Second", key)
t.FailNow()
}
}
key = "do"
if do, err := core.String(key); err != nil {
t.Errorf("core.String(\"%s\") failed (%s)", key, err.Error())
t.FailNow()
} else {
if do != "hehe" {
t.Errorf("%s not equals \"hehe\"", key)
t.FailNow()
}
}
key = "id2"
if id2, err := test.Int(key); err != nil {
t.Errorf("test.Int(\"%s\") failed (%s)", key, err.Error())
t.FailNow()
} else {
if id2 != 2 {
t.Errorf("%s not equals 2", key)
t.FailNow()
}
}
key = "id3"
if id3, err := test1.Bool(key); err != nil {
t.Errorf("test.Bool(\"%s\") failed (%s)", key, err.Error())
t.FailNow()
} else {
if !id3 {
t.Errorf("%s not equals false", key)
t.FailNow()
}
}
test1.Add("id4", "goconf baby", " hahah\n heihei,woshishei")
save := "./examples/conf_reload.txt"
if err := conf.Save(save); err != nil {
t.Errorf("conf.Save(\"%s\") failed (%s)", save, err.Error())
t.FailNow()
}
test1.Remove("id4")
save = "./examples/conf_reload1.txt"
if err := conf.Save(save); err != nil {
t.Errorf("conf.Save(\"%s\") failed (%s)", save, err.Error())
t.FailNow()
}
conf.Remove("test1")
save = "./examples/conf_reload2.txt"
if err := conf.Save(save); err != nil {
t.Errorf("conf.Save(\"%s\") failed (%s)", save, err.Error())
t.FailNow()
}
if _, err := conf.Reload(); err != nil {
t.Errorf("conf.Reload() failed (%s)", err.Error())
t.FailNow()
}
// test unmarshall
tf := &TestConfig{}
if err := conf.Unmarshal(tf); err != nil {
t.Errorf("c.Unmarshal() failed (%s)", err.Error())
t.FailNow()
}
if tf.ID != 1 {
t.Errorf("TestConfig ID not equals 1")
t.FailNow()
}
if tf.Col != "goconf" {
t.Errorf("TestConfig Col not equals \"goconf\"")
t.FailNow()
}
if len(tf.Arr) != 4 {
t.Errorf("TestConfig Arr length not equals 4")
t.FailNow()
}
if tf.Arr[0] != "1" {
t.Errorf("TestConfig Arr[0] length not equals \"1\"")
t.FailNow()
}
if tf.Arr[1] != "2" {
t.Errorf("TestConfig Arr[1] length not equals \"2\"")
t.FailNow()
}
if tf.Arr[2] != "3" {
t.Errorf("TestConfig Arr[2] length not equals \"3\"")
t.FailNow()
}
if tf.Arr[3] != "come on baby" {
t.Errorf("TestConfig Arr[3] length not equals \"come on baby\"")
t.FailNow()
}
if len(tf.Arr1) != 3 {
t.Errorf("TestConfig Arr length not equals 4")
t.FailNow()
}
if tf.Arr1[0] != 1 {
t.Errorf("TestConfig Arr1[0] length not equals 1")
t.FailNow()
}
if tf.Arr1[1] != 3 {
t.Errorf("TestConfig Arr[1] length not equals 3")
t.FailNow()
}
if tf.Arr1[2] != 4 {
t.Errorf("TestConfig Arr1[2] length not equals 4")
t.FailNow()
}
if tf.Test != 2*time.Hour {
t.Errorf("TestConfig t_1 not equals 2 * time.Hour")
t.FailNow()
}
if tf.Buf != 1*GB {
t.Errorf("TestConfig t_1 not equals 1 * GB")
t.FailNow()
}
if len(tf.M) != 2 {
t.Errorf("TestConfig M length not equals 2")
t.FailNow()
}
if v, ok := tf.M[1]; !ok {
t.Errorf("TestConfig M no key 1")
t.FailNow()
} else {
if v != "str" {
t.Errorf("TestConfig M[1] not equals \"str\"")
t.FailNow()
}
}
if v, ok := tf.M[2]; !ok {
t.Errorf("TestConfig M no key 2")
t.FailNow()
} else {
if v != "str1" {
t.Errorf("TestConfig M[2] not equals \"str1\"")
t.FailNow()
}
}
}