-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPropertiesDocument_test.go
495 lines (392 loc) · 10.7 KB
/
PropertiesDocument_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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package properties
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
"testing"
)
func expect(t *testing.T, msg string, result bool) {
if result {
return
}
t.Error(msg)
}
func Test_Load(t *testing.T) {
s := `
a=aa
b=bbb
c ccc = cccc
dd
# commment1
!comment2
ee: r-rt rr
`
p, err := Load(strings.NewReader(s))
if nil != err {
t.Error("加载失败")
return
}
v := ""
v = p.String("a")
if "aa" != v {
t.Error("Get string failed")
return
}
v = p.String("b")
if "bbb" != v {
t.Error("Get string failed")
return
}
v = p.String("Z")
if "" != v {
t.Error("Get string failed")
return
}
v = p.String("c ccc")
if "cccc" != v {
t.Error("Get string failed")
return
}
v = p.String("dd")
if "" != v {
t.Error("Get string failed")
return
}
v = p.String("ee")
if "r-rt rr" != v {
t.Error("Get string failed")
return
}
}
func Test_LoadFromFile(t *testing.T) {
file, err := os.Open("test1.properties")
if nil != err {
return
}
defer file.Close()
doc, err := Load(file)
if nil != err {
t.Error("加载失败")
return
}
fmt.Println(doc.String("key"))
}
func Test_New(t *testing.T) {
doc := New()
doc.Set("a", "aaa")
doc.Comment("a", "This is a comment for a")
buf := bytes.NewBufferString("")
Save(doc, buf)
if "#This is a comment for a\na=aaa\n" != buf.String() {
fmt.Println("Dump failed:[" + buf.String() + "]")
t.Error("Dump failed")
return
}
}
func Test_Save(t *testing.T) {
doc := New()
doc.Set("a", "aaa")
doc.Comment("a", "This is a comment for a")
buf := bytes.NewBufferString("")
Save(doc, buf)
expect(t, "注释之后保存", "#This is a comment for a\na=aaa\n" == buf.String())
}
func Test_Get(t *testing.T) {
str := `
key1=1
key 2 = 2
`
doc, _ := Load(bytes.NewBufferString(str))
value, exist := doc.Get("key1")
expect(t, "检测Get函数的行为:EXIST", true == exist)
expect(t, "检测Get函数的行为:EXIST", value == "1")
value, exist = doc.Get("NOT-EXIST")
expect(t, "检测Get函数的行为:NOT-EXIST", false == exist)
expect(t, "检测Get函数的行为:NOT-EXIST", value == "")
}
func Test_Set(t *testing.T) {
str := `
key1=1
key 2 = 2
`
doc, _ := Load(bytes.NewBufferString(str))
doc.Set("key1", "new-value")
newValue, _ := doc.Get("key1")
expect(t, "修改已经存在的项的值", "new-value" == newValue)
doc.Set("NOT-EXIST", "Setup-New-Item")
newValue, _ = doc.Get("NOT-EXIST")
expect(t, "修改不存在的项,默认是新增行为", "Setup-New-Item" == newValue)
}
func Test_Del(t *testing.T) {
str := `
key1=1
key 2 = 2
`
doc, _ := Load(bytes.NewBufferString(str))
exist := doc.Del("NOT-EXIST")
expect(t, "删除不存在的项,需要返回false", false == exist)
exist = doc.Del("key1")
expect(t, "删除已经存在的项,返回true", true == exist)
}
func Test_Comment_Uncomment(t *testing.T) {
str := "key1=1\nkey 2 = 2"
doc, _ := Load(bytes.NewBufferString(str))
exist := doc.Comment("NOT-EXIST", "Some comment")
expect(t, "对一个不存在的项执行注释操作,返回false", false == exist)
doc.Comment("key1", "This is a \ncomment \nfor a")
buf := bytes.NewBufferString("")
err := Save(doc, buf)
expect(t, "格式化成功", nil == err)
exp1 := "#This is a \n#comment \n#for a\nkey1=1\nkey 2=2\n"
expect(t, "对已经存在的项进行注释", exp1 == buf.String())
doc.Comment("key 2", "")
buf = bytes.NewBufferString("")
err = Save(doc, buf)
expect(t, "格式化成功", nil == err)
exp2 := "#This is a \n#comment \n#for a\nkey1=1\n#\nkey 2=2\n"
expect(t, "对已经存在的项进行注释", exp2 == buf.String())
exist = doc.Uncomment("key1")
expect(t, "对已经存在的key进行注释,返回true", true == exist)
buf = bytes.NewBufferString("")
err = Save(doc, buf)
expect(t, "格式化成功", nil == err)
exp3 := "key1=1\n#\nkey 2=2\n"
expect(t, "对已经存在的项进行注释", exp3 == buf.String())
exist = doc.Uncomment("key 2")
expect(t, "对已经存在的key进行注释,返回true", true == exist)
expect(t, "对已经存在的项进行注释", exp3 == buf.String())
buf = bytes.NewBufferString("")
err = Save(doc, buf)
expect(t, "格式化成功", nil == err)
exp4 := "key1=1\nkey 2=2\n"
expect(t, "对已经存在的项进行注释", exp4 == buf.String())
exist = doc.Uncomment("NOT-EXIST")
expect(t, "对不已经存在的key进行注释,返回false", false == exist)
}
func Test_Accept(t *testing.T) {
str := `
#comment1
key1=1
#comment2
key 2 : 2
key3 : 2
#coment3
`
count := 0
doc, _ := Load(bytes.NewBufferString(str))
doc.Accept(func(typo byte, value string, key string) bool {
count++
if ':' == typo {
return false
}
return true
})
expect(t, "Accept提前中断", 5 == count)
}
func Test_Foreach(t *testing.T) {
str := `
#comment1
key1=1
#comment2
key 2 : 2
key3 : 2
#coment3
`
count := 0
doc, _ := Load(bytes.NewBufferString(str))
doc.Foreach(func(value string, key string) bool {
count++
if "key 2" == key {
return false
}
return true
})
expect(t, "Foreach提前中断", 2 == count)
}
func Test_IntDefault(t *testing.T) {
str := `
key1 : 1
key2 : 123456789012345678901234567890
key3 : abc
key4 : 444 sina
key5 : 555 007
`
doc, _ := Load(bytes.NewBufferString(str))
v := doc.IntDefault("key1", 111)
expect(t, "属性key已经存在时,返回存在的值", 1 == v)
v = doc.IntDefault("NOT-EXIST", 111)
expect(t, "属性key已经不存在时,返回缺省值", 111 == v)
v = doc.IntDefault("key2", 111)
expect(t, "属性key转换失败时,返回缺省值", 111 == v)
v = doc.IntDefault("key3", 111)
expect(t, "属性key转换失败时,返回缺省值", 111 == v)
v = doc.IntDefault("key4", 111)
expect(t, "属性key转换失败时,返回缺省值", 111 == v)
v = doc.IntDefault("key5", 111)
expect(t, "属性key转换失败时,返回缺省值", 111 == v)
}
func Test_UintDefault(t *testing.T) {
str := `
key1 : 1
key2 : 123456789012345678901234567890
key3 : abc
key4 : 444 sina
key5 : 555 007
`
doc, _ := Load(bytes.NewBufferString(str))
v := doc.UintDefault("key1", 111)
expect(t, "属性key已经存在时,返回存在的值", 1 == v)
v = doc.UintDefault("NOT-EXIST", 111)
expect(t, "属性key已经不存在时,返回缺省值", 111 == v)
v = doc.UintDefault("key2", 111)
expect(t, "属性key转换失败时,返回缺省值", 111 == v)
v = doc.UintDefault("key3", 111)
expect(t, "属性key转换失败时,返回缺省值", 111 == v)
v = doc.UintDefault("key4", 111)
expect(t, "属性key转换失败时,返回缺省值", 111 == v)
v = doc.UintDefault("key5", 111)
expect(t, "属性key转换失败时,返回缺省值", 111 == v)
}
func Test_FloatDefault(t *testing.T) {
str := `
key1 : 1
key2 : 123456789.012345678901234567890
key3 : abc
key4 : 123456789.a
key5 : 123456789.
`
doc, _ := Load(bytes.NewBufferString(str))
v := doc.FloatDefault("key1", 111.0)
expect(t, "属性key已经存在时,返回存在的值", 1.0 == v)
v = doc.FloatDefault("key2", 111.0)
expect(t, "属性key已经存在时,返回存在的值", (v > 123456789.0) && (v < 123456789.1))
v = doc.FloatDefault("key5", 111.0)
expect(t, "属性key已经存在时,返回存在的值", 123456789. == v)
v = doc.FloatDefault("key3", 111.0)
expect(t, "转换失败时,返回def", 111.0 == v)
v = doc.FloatDefault("key4", 111.0)
expect(t, "转换失败时,返回def", 111.0 == v)
v = doc.FloatDefault("NOT-EXIST", 111.0)
expect(t, "属性不存在时,返回def", 111.0 == v)
}
func Test_BoolDefault(t *testing.T) {
str := `
key0 : 1
key1 : T
key2 : t
key3 : true
key4 : TRUE
key5 : True
key6 : 0
key7 : F
key8 : f
key9 : false
key10 : FALSE
key11 : False
key12 : Sina
key13 : fALSE
`
doc, _ := Load(bytes.NewBufferString(str))
for i := 0; i <= 5; i++ {
v := doc.BoolDefault(fmt.Sprintf("key%d", i), false)
expect(t, "BoolDefault基本场景", v == true)
}
for i := 6; i <= 11; i++ {
v := doc.BoolDefault(fmt.Sprintf("key%d", i), true)
expect(t, "BoolDefault基本场景", v == false)
}
v := doc.BoolDefault("NOT-EXIST", true)
expect(t, "获取不存在的项,返回def", v == true)
v = doc.BoolDefault("key12", false)
expect(t, "无法转换的,返回def", v == false)
v = doc.BoolDefault("key13", true)
expect(t, "无法转换的,返回def", v == true)
}
func Test_ObjectDefault(t *testing.T) {
str := `
key1 = 1
key2 = man
key3 = women
key4 = 0
key5
`
// 映射函数
mapping := func(k string, v string) (interface{}, error) {
if "0" == v {
return 0, nil
}
if "1" == v {
return 1, nil
}
if "man" == v {
return 1, nil
}
if "women" == v {
return 0, nil
}
if "" == v {
return 0, errors.New("INVALID")
}
return -1, nil
}
doc, _ := Load(bytes.NewBufferString(str))
expect(t, "ObjectDefault:属性key已经存在时,返回存在的值1", 1 == doc.ObjectDefault("key1", 123, mapping).(int))
expect(t, "ObjectDefault:属性key已经存在时,返回存在的值2", 1 == doc.ObjectDefault("key2", 123, mapping).(int))
expect(t, "ObjectDefault:属性key已经存在时,返回存在的值3", 0 == doc.ObjectDefault("key3", 123, mapping).(int))
expect(t, "ObjectDefault:属性key已经存在时,返回存在的值4", 0 == doc.ObjectDefault("key4", 123, mapping).(int))
expect(t, "ObjectDefault:属性key转换失败时,返回def值5", 123 == doc.ObjectDefault("key5", 123, mapping).(int))
expect(t, "ObjectDefault:属性key不存在时,返回def值5", 123 == doc.ObjectDefault("NOT-EXIST", 123, mapping).(int))
}
func Test_Object(t *testing.T) {
str := `
key1 = 1
key2 = man
key3 = women
key4 = 0
key5
`
// 映射函数
mapping := func(k string, v string) (interface{}, error) {
if "0" == v {
return 0, nil
}
if "1" == v {
return 1, nil
}
if "man" == v {
return 1, nil
}
if "women" == v {
return 0, nil
}
if "" == v {
return 0, errors.New("INVALID")
}
return -1, nil
}
doc, _ := Load(bytes.NewBufferString(str))
expect(t, "ObjectDefault:属性key已经存在时,返回存在的值1", 1 == doc.Object("key1", mapping).(int))
// nil不是万能类型,需要再想办法
//expect(t, "ObjectDefault:属性key不存在时,返回nil值1", 0 == doc.Object("NOT-EXIST", mapping).(int))
}
func Test_Int_String_Uint_Float_Bool(t *testing.T) {
str := `
key0 : -1
key1 : timo
key2 : 1234
key3 : 12.5
key4 : false
`
doc, _ := Load(bytes.NewBufferString(str))
expect(t, "Int", -1 == doc.Int("key0"))
expect(t, "String", "-1" == doc.String("key0"))
expect(t, "String", "timo" == doc.String("key1"))
expect(t, "String", "1234" == doc.String("key2"))
expect(t, "String", "12.5" == doc.String("key3"))
expect(t, "String", "false" == doc.String("key4"))
expect(t, "Int", 1234 == doc.Uint("key2"))
expect(t, "Int", 12.5 == doc.Float("key3"))
expect(t, "Int", false == doc.Bool("key4"))
}