-
Notifications
You must be signed in to change notification settings - Fork 20
/
mini_test.go
762 lines (546 loc) · 24.7 KB
/
mini_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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
package mini
import (
"github.com/stretchr/testify/assert"
"os"
"path"
"strings"
"testing"
)
func TestSimpleIniFile(t *testing.T) {
simpleIni := `first=alpha
second=beta
third="gamma bamma"
fourth = 'delta'
int=32
float=3.14
true=true
false=false
#comment
; comment`
filepath := path.Join(os.TempDir(), "simpleini.txt")
f, err := os.Create(filepath)
if err != nil {
t.Fatal(err)
}
defer os.Remove(filepath)
if _, err := f.WriteString(simpleIni); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
config, err := LoadConfiguration(filepath)
assert.Nil(t, err, "Simple configuration should load without error.")
assert.Equal(t, config.String("first", ""), "alpha", "Read value of first wrong")
assert.Equal(t, config.String("second", ""), "beta", "Read value of second wrong")
assert.Equal(t, config.String("third", ""), "gamma bamma", "Read value of third wrong")
assert.Equal(t, config.String("fourth", ""), "delta", "Read value of fourth wrong")
assert.Equal(t, config.Integer("int", 0), 32, "Read value of int wrong")
assert.Equal(t, config.Float("float", 0), 3.14, "Read value of float wrong")
assert.Equal(t, config.Boolean("true", false), true, "Read true wrong")
assert.Equal(t, config.Boolean("false", true), false, "Read false wrong")
assert.Equal(t, len(config.Keys()), 8, "Simple ini contains 8 fields")
}
func TestSimpleIniFileFromReader(t *testing.T) {
simpleIni := `first=alpha
second=beta
third="gamma bamma"
fourth = 'delta'
int=32
float=3.14
true=true
false=false
#comment
; comment`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Simple configuration should load without error.")
assert.Equal(t, config.String("first", ""), "alpha", "Read value of first wrong")
assert.Equal(t, config.String("second", ""), "beta", "Read value of second wrong")
assert.Equal(t, config.String("third", ""), "gamma bamma", "Read value of third wrong")
assert.Equal(t, config.String("fourth", ""), "delta", "Read value of fourth wrong")
assert.Equal(t, config.Integer("int", 0), 32, "Read value of int wrong")
assert.Equal(t, config.Float("float", 0), 3.14, "Read value of float wrong")
assert.Equal(t, config.Boolean("true", false), true, "Read true wrong")
assert.Equal(t, config.Boolean("false", true), false, "Read false wrong")
assert.Equal(t, len(config.Keys()), 8, "Simple ini contains 8 fields")
}
func TestCaseInsensitive(t *testing.T) {
simpleIni := `fIrst=alpha
SECOND=beta
Third="gamma bamma"
FourTh = 'delta'`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Case insensitive configuration should load without error.")
assert.Equal(t, config.String("first", ""), "alpha", "Read value of first wrong")
assert.Equal(t, config.String("second", ""), "beta", "Read value of second wrong")
assert.Equal(t, config.String("THIRD", ""), "gamma bamma", "Read value of third wrong")
assert.Equal(t, config.String("fourth", ""), "delta", "Read value of fourth wrong")
assert.Equal(t, len(config.Keys()), 4, "Case ins ini contains 4 fields")
}
func TestArrayOfStrings(t *testing.T) {
simpleIni := `key[]=one
key[]=two
noarray=three`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Simple configuration should load without error.")
val := config.Strings("key")
assert.Equal(t, len(val), 2, "Array for keys should have 2 values")
assert.Equal(t, val[0], "one", "Read value of first wrong")
assert.Equal(t, val[1], "two", "Read value of second wrong")
val = config.Strings("noarray")
assert.Equal(t, len(val), 1, "Array for noarray should have 1 value")
assert.Equal(t, val[0], "three", "Read value of noarray wrong")
assert.Equal(t, len(config.Keys()), 2, "StringArray test contains 2 fields")
}
func TestArrayOfIntegers(t *testing.T) {
simpleIni := `key[]=1
key[]=2
noarray=3`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Simple configuration should load without error.")
val := config.Integers("key")
assert.Equal(t, len(val), 2, "Array for keys should have 2 values")
assert.Equal(t, val[0], 1, "Read value of first wrong")
assert.Equal(t, val[1], 2, "Read value of second wrong")
val = config.Integers("noarray")
assert.Equal(t, len(val), 1, "Array for noarray should have 1 value")
assert.Equal(t, val[0], 3, "Read value of noarray wrong")
assert.Equal(t, len(config.Keys()), 2, "IntArray test contains 2 fields")
}
func TestArrayOfFloats(t *testing.T) {
simpleIni := `key[]=1.1
key[]=2.2
noarray=3.3`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Simple configuration should load without error.")
val := config.Floats("key")
assert.Equal(t, len(val), 2, "Array for keys should have 2 values")
assert.Equal(t, val[0], 1.1, "Read value of first wrong")
assert.Equal(t, val[1], 2.2, "Read value of second wrong")
val = config.Floats("noarray")
assert.Equal(t, len(val), 1, "Array for noarray should have 1 value")
assert.Equal(t, val[0], 3.3, "Read value of noarray wrong")
assert.Equal(t, len(config.Keys()), 2, "FloatArray test contains 2 fields")
}
func TestSectionedIniFile(t *testing.T) {
simpleIni := `first=alpha
second=beta
third="gamma bamma"
fourth = 'delta'
int=32
float=3.14
true=true
false=false
[section]
first=raz
second=dba
int=124
float=1222.7
true=false
false=true
#comment
; comment`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Sectioned configuration should load without error.")
assert.Equal(t, config.String("first", ""), "alpha", "Read value of first wrong")
assert.Equal(t, config.String("second", ""), "beta", "Read value of second wrong")
assert.Equal(t, config.String("third", ""), "gamma bamma", "Read value of third wrong")
assert.Equal(t, config.String("fourth", ""), "delta", "Read value of fourth wrong")
assert.Equal(t, config.Integer("int", 0), 32, "Read value of int wrong")
assert.Equal(t, config.Float("float", 0), 3.14, "Read value of float wrong")
assert.Equal(t, config.Boolean("true", false), true, "Read true wrong")
assert.Equal(t, config.Boolean("false", true), false, "Read false wrong")
assert.Equal(t, config.StringFromSection("section", "first", ""), "raz", "Read value of first from section wrong")
assert.Equal(t, config.StringFromSection("section", "second", ""), "dba", "Read value of second from section wrong")
assert.Equal(t, config.IntegerFromSection("section", "int", 0), 124, "Read value of int in section wrong")
assert.Equal(t, config.FloatFromSection("section", "float", 0), 1222.7, "Read value of float in section wrong")
assert.Equal(t, config.BooleanFromSection("section", "true", true), false, "Read true in section wrong")
assert.Equal(t, config.BooleanFromSection("section", "false", false), true, "Read false in section wrong")
assert.Equal(t, len(config.Keys()), 8, "Section ini contains 6 fields")
assert.Equal(t, len(config.KeysForSection("section")), 6, "Section in ini contains 4 fields")
}
func TestArrayOfStringsInSection(t *testing.T) {
simpleIni := `key=nope
noarray=nope
[section]
key[]=one
key[]=two
noarray=three`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Configuration should load without error.")
val := config.StringsFromSection("section", "key")
assert.Equal(t, len(val), 2, "Array for keys should have 2 values")
assert.Equal(t, val[0], "one", "Read value of first wrong")
assert.Equal(t, val[1], "two", "Read value of second wrong")
val = config.StringsFromSection("section", "noarray")
assert.Equal(t, len(val), 1, "Array for noarray should have 1 value")
assert.Equal(t, val[0], "three", "Read value of noarray wrong")
assert.Equal(t, len(config.Keys()), 2, "StringArray section test contains 2 fields")
}
func TestArrayOfIntegersInSection(t *testing.T) {
simpleIni := `key=nope
noarray=nope
[section]
key[]=1
key[]=2
noarray=3`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Configuration should load without error.")
val := config.IntegersFromSection("section", "key")
assert.Equal(t, len(val), 2, "Array for keys should have 2 values")
assert.Equal(t, val[0], 1, "Read value of first wrong")
assert.Equal(t, val[1], 2, "Read value of second wrong")
val = config.IntegersFromSection("section", "noarray")
assert.Equal(t, len(val), 1, "Array for noarray should have 1 value")
assert.Equal(t, val[0], 3, "Read value of noarray wrong")
assert.Equal(t, len(config.KeysForSection("section")), 2, "IntArray section test contains 2 fields")
}
func TestArrayOfFloatsInSection(t *testing.T) {
simpleIni := `key=nope
noarray=nope
[section]
key[]=1.1
key[]=2.2
noarray=3.3`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Configuration should load without error.")
val := config.FloatsFromSection("section", "key")
assert.Equal(t, len(val), 2, "Array for keys should have 2 values")
assert.Equal(t, val[0], 1.1, "Read value of first wrong")
assert.Equal(t, val[1], 2.2, "Read value of second wrong")
val = config.FloatsFromSection("section", "noarray")
assert.Equal(t, len(val), 1, "Array for noarray should have 1 value")
assert.Equal(t, val[0], 3.3, "Read value of noarray wrong")
assert.Equal(t, len(config.Keys()), 2, "FloatArray section test contains 2 fields")
}
func TestMultipleSections(t *testing.T) {
simpleIni := `first=alpha
int=32
float=3.14
[section_one]
first=raz
int=124
float=1222.7
[section_two]
first=one
int=555
float=124.3`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Sectioned configuration should load without error.")
assert.Equal(t, config.String("first", ""), "alpha", "Read value of first wrong")
assert.Equal(t, config.Integer("int", 0), 32, "Read value of int wrong")
assert.Equal(t, config.Float("float", 0), 3.14, "Read value of float wrong")
assert.Equal(t, config.StringFromSection("", "first", ""), "alpha", "Read value of first wrong")
assert.Equal(t, config.IntegerFromSection("", "int", 0), 32, "Read value of int wrong")
assert.Equal(t, config.FloatFromSection("", "float", 0), 3.14, "Read value of float wrong")
config.SetName("section_zero")
assert.Equal(t, config.StringFromSection("section_zero", "first", ""), "alpha", "Read value of first wrong")
assert.Equal(t, config.IntegerFromSection("section_zero", "int", 0), 32, "Read value of int wrong")
assert.Equal(t, config.FloatFromSection("section_zero", "float", 0), 3.14, "Read value of float wrong")
assert.Equal(t, config.StringFromSection("section_one", "first", ""), "raz", "Read value of first from section wrong")
assert.Equal(t, config.IntegerFromSection("section_one", "int", 0), 124, "Read value of int in section wrong")
assert.Equal(t, config.FloatFromSection("section_one", "float", 0), 1222.7, "Read value of float in section wrong")
assert.Equal(t, config.StringFromSection("section_two", "first", ""), "one", "Read value of first from section wrong")
assert.Equal(t, config.IntegerFromSection("section_two", "int", 0), 555, "Read value of int in section wrong")
assert.Equal(t, config.FloatFromSection("section_two", "float", 0), 124.3, "Read value of float in section wrong")
assert.Equal(t, len(config.Keys()), 3, "Section ini contains 3 fields")
assert.Equal(t, len(config.KeysForSection("section_one")), 3, "Section in ini contains 3 fields")
assert.Equal(t, len(config.KeysForSection("section_two")), 3, "Section in ini contains 3 fields")
}
func TestSectionNames(t *testing.T) {
simpleIni := `first=alpha
int=32
float=3.14
[section_one]
first=raz
int=124
float=1222.7
[section_two]
first=one
[section_three]
int=555
float=124.3`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Sectioned configuration should load without error.")
sectionNames := config.SectionNames()
assert.Equal(t, len(sectionNames), 3, "Read section name array wrong")
//Section names should be sorted, alphabetically
assert.Equal(t, sectionNames[0], "section_one", "Read section name wrong")
assert.Equal(t, sectionNames[1], "section_three", "Read section name wrong")
assert.Equal(t, sectionNames[2], "section_two", "Read section name wrong")
config.SetName("section_zero")
sectionNames = config.SectionNames()
assert.Equal(t, len(sectionNames), 4, "Read section name array wrong")
//Section names should be sorted, alphabetically
assert.Equal(t, sectionNames[0], "section_one", "Read section name wrong")
assert.Equal(t, sectionNames[1], "section_three", "Read section name wrong")
assert.Equal(t, sectionNames[2], "section_two", "Read section name wrong")
assert.Equal(t, sectionNames[3], "section_zero", "Read section name wrong")
}
func TestSplitSection(t *testing.T) {
simpleIni := `first=alpha
int=32
float=3.14
[section_one]
first=raz
[section_two]
first=one
int=555
float=124.3
[section_one]
int=124
float=1222.7`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Sectioned configuration should load without error.")
assert.Equal(t, config.String("first", ""), "alpha", "Read value of first wrong")
assert.Equal(t, config.Integer("int", 0), 32, "Read value of int wrong")
assert.Equal(t, config.Float("float", 0), 3.14, "Read value of float wrong")
assert.Equal(t, config.StringFromSection("section_one", "first", ""), "raz", "Read value of first from section wrong")
assert.Equal(t, config.IntegerFromSection("section_one", "int", 0), 124, "Read value of int in section wrong")
assert.Equal(t, config.FloatFromSection("section_one", "float", 0), 1222.7, "Read value of float in section wrong")
assert.Equal(t, config.StringFromSection("section_two", "first", ""), "one", "Read value of first from section wrong")
assert.Equal(t, config.IntegerFromSection("section_two", "int", 0), 555, "Read value of int in section wrong")
assert.Equal(t, config.FloatFromSection("section_two", "float", 0), 124.3, "Read value of float in section wrong")
assert.Equal(t, len(config.Keys()), 3, "Section ini contains 3 fields")
assert.Equal(t, len(config.KeysForSection("section_one")), 3, "Section in ini contains 3 fields")
assert.Equal(t, len(config.KeysForSection("section_two")), 3, "Section in ini contains 3 fields")
}
func TestRepeatedKey(t *testing.T) {
simpleIni := `first=alpha
first=beta`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Configuration should load without error.")
assert.Equal(t, config.String("first", ""), "beta", "Read value of first wrong")
assert.Equal(t, len(config.Keys()), 1, "ini contains 1 fields")
}
func TestDefaults(t *testing.T) {
simpleIni := `first=alpha
third=\`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Configuration should load without error.")
assert.Equal(t, config.String("second", "beta"), "beta", "Read default value of first wrong")
assert.Equal(t, config.String("third", "gamma"), "gamma", "Read default value of too short a string")
assert.Equal(t, config.Integer("int", 32), 32, "Default value of int wrong")
assert.Equal(t, config.Float("float", 3.14), 3.14, "Default value of float wrong")
assert.Equal(t, config.Boolean("bool", true), true, "Default value of bool wrong")
assert.Equal(t, config.String("", "test"), "test", "Nil key should result in empty value")
assert.Equal(t, config.Integer("", 32), 32, "Default value of int wrong for empty key")
assert.Equal(t, config.Float("", 3.14), 3.14, "Default value of float wrong for empty key")
assert.Equal(t, config.Boolean("", true), true, "Default value of bool wrong for empty key")
assert.Equal(t, len(config.Keys()), 2, "ini contains 2 fields")
}
func TestDefaultsOnParseError(t *testing.T) {
simpleIni := `first=alpha
int=yex
float=blap
bool=zipzap
intarray[]=blip
floatarray[]=blap`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Configuration should load without error.")
assert.Equal(t, config.String("second", "beta"), "beta", "Read default value of first wrong")
assert.Equal(t, config.Integer("int", 32), 32, "Default value of int wrong")
assert.Equal(t, config.Float("float", 3.14), 3.14, "Default value of float wrong")
assert.Equal(t, config.Boolean("bool", true), true, "Default value of bool wrong")
assert.Nil(t, config.Integers("intarray"), "Default value of ints wrong on parse error")
assert.Nil(t, config.Floats("floatarray"), "Default value of floats wrong on parse error")
assert.Equal(t, len(config.Keys()), 6, "ini contains 4 fields")
}
func TestMissingArray(t *testing.T) {
simpleIni := `first=alpha`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Configuration should load without error.")
assert.Nil(t, config.Strings("second"), "Read default value of strings wrong")
assert.Nil(t, config.Integers("int"), "Default value of ints wrong")
assert.Nil(t, config.Floats("float"), "Default value of floats wrong")
assert.Nil(t, config.Strings(""), "Read default value of strings wrong for empty key")
assert.Nil(t, config.Integers(""), "Default value of ints wrong for empty key")
assert.Nil(t, config.Floats(""), "Default value of floats wrong for empty key")
assert.Equal(t, len(config.Keys()), 1, "ini contains 1 fields")
}
func TestDefaultsWithSection(t *testing.T) {
simpleIni := `[section]
first=alpha`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Configuration should load without error.")
assert.Equal(t, config.StringFromSection("section", "second", "beta"), "beta", "Read default value of first wrong")
assert.Equal(t, config.IntegerFromSection("section", "int", 32), 32, "Default value of int wrong")
assert.Equal(t, config.FloatFromSection("section", "float", 3.14), 3.14, "Default value of float wrong")
assert.Equal(t, config.BooleanFromSection("section", "bool", true), true, "Default value of bool wrong")
assert.Equal(t, config.StringFromSection("section-1", "second", "beta"), "beta", "Missing section for first wrong")
assert.Equal(t, config.IntegerFromSection("section-1", "int", 32), 32, "Missing section for int wrong")
assert.Equal(t, config.FloatFromSection("section-1", "float", 3.14), 3.14, "Missing section for float wrong")
assert.Equal(t, config.BooleanFromSection("section-1", "bool", true), true, "Missing section for bool wrong")
assert.Equal(t, len(config.Keys()), 0, "ini contains 0 fields")
assert.Equal(t, len(config.KeysForSection("section")), 1, "section contains 1 field")
assert.Nil(t, config.KeysForSection("section-1"), "missing section should have no keys")
}
type testStruct struct {
First string
Second string
L int64
F64 float64
Flag bool
Strings []string
LS []int64
F64s []float64
Missing string
MissingInt int64
MissingArray []string
Flags []bool
U uint64
private string
}
func TestLoadStructFile(t *testing.T) {
simpleIni := `[section]
first=alpha
second=beta
third="gamma bamma"
fourth = 'delta'
l=-32
f64=3.14
flag=true
unflag=false
strings[]=one
strings[]=two
LS[]=1
LS[]=2
F64s[]=11.0
F64s[]=22.0
#comment
; comment`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Simple configuration should load without error.")
var data testStruct
data.MissingInt = 33
data.Missing = "hello world"
ok := config.DataFromSection("section", &data)
assert.Equal(t, ok, true, "load should succeed")
assert.Equal(t, data.First, "alpha", "Read value of first wrong")
assert.Equal(t, data.Second, "beta", "Read value of second wrong")
assert.Equal(t, data.L, -32, "Read value of int wrong")
assert.Equal(t, data.F64, 3.14, "Read value of float wrong")
assert.Equal(t, data.Flag, true, "Read true wrong")
assert.Equal(t, data.Missing, "hello world", "Read value of missing wrong")
assert.Equal(t, data.MissingInt, 33, "Read false wrong")
assert.Nil(t, data.MissingArray, "Missing array Should be nil")
assert.Equal(t, data.private, "", "private value in struct should be ignored")
strings := data.Strings
assert.NotNil(t, strings, "strings should not be nil")
assert.Equal(t, len(strings), 2, "Read wrong length of string array")
assert.Equal(t, strings[0], "one", "Read string array wrong")
assert.Equal(t, strings[1], "two", "Read string array wrong")
assert.NotNil(t, data.LS, "ints should not be nil")
assert.Equal(t, len(data.LS), 2, "Read wrong length of ints array")
assert.Equal(t, data.LS[0], 1, "Read ints array wrong")
assert.Equal(t, data.LS[1], 2, "Read ints array wrong")
assert.NotNil(t, data.F64s, "floats should not be nil")
assert.Equal(t, len(data.F64s), 2, "Read wrong length of floats array")
assert.Equal(t, data.F64s[0], 11.0, "Read floats array wrong")
assert.Equal(t, data.F64s[1], 22.0, "Read floats array wrong")
}
func TestLoadStructMissingSection(t *testing.T) {
simpleIni := ``
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Simple configuration should load without error.")
var data testStruct
ok := config.DataFromSection("section", &data)
assert.Equal(t, ok, false, "section is missing so ok should be false")
}
func TestMissingSection(t *testing.T) {
simpleIni := `[section]
first=alpha`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Simple configuration should load without error.")
var data testStruct
val := config.DataFromSection("missing_section", &data)
assert.False(t, val)
}
func TestMissingArrayInSection(t *testing.T) {
simpleIni := `[section]
first=alpha`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Configuration should load without error.")
assert.Nil(t, config.StringsFromSection("section", "second"), "Read default value of strings wrong")
assert.Nil(t, config.IntegersFromSection("section", "int"), "Default value of ints wrong")
assert.Nil(t, config.FloatsFromSection("section", "float"), "Default value of floats wrong")
assert.Nil(t, config.StringsFromSection("section-1", "second"), "Missing section for strings wrong")
assert.Nil(t, config.IntegersFromSection("section-1", "int"), "Missing section for ints wrong")
assert.Nil(t, config.FloatsFromSection("section-1", "float"), "Missing section for floats wrong")
assert.Equal(t, len(config.Keys()), 0, "ini contains 0 fields")
assert.Equal(t, len(config.KeysForSection("section")), 1, "section contains 1 field")
assert.Nil(t, config.KeysForSection("section-1"), "missing section should have no keys")
}
func TestBadSection(t *testing.T) {
simpleIni := `key=nope
noarray=nope
[section
key[]=one
key[]=two
noarray=three`
_, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.NotNil(t, err, "Configuration should load with error.")
}
func TestBadKeyValue(t *testing.T) {
simpleIni := `key=nope
noarray:nope`
_, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.NotNil(t, err, "Configuration should load with error.")
}
func TestBadArrayAsSingle(t *testing.T) {
simpleIni := `key=nope
noarray=nope
[section]
key[]=one
key[]=two
noarray=three`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Configuration should load without error.")
assert.Equal(t, config.StringFromSection("section", "key", "default"), "default", "Read default value of strings wrong")
}
func TestBadFile(t *testing.T) {
const filepath = "/no.such.dir/xxx.no-such-file.txt"
config, err := LoadConfiguration(filepath)
assert.NotNil(t, err, "No valid file is an error.")
assert.True(t, os.IsNotExist(err), "No valid file errors is of expected type.")
assert.Nil(t, config, "Configuration should be nil.")
}
func TestNullValuesInGet(t *testing.T) {
assert.Nil(t, get(nil, "foo"), "Configuration should be nil.")
}
func TestStringEscape(t *testing.T) {
simpleIni := `first=\n\t\rhello`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Simple configuration should load without error.")
assert.Equal(t, config.String("first", ""), "\n\t\rhello", "Read value of first wrong")
assert.Equal(t, len(config.Keys()), 1, "ini contains 1 fields")
}
func TestBadStringArray(t *testing.T) {
simpleIni := `first[]=\`
config, err := LoadConfigurationFromReader(strings.NewReader(simpleIni))
assert.Nil(t, err, "Simple configuration should load without error.")
assert.Nil(t, config.Strings("first"), "Read value of first wrong")
}
func BenchmarkLoadConfiguration(b *testing.B) {
simpleIni := `first=alpha
int=32
float=3.14
[section_one]
first=raz
[section_two]
first=one
int=555
float=124.3
[section_one]
int=124
float=1222.7`
b.ReportAllocs()
r := strings.NewReader(simpleIni)
for i := 0; i < b.N; i++ {
r.Seek(0, os.SEEK_SET)
_, err := LoadConfigurationFromReader(r)
if err != nil {
b.Fatal(err)
}
}
}