-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathconfig_test.go
More file actions
1009 lines (830 loc) · 32 KB
/
Copy pathconfig_test.go
File metadata and controls
1009 lines (830 loc) · 32 KB
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package hocon
import (
"errors"
"fmt"
"testing"
"time"
)
func TestGetRoot(t *testing.T) {
root := Object{"a": Object{"b": String("c")}, "d": Array{}}
config := &Config{root}
t.Run("get root value", func(t *testing.T) {
got := config.GetRoot()
assertDeepEqual(t, got, root)
})
}
func TestGetObject(t *testing.T) {
config := &Config{Object{"a": Object{"b": String("c")}, "d": Array{}}}
t.Run("get object", func(t *testing.T) {
got := config.GetObject("a")
assertDeepEqual(t, got, Object{"b": String("c")})
})
t.Run("return nil for a non-existing object", func(t *testing.T) {
got := config.GetObject("e")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
})
t.Run("panic if non-object type is requested as Object", func(t *testing.T) {
assertPanic(t, func() { config.GetObject("d") })
})
}
func TestGetConfig(t *testing.T) {
nestedConfig := &Config{Object{"b": String("c"), "d": Array{}}}
config := &Config{Object{"a": nestedConfig.root}}
t.Run("get nested config", func(t *testing.T) {
got := config.GetConfig("a")
assertDeepEqual(t, got, nestedConfig)
})
t.Run("return nil for non existing config", func(t *testing.T) {
got := config.GetConfig("b")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
})
}
func TestGetStringMap(t *testing.T) {
config := &Config{Object{"a": Object{"b": Int(1)}}}
got := config.GetStringMap("a")
assertDeepEqual(t, got, map[string]Value{"b": Int(1)})
}
func TestGetStringMapString(t *testing.T) {
config := &Config{Object{"a": Object{"b": String("c"), "e": Int(1)}, "d": Array{}}}
t.Run("get object as map[string]string", func(t *testing.T) {
got := config.GetStringMapString("a")
assertDeepEqual(t, got, map[string]string{"b": "c", "e": "1"})
})
t.Run("return nil for a non-existing string map", func(t *testing.T) {
got := config.GetStringMapString("f")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
})
t.Run("return the raw string values without adding quotes", func(t *testing.T) {
conf, err := ParseString(`a = { b = "x y", c = 1 }`)
assertNoError(t, err)
assertDeepEqual(t, conf.GetStringMapString("a"), map[string]string{"b": "x y", "c": "1"})
})
}
func TestGetArray(t *testing.T) {
config := &Config{Object{"a": Array{Int(1), Int(2)}, "b": Object{"c": String("d")}}}
t.Run("get array", func(t *testing.T) {
got := config.GetArray("a")
assertDeepEqual(t, got, Array{Int(1), Int(2)})
})
t.Run("return nil for a non-existing array", func(t *testing.T) {
got := config.GetArray("e")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
})
t.Run("panic if non-array type is requested as Array", func(t *testing.T) {
assertPanic(t, func() { config.GetArray("b") })
})
}
func TestGetIntSlice(t *testing.T) {
config := &Config{Object{"a": Array{Int(1), Int(2)}, "b": Array{String("c"), Int(1)}}}
t.Run("get array as int slice", func(t *testing.T) {
got := config.GetIntSlice("a")
assertDeepEqual(t, got, []int{1, 2})
})
t.Run("return nil for a non-existing int slice", func(t *testing.T) {
got := config.GetIntSlice("e")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
})
t.Run("panic if there is a non-int element in the requested array", func(t *testing.T) {
assertPanic(t, func() { config.GetIntSlice("b") })
})
}
func TestGetStringSlice(t *testing.T) {
config := &Config{Object{"a": Array{String("a"), String("b")}, "b": Array{Int(1), String("c")}}}
t.Run("get array as string slice", func(t *testing.T) {
got := config.GetStringSlice("a")
assertDeepEqual(t, got, []string{"a", "b"})
})
t.Run("return nil for a non-existing string slice", func(t *testing.T) {
got := config.GetStringSlice("e")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
})
t.Run("use string representations of non-string elements and return string slice", func(t *testing.T) {
got := config.GetStringSlice("b")
assertDeepEqual(t, got, []string{"1", "c"})
})
t.Run("return the raw strings without adding quotes", func(t *testing.T) {
conf, err := ParseString(`a = ["x y", "http://z"]`)
assertNoError(t, err)
assertDeepEqual(t, conf.GetStringSlice("a"), []string{"x y", "http://z"})
})
}
func TestGetString(t *testing.T) {
config := &Config{Object{"a": String("b"), "c": Int(2)}}
t.Run("get string", func(t *testing.T) {
assertEquals(t, config.GetString("a"), "b")
})
t.Run("return zero value(empty string) for a non-existing string", func(t *testing.T) {
assertEquals(t, config.GetString("d"), "")
})
t.Run("convert to string and return the value if it is not a string", func(t *testing.T) {
assertEquals(t, config.GetString("c"), "2")
})
t.Run("return the quoted strings without adding extra quotes", func(t *testing.T) {
conf, err := ParseString(`url = "https://example.com/path?q=1"`)
assertNoError(t, err)
assertEquals(t, conf.GetString("url"), "https://example.com/path?q=1")
})
t.Run("return the unquoted strings containing special characters without adding quotes", func(t *testing.T) {
conf, err := ParseString("name = foo-bar")
assertNoError(t, err)
assertEquals(t, conf.GetString("name"), "foo-bar")
})
t.Run("keep the hocon quoting in the rendered configuration while returning the raw string", func(t *testing.T) {
conf, err := ParseString(`greeting = "hello world"`)
assertNoError(t, err)
assertEquals(t, conf.GetString("greeting"), "hello world")
assertEquals(t, conf.String(), `{greeting:"hello world"}`)
})
t.Run("return the flattened string of a string value concatenation", func(t *testing.T) {
conf, err := ParseString(`greeting = "hello" "world"`)
assertNoError(t, err)
assertEquals(t, conf.GetString("greeting"), "hello world")
})
t.Run("return the flattened string of a concatenation with substitutions", func(t *testing.T) {
conf, err := ParseString("host = localhost\nurl = \"https://\"${host}\"/api\"")
assertNoError(t, err)
assertEquals(t, conf.GetString("url"), "https://localhost/api")
})
}
func TestGetInt(t *testing.T) {
config := &Config{Object{"a": String("aa"), "b": String("3"), "c": Int(2), "d": Array{Int(5)}}}
t.Run("get int", func(t *testing.T) {
assertEquals(t, config.GetInt("c"), 2)
})
t.Run("return zero for a non-existing int", func(t *testing.T) {
assertEquals(t, config.GetInt("e"), 0)
})
t.Run("convert to int and return if the value is a string that can be converted to int", func(t *testing.T) {
assertEquals(t, config.GetInt("b"), 3)
})
t.Run("panic if the value is a string that can not be converted to int", func(t *testing.T) {
assertPanic(t, func() { config.GetInt("a") })
})
t.Run("panic if the value is not an int or a string", func(t *testing.T) {
assertPanic(t, func() { config.GetInt("d") })
})
}
func TestGetFloat32(t *testing.T) {
config := &Config{Object{"a": String("aa"), "b": String("3.2"), "c": Float32(2.4), "d": Array{Int(5)}, "e": Float64(2.5)}}
t.Run("get float32", func(t *testing.T) {
assertEquals(t, config.GetFloat32("c"), float32(2.4))
})
t.Run("convert to float32 and return if the value is float64", func(t *testing.T) {
assertEquals(t, config.GetFloat32("e"), float32(2.5))
})
t.Run("return float32(0.0) for a non-existing float32", func(t *testing.T) {
assertEquals(t, config.GetFloat32("z"), float32(0.0))
})
t.Run("convert to float32 and return if the value is a string that can be converted to float32", func(t *testing.T) {
assertEquals(t, config.GetFloat32("b"), float32(3.2))
})
t.Run("panic if the value is a string that can not be converted to float32", func(t *testing.T) {
assertPanic(t, func() { config.GetFloat32("a") })
})
t.Run("panic if the value is not a float32 or a string", func(t *testing.T) {
assertPanic(t, func() { config.GetFloat32("d") })
})
}
func TestGetFloat64(t *testing.T) {
config := &Config{Object{"a": String("aa"), "b": String("3.2"), "c": Float32(2.4), "d": Array{Int(5)}, "e": Float64(2.5)}}
t.Run("get float64", func(t *testing.T) {
assertEquals(t, config.GetFloat64("e"), 2.5)
})
t.Run("convert to float64 and return if the value is float32", func(t *testing.T) {
assertEquals(t, config.GetFloat64("c"), float64(float32(2.4)))
})
t.Run("return float64(0.0) for a non-existing float64", func(t *testing.T) {
assertEquals(t, config.GetFloat64("z"), 0.0)
})
t.Run("convert to float64 and return if the value is a string that can be converted to float64", func(t *testing.T) {
assertEquals(t, config.GetFloat64("b"), 3.2)
})
t.Run("panic if the value is a string that can not be converted to float64", func(t *testing.T) {
assertPanic(t, func() { config.GetFloat64("a") })
})
t.Run("panic if the value is not a float64 or a string", func(t *testing.T) {
assertPanic(t, func() { config.GetFloat64("d") })
})
}
func TestGetBoolean(t *testing.T) {
config := &Config{Object{
"a": Boolean(true),
"b": Boolean(false),
"c": String("true"),
"d": String("yes"),
"e": String("on"),
"f": String("false"),
"g": String("no"),
"h": String("off"),
"i": String("aa"),
"j": Array{Int(5)},
}}
t.Run("return zero value(false) for a non-existing boolean", func(t *testing.T) {
assertEquals(t, config.GetBoolean("z"), false)
})
t.Run("panic if the value is a string that can not be converted to boolean", func(t *testing.T) {
assertPanic(t, func() { config.GetBoolean("i") })
})
t.Run("panic if the value is not a boolean or string", func(t *testing.T) {
assertPanic(t, func() { config.GetBoolean("j") })
})
var booleanTestCases = []struct {
path string
expected bool
}{
{"a", true},
{"b", false},
{"c", true},
{"d", true},
{"e", true},
{"f", false},
{"g", false},
{"h", false},
}
for _, tc := range booleanTestCases {
t.Run(tc.path, func(t *testing.T) {
assertEquals(t, config.GetBoolean(tc.path), tc.expected)
})
}
}
func TestGetDuration(t *testing.T) {
config := &Config{Object{"a": Duration(5 * time.Second), "b": String("bb")}}
t.Run("get Duration at the given path", func(t *testing.T) {
got := config.GetDuration("a")
assertEquals(t, got.String(), Duration(5*time.Second).String())
})
t.Run("return zero for non-existing duration", func(t *testing.T) {
got := config.GetDuration("c")
assertEquals(t, got.String(), Duration(0).String())
})
t.Run("panic if the value is not a duration", func(t *testing.T) {
assertPanic(t, func() { config.GetDuration("b") })
})
}
func TestHasPath(t *testing.T) {
config := &Config{Object{"a": Int(1), "b": Object{"c": String("d")}}}
t.Run("return true if a value exists at the given path", func(t *testing.T) {
assertEquals(t, config.HasPath("b.c"), true)
})
t.Run("return false if no value exists at the given path", func(t *testing.T) {
assertEquals(t, config.HasPath("b.e"), false)
})
t.Run("return false if the path traverses through a non-object value", func(t *testing.T) {
assertEquals(t, config.HasPath("a.b"), false)
})
}
func TestGetObjectE(t *testing.T) {
config := &Config{Object{"a": Object{"b": Int(1)}, "c": Int(2)}}
t.Run("get object", func(t *testing.T) {
got, err := config.GetObjectE("a")
assertNoError(t, err)
assertDeepEqual(t, got, Object{"b": Int(1)})
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetObjectE("d")
assertPathNotFoundError(t, err)
assertNil(t, got)
})
t.Run("return an error if the value is not an object", func(t *testing.T) {
got, err := config.GetObjectE("c")
assertError(t, err, errors.New("cannot parse value: 2 to Object!"))
assertNil(t, got)
})
}
func TestGetConfigE(t *testing.T) {
config := &Config{Object{"a": Object{"b": Int(1)}, "c": Int(2)}}
t.Run("get config", func(t *testing.T) {
got, err := config.GetConfigE("a")
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"b": Int(1)}})
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetConfigE("d")
assertPathNotFoundError(t, err)
assertNil(t, got)
})
t.Run("return an error if the value is not an object", func(t *testing.T) {
got, err := config.GetConfigE("c")
assertError(t, err, errors.New("cannot parse value: 2 to Object!"))
assertNil(t, got)
})
}
func TestGetStringMapE(t *testing.T) {
config := &Config{Object{"a": Object{"b": Int(1)}, "c": Int(2)}}
t.Run("get object as map[string]Value", func(t *testing.T) {
got, err := config.GetStringMapE("a")
assertNoError(t, err)
assertDeepEqual(t, got, map[string]Value{"b": Int(1)})
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetStringMapE("d")
assertPathNotFoundError(t, err)
assertNil(t, got)
})
t.Run("return an error if the value is not an object", func(t *testing.T) {
got, err := config.GetStringMapE("c")
assertError(t, err, errors.New("cannot parse value: 2 to Object!"))
assertNil(t, got)
})
}
func TestGetStringMapStringE(t *testing.T) {
config := &Config{Object{"a": Object{"b": String("x y"), "e": Int(1)}, "c": Int(2)}}
t.Run("get object as map[string]string with the raw string values", func(t *testing.T) {
got, err := config.GetStringMapStringE("a")
assertNoError(t, err)
assertDeepEqual(t, got, map[string]string{"b": "x y", "e": "1"})
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetStringMapStringE("d")
assertPathNotFoundError(t, err)
assertNil(t, got)
})
t.Run("return an error if the value is not an object", func(t *testing.T) {
got, err := config.GetStringMapStringE("c")
assertError(t, err, errors.New("cannot parse value: 2 to Object!"))
assertNil(t, got)
})
}
func TestGetArrayE(t *testing.T) {
config := &Config{Object{"a": Array{Int(1), Int(2)}, "b": Int(3)}}
t.Run("get array", func(t *testing.T) {
got, err := config.GetArrayE("a")
assertNoError(t, err)
assertDeepEqual(t, got, Array{Int(1), Int(2)})
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetArrayE("c")
assertPathNotFoundError(t, err)
assertNil(t, got)
})
t.Run("return an error if the value is not an array", func(t *testing.T) {
got, err := config.GetArrayE("b")
assertError(t, err, errors.New("cannot parse value: 3 to Array!"))
assertNil(t, got)
})
}
func TestGetIntSliceE(t *testing.T) {
config := &Config{Object{"a": Array{Int(1), Int(2)}, "b": Array{String("c"), Int(1)}}}
t.Run("get array as int slice", func(t *testing.T) {
got, err := config.GetIntSliceE("a")
assertNoError(t, err)
assertDeepEqual(t, got, []int{1, 2})
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetIntSliceE("e")
assertPathNotFoundError(t, err)
assertNil(t, got)
})
t.Run("return an error if the array contains a non-int element", func(t *testing.T) {
got, err := config.GetIntSliceE("b")
assertError(t, err, errors.New("cannot parse value: c to int!"))
assertNil(t, got)
})
}
func TestGetStringSliceE(t *testing.T) {
config := &Config{Object{"a": Array{String("x y"), Int(1)}, "b": Int(2)}}
t.Run("get array as string slice with the raw string values", func(t *testing.T) {
got, err := config.GetStringSliceE("a")
assertNoError(t, err)
assertDeepEqual(t, got, []string{"x y", "1"})
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetStringSliceE("c")
assertPathNotFoundError(t, err)
assertNil(t, got)
})
t.Run("return an error if the value is not an array", func(t *testing.T) {
got, err := config.GetStringSliceE("b")
assertError(t, err, errors.New("cannot parse value: 2 to Array!"))
assertNil(t, got)
})
}
func TestGetStringE(t *testing.T) {
config := &Config{Object{"a": String("b"), "c": Int(2)}}
t.Run("get string", func(t *testing.T) {
got, err := config.GetStringE("a")
assertNoError(t, err)
assertEquals(t, got, "b")
})
t.Run("convert to string and return the value if it is not a string", func(t *testing.T) {
got, err := config.GetStringE("c")
assertNoError(t, err)
assertEquals(t, got, "2")
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetStringE("d")
assertPathNotFoundError(t, err)
assertEquals(t, got, "")
})
}
func TestGetIntE(t *testing.T) {
config := &Config{Object{"a": Int(1), "b": String("2"), "c": String("xyz"), "d": Boolean(true)}}
t.Run("get int", func(t *testing.T) {
got, err := config.GetIntE("a")
assertNoError(t, err)
assertEquals(t, got, 1)
})
t.Run("convert the numeric string value to int", func(t *testing.T) {
got, err := config.GetIntE("b")
assertNoError(t, err)
assertEquals(t, got, 2)
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetIntE("e")
assertPathNotFoundError(t, err)
assertEquals(t, got, 0)
})
t.Run("return an error if the value is a non-numeric string", func(t *testing.T) {
got, err := config.GetIntE("c")
assertError(t, err, errors.New(`strconv.Atoi: parsing "xyz": invalid syntax`))
assertEquals(t, got, 0)
})
t.Run("return an error if the value cannot be converted to int", func(t *testing.T) {
got, err := config.GetIntE("d")
assertError(t, err, errors.New("cannot parse value: true to int!"))
assertEquals(t, got, 0)
})
}
func TestGetFloat32E(t *testing.T) {
config := &Config{Object{"a": Float32(1.5), "b": Float64(2.5), "c": String("3.5"), "d": Boolean(true)}}
t.Run("get float32", func(t *testing.T) {
got, err := config.GetFloat32E("a")
assertNoError(t, err)
assertEquals(t, got, float32(1.5))
})
t.Run("convert the float64 value to float32", func(t *testing.T) {
got, err := config.GetFloat32E("b")
assertNoError(t, err)
assertEquals(t, got, float32(2.5))
})
t.Run("convert the numeric string value to float32", func(t *testing.T) {
got, err := config.GetFloat32E("c")
assertNoError(t, err)
assertEquals(t, got, float32(3.5))
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetFloat32E("e")
assertPathNotFoundError(t, err)
assertEquals(t, got, float32(0))
})
t.Run("return an error if the value cannot be converted to float32", func(t *testing.T) {
got, err := config.GetFloat32E("d")
assertError(t, err, errors.New("cannot parse value: true to float32!"))
assertEquals(t, got, float32(0))
})
}
func TestGetFloat64E(t *testing.T) {
config := &Config{Object{"a": Float64(1.5), "b": Float32(2.5), "c": String("3.5"), "d": Boolean(true)}}
t.Run("get float64", func(t *testing.T) {
got, err := config.GetFloat64E("a")
assertNoError(t, err)
assertEquals(t, got, 1.5)
})
t.Run("convert the float32 value to float64", func(t *testing.T) {
got, err := config.GetFloat64E("b")
assertNoError(t, err)
assertEquals(t, got, 2.5)
})
t.Run("convert the numeric string value to float64", func(t *testing.T) {
got, err := config.GetFloat64E("c")
assertNoError(t, err)
assertEquals(t, got, 3.5)
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetFloat64E("e")
assertPathNotFoundError(t, err)
assertEquals(t, got, 0.0)
})
t.Run("return an error if the value cannot be converted to float64", func(t *testing.T) {
got, err := config.GetFloat64E("d")
assertError(t, err, errors.New("cannot parse value: true to float64!"))
assertEquals(t, got, 0.0)
})
}
func TestGetBooleanE(t *testing.T) {
config := &Config{Object{"a": Boolean(true), "b": String("yes"), "c": String("off"), "d": String("xyz"), "e": Int(1)}}
t.Run("get boolean", func(t *testing.T) {
got, err := config.GetBooleanE("a")
assertNoError(t, err)
assertEquals(t, got, true)
})
t.Run("convert the truthy string value to boolean", func(t *testing.T) {
got, err := config.GetBooleanE("b")
assertNoError(t, err)
assertEquals(t, got, true)
})
t.Run("convert the falsy string value to boolean", func(t *testing.T) {
got, err := config.GetBooleanE("c")
assertNoError(t, err)
assertEquals(t, got, false)
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetBooleanE("f")
assertPathNotFoundError(t, err)
assertEquals(t, got, false)
})
t.Run("return an error if the value is a non-boolean string", func(t *testing.T) {
got, err := config.GetBooleanE("d")
assertError(t, err, errors.New("cannot parse value: xyz to boolean!"))
assertEquals(t, got, false)
})
t.Run("return an error if the value cannot be converted to boolean", func(t *testing.T) {
got, err := config.GetBooleanE("e")
assertError(t, err, errors.New("cannot parse value: 1 to boolean!"))
assertEquals(t, got, false)
})
}
func TestGetDurationE(t *testing.T) {
config := &Config{Object{"a": Duration(5 * time.Second), "b": Int(1)}}
t.Run("get duration", func(t *testing.T) {
got, err := config.GetDurationE("a")
assertNoError(t, err)
assertEquals(t, got, 5*time.Second)
})
t.Run("return ErrPathNotFound for a non-existing path", func(t *testing.T) {
got, err := config.GetDurationE("c")
assertPathNotFoundError(t, err)
assertEquals(t, got, time.Duration(0))
})
t.Run("return an error if the value is not a duration", func(t *testing.T) {
got, err := config.GetDurationE("b")
assertError(t, err, errors.New("cannot parse value: 1 to Duration!"))
assertEquals(t, got, time.Duration(0))
})
}
func TestWithFallback(t *testing.T) {
config1 := &Config{Object{"a": String("aa"), "b": String("bb")}}
config2 := &Config{Object{"a": String("aaa"), "c": String("cc")}}
config3 := &Config{Array{Int(1), Int(2)}}
config4 := &Config{Object{"a": String("aa")}}
config5 := &Config{Object{"a": nil}}
config6 := &Config{Object{"a": Object{"a": String("aa"), "b": String("bb")}}}
t.Run("merge the given fallback config with the current config if the root of both of them are of type Object (for the same keys current config should override the fallback)", func(t *testing.T) {
expected := &Config{Object{"a": String("aa"), "b": String("bb"), "c": String("cc")}}
got := config1.WithFallback(config2)
assertDeepEqual(t, got, expected)
})
t.Run("return the current config if the root of the given fallback config is not an Object", func(t *testing.T) {
got := config1.WithFallback(config3)
assertDeepEqual(t, got, config1)
})
t.Run("return the current config if the root of it is not an Object", func(t *testing.T) {
got := config3.WithFallback(config1)
assertDeepEqual(t, got, config3)
})
t.Run("return the current value if new value is nil", func(t *testing.T) {
got := config4.WithFallback(config5)
assertDeepEqual(t, got, config4)
})
t.Run("rewrite the current value if old value is nil and new value is not nil", func(t *testing.T) {
got := config5.WithFallback(config4)
assertDeepEqual(t, got, config4)
})
t.Run("rewrite the current value if old value is nil and new value is not nil and Object", func(t *testing.T) {
got := config5.WithFallback(config6)
assertDeepEqual(t, got, config6)
})
}
func TestResolve(t *testing.T) {
t.Run("resolve the substitutions using the values of the fallback config", func(t *testing.T) {
mainConfig, err := ParseStringUnresolved("namespace { key: ${namespace.other} }")
assertNoError(t, err)
fallbackConfig, err := ParseString("namespace { other: value }")
assertNoError(t, err)
got, err := mainConfig.WithFallback(fallbackConfig).Resolve()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"namespace": Object{"key": String("value"), "other": String("value")}}})
})
t.Run("return an error if a required substitution cannot be resolved", func(t *testing.T) {
config, err := ParseStringUnresolved("a = ${nonExisting}")
assertNoError(t, err)
got, err := config.Resolve()
assertError(t, err, errors.New("could not resolve substitution: ${nonExisting} to a value"))
assertNil(t, got)
})
t.Run("omit the fields with unresolved optional substitutions", func(t *testing.T) {
config, err := ParseStringUnresolved("a = 1\nb = ${?nonExisting}")
assertNoError(t, err)
got, err := config.Resolve()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"a": Int(1)}})
})
t.Run("flatten the string value concatenations", func(t *testing.T) {
config, err := ParseStringUnresolved("greeting = hello world")
assertNoError(t, err)
got, err := config.Resolve()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"greeting": String("hello world")}})
})
t.Run("keep an already resolved config unchanged", func(t *testing.T) {
config, err := ParseString("a = 1\nb = ${a}")
assertNoError(t, err)
got, err := config.Resolve()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"a": Int(1), "b": Int(1)}})
})
t.Run("return the config as it is if the root is not an object", func(t *testing.T) {
config, err := ParseString("[1, 2]")
assertNoError(t, err)
got, err := config.Resolve()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Array{Int(1), Int(2)}})
})
t.Run("not modify the original configs when the merged config is resolved", func(t *testing.T) {
mainConfig, err := ParseStringUnresolved("arr = [${x}]")
assertNoError(t, err)
fallbackConfig, err := ParseString("x = 5")
assertNoError(t, err)
resolved, err := mainConfig.WithFallback(fallbackConfig).Resolve()
assertNoError(t, err)
assertDeepEqual(t, resolved, &Config{Object{"x": Int(5), "arr": Array{Int(5)}}})
assertDeepEqual(t, mainConfig, &Config{Object{"arr": Array{&Substitution{path: "x", optional: false}}}})
otherFallback, err := ParseString("x = 7")
assertNoError(t, err)
resolvedAgain, err := mainConfig.WithFallback(otherFallback).Resolve()
assertNoError(t, err)
assertDeepEqual(t, resolvedAgain, &Config{Object{"x": Int(7), "arr": Array{Int(7)}}})
})
}
func TestFind(t *testing.T) {
t.Run("return nil if path does not contain any dot and there is no value with the given path", func(t *testing.T) {
object := Object{"a": Int(1)}
got := object.find("b")
assertNil(t, got)
})
t.Run("find the value with the path that does not contain any dot", func(t *testing.T) {
object := Object{"a": Int(1)}
got := object.find("a")
assertEquals(t, got, Int(1))
})
t.Run("return nil if path contains dot and there is no value with the sub-path", func(t *testing.T) {
object := Object{"a": Object{"b": Int(1)}}
got := object.find("c.b")
assertNil(t, got)
})
t.Run("find the value with the path that contains dots", func(t *testing.T) {
object := Object{"a": Object{"b": Int(1)}}
got := object.find("a.b")
assertEquals(t, got, Int(1))
})
t.Run("return nil if the path traverses through a non-object value", func(t *testing.T) {
object := Object{"a": Int(1)}
got := object.find("a.b")
assertNil(t, got)
})
}
func TestObject_String(t *testing.T) {
t.Run("return the string of an empty object", func(t *testing.T) {
got := Object{}.String()
assertEquals(t, got, "{}")
})
t.Run("return the string of an object that contains a empty string", func(t *testing.T) {
got := Object{"a": String("")}.String()
assertEquals(t, got, "{a:\"\"}")
})
t.Run("return the string of an object that contains a single element", func(t *testing.T) {
got := Object{"a": Int(1)}.String()
assertEquals(t, got, "{a:1}")
})
t.Run("return the string of an object that contains multiple elements", func(t *testing.T) {
got := Object{"a": Int(1), "b": Int(2)}.String()
if got != "{a:1, b:2}" && got != "{b:2, a:1}" {
fail(t, got, "{a:1, b:2}")
}
})
t.Run("return the string of an object that contains a single element with the forbidden characters", func(t *testing.T) {
got := Object{"a": String("!@#$%^&*()_+{}[];:',./<>?\"\\")}.String()
assertEquals(t, got, "{a:\"!@#$%^&*()_+{}[];:',./<>?\"\\\"}")
})
t.Run("return the string of an object that contains multiple elements with the forbidden characters", func(t *testing.T) {
got := Object{"a": String("!@#$%^&*()_+{}[];:',./<>?\"\\"), "b": Int(2)}.String()
if got != "{a:\"!@#$%^&*()_+{}[];:',./<>?\"\\\", b:2}" && got != "{b:2, a:\"!@#$%^&*()_+{}[];:',./<>?\"\\\"}" {
fail(t, got, "{a:\"!@#$%^&*()_+{}[];:',./<>?\"\\\", b:2}")
}
})
}
func TestArray_String(t *testing.T) {
t.Run("return the string of an empty array", func(t *testing.T) {
got := Array{}.String()
assertEquals(t, got, "[]")
})
t.Run("return the string of an object that contains a empty string", func(t *testing.T) {
got := Array{String("")}.String()
assertEquals(t, got, "[\"\"]")
})
t.Run("return the string of an array that contains a single element", func(t *testing.T) {
got := Array{Int(1)}.String()
assertEquals(t, got, "[1]")
})
t.Run("return the string of an array that contains multiple elements", func(t *testing.T) {
got := Array{Int(1), Int(2)}.String()
assertEquals(t, got, "[1,2]")
})
t.Run("return the string of an array that contains a single elements with the ':' character", func(t *testing.T) {
got := Array{String("!@#$%^&*()_+{}[];:',./<>?\"\\")}.String()
assertEquals(t, got, "[\"!@#$%^&*()_+{}[];:',./<>?\"\\\"]")
})
t.Run("return the string of an array that contains multiple elements with the ':' character", func(t *testing.T) {
got := Array{String("!@#$%^&*()_+"), String("{}[]|;':\",./<>?\\")}.String()
assertEquals(t, got, "[\"!@#$%^&*()_+\",\"{}[]|;':\",./<>?\\\"]")
})
}
func TestGet(t *testing.T) {
t.Run("return nil if the root of config is not an Object", func(t *testing.T) {
config := &Config{Array{Int(1)}}
got := config.Get("a")
assertNil(t, got)
})
t.Run("find the value if the root of config is an object and a value exist with the given path", func(t *testing.T) {
config := &Config{Object{"a": Int(1)}}
got := config.Get("a")
assertEquals(t, got, Int(1))
})
t.Run("return nil if the root of config is an object but value with the given path does not exist", func(t *testing.T) {
config := &Config{Object{"a": Int(1)}}
got := config.Get("b")
assertNil(t, got)
})
}
func TestNewBooleanFromString(t *testing.T) {
var testCases = []struct {
input string
expected Boolean
}{
{"true", Boolean(true)},
{"yes", Boolean(true)},
{"on", Boolean(true)},
{"false", Boolean(false)},
{"no", Boolean(false)},
{"off", Boolean(false)},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("create the Boolean(%s) from the input string: %s", tc.expected, tc.input), func(t *testing.T) {
got := newBooleanFromString(tc.input)
assertEquals(t, got, tc.expected)
})
}
t.Run("panic if the given string is not a boolean string", func(t *testing.T) {
nonBooleanString := "nonBooleanString"
assertPanic(t, func() { newBooleanFromString(nonBooleanString) }, fmt.Sprintf("cannot parse value: %s to Boolean!", nonBooleanString))
})
}
func TestSubstitution_String(t *testing.T) {
t.Run("return the string of required substitution", func(t *testing.T) {
substitution := &Substitution{path: "a", optional: false}
got := substitution.String()
assertEquals(t, got, "${a}")
})
t.Run("return the string of optional substitution", func(t *testing.T) {
substitution := &Substitution{path: "a", optional: true}
got := substitution.String()
assertEquals(t, got, "${?a}")
})
}
func TestValueWithAlternative_String(t *testing.T) {
t.Run("return the string of valueWithAlternative", func(t *testing.T) {
substitution := Substitution{path: "a", optional: false}
withAlt := valueWithAlternative{value: String("value"), alternative: &substitution}
got := withAlt.String()
assertEquals(t, got, "(value | ${a})")
})
}
func TestToConfig(t *testing.T) {
object := Object{"a": Int(1)}
got := object.ToConfig()
assertDeepEqual(t, got.root, object)
}
func TestContainsObject(t *testing.T) {
t.Run("return false if the concatenation does not contain an Object", func(t *testing.T) {
concatenation := concatenation{String("a"), String("b")}
got := concatenation.containsObject()
assertEquals(t, got, false)
})
t.Run("return true if the concatenation contains an Object", func(t *testing.T) {
concatenation := concatenation{Object{"a": String("aa")}, String("b")}
got := concatenation.containsObject()
assertEquals(t, got, true)
})
t.Run("return true if the concatenation contains a nil element and an Object", func(t *testing.T) {
concatenation := concatenation{nil, Object{"a": String("aa")}}
got := concatenation.containsObject()