-
Notifications
You must be signed in to change notification settings - Fork 17
/
parser_test.go
1709 lines (1521 loc) · 62.9 KB
/
parser_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
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"
"os"
"strings"
"testing"
"time"
)
func TestParseString(t *testing.T) {
t.Run("parse the string and return a pointer to the Config", func(t *testing.T) {
got, err := ParseString("{a:1}")
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"a": Int(1)}})
})
t.Run("return the error if any error occurs in the parse() method", func(t *testing.T) {
got, err := ParseString("{.a:1}")
assertError(t, err, leadingPeriodError(1, 2))
assertNil(t, got)
})
}
func TestParseResource(t *testing.T) {
t.Run("return error if there is an error in the os.Open(path) method", func(t *testing.T) {
got, err := ParseResource("nonExistPath")
expectedError := fmt.Errorf("could not parse resource: open nonExistPath: no such file or directory")
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("parse and return a pointer to the config if there is no error", func(t *testing.T) {
got, err := ParseResource("testdata/array.conf")
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Array{Int(1), Int(2), Int(3)}})
})
}
func TestParse(t *testing.T) {
t.Run("try to parse as config array if the input starts with '[' and return the error from extractArray if any", func(t *testing.T) {
parser := newParser(strings.NewReader("[5"))
expectedError := invalidArrayError("parenthesis do not match", 1, 2)
got, err := parser.parse()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("parse as config array if the input starts with '['", func(t *testing.T) {
parser := newParser(strings.NewReader("[5]"))
got, err := parser.parse()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Array{Int(5)}})
})
t.Run("return the same error if any error occurs in the extractObject method", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:5"))
expectedError := invalidObjectError("parenthesis do not match", 1, 5)
got, err := parser.parse()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return an invalidObjectError if the EOF is not reached after extractObject method returns", func(t *testing.T) {
parser := newParser(strings.NewReader("a:{b:1}bb"))
expectedError := invalidObjectError("invalid token bb", 1, 8)
got, err := parser.parse()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return the same error if any error occurs in the resolveSubstitution method", func(t *testing.T) {
parser := newParser(strings.NewReader("a:${b}"))
expectedError := fmt.Errorf("could not resolve substitution: ${b} to a value")
got, err := parser.parse()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("parse as object if the input does not start with '['", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:42}"))
got, err := parser.parse()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"a": Int(42)}})
})
// ###############################################################
// ###############################################################
t.Run("parse simple object", func(t *testing.T) {
parser := newParser(strings.NewReader(`{a:"b"}`))
got, err := parser.parse()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"a": String("b")}})
})
t.Run("parse simple array", func(t *testing.T) {
parser := newParser(strings.NewReader(`["a", "b"]`))
got, err := parser.parse()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Array{String("a"), String("b")}})
})
t.Run("parse nested object", func(t *testing.T) {
parser := newParser(strings.NewReader(`{a: {c: "d"}}`))
got, err := parser.parse()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"a": Object{"c": String("d")}}})
})
t.Run("parse with the omitted root braces", func(t *testing.T) {
parser := newParser(strings.NewReader("a=1"))
got, err := parser.parse()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"a": Int(1)}})
})
t.Run("parse the path key", func(t *testing.T) {
parser := newParser(strings.NewReader(`{a.b:"c"}`))
got, err := parser.parse()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"a": Object{"b": String("c")}}})
})
t.Run("parse the path key that contains a hyphen", func(t *testing.T) {
parser := newParser(strings.NewReader(`a.b-1: "c"`))
got, err := parser.parse()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"a": Object{"b-1": String("c")}}})
})
t.Run("parse the nested object with a key containing a hyphen", func(t *testing.T) {
parser := newParser(strings.NewReader(`{a: {b-1: "c"}}`))
got, err := parser.parse()
assertNoError(t, err)
assertDeepEqual(t, got, &Config{Object{"a": Object{"b-1": String("c")}}})
})
}
func TestExtractObject(t *testing.T) {
t.Run("extract empty object", func(t *testing.T) {
parser := newParser(strings.NewReader("{}"))
parser.advance() // move scanner to the first token for the test case
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{})
})
t.Run("extract object with the root braces omitted", func(t *testing.T) {
parser := newParser(strings.NewReader("a=1"))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"a": Int(1)})
})
t.Run("extract simple object", func(t *testing.T) {
parser := newParser(strings.NewReader("{a=1}"))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"a": Int(1)})
})
t.Run("extract nested object", func(t *testing.T) {
parser := newParser(strings.NewReader("{a.b:1,c:2}"))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"a": Object{"b": Int(1)}, "c": Int(2)})
})
t.Run("extract nested object with the value of unquoted string", func(t *testing.T) {
parser := newParser(strings.NewReader("x {a.b:10cc}"))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"x": Object{"a": Object{"b": concatenation{Int(10), String(""), String("cc")}}}})
})
t.Run("skip the comments inside objects", func(t *testing.T) {
config := `{
# this is a comment
# this is also a comment
a: 1
}
`
parser := newParser(strings.NewReader(config))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"a": Int(1)})
})
t.Run("return the error if any error occurs in parseIncludedResource method", func(t *testing.T) {
parser := newParser(strings.NewReader(`{include "testdata/array.conf"}`))
parser.advance()
expectedErr := invalidValueError("included file cannot contain an array as the root value", 1, 10)
got, err := parser.extractObject()
assertError(t, err, expectedErr)
assertNil(t, got)
})
t.Run("merge the included object with the existing", func(t *testing.T) {
parser := newParser(strings.NewReader(`b:2, include "testdata/a.conf"`))
parser.advance()
expected := Object{"a": Int(1), "b": Int(2)}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("merge multiple included objects with the existing", func(t *testing.T) {
parser := newParser(strings.NewReader(
`c:3
include "testdata/a.conf"
include "testdata/b.conf"
`))
parser.advance()
expected := Object{"a": Int(1), "b": Int(2), "c": Int(3)}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("parse a comment between two includes", func(t *testing.T) {
parser := newParser(strings.NewReader(
`include "testdata/a.conf"
# comment
include "testdata/b.conf"
`))
parser.advance()
expected := Object{"a": Int(1), "b": Int(2)}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("parse correctly if the last line is a comment", func(t *testing.T) {
config := `{
a: 1
# this is a comment
}
`
parser := newParser(strings.NewReader(config))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"a": Int(1)})
})
for forbiddenChar := range forbiddenCharacters {
t.Run(fmt.Sprintf("return error if the key contains the forbidden character: %q", forbiddenChar), func(t *testing.T) {
if forbiddenChar != "`" && forbiddenChar != `"` && forbiddenChar != "}" && forbiddenChar != "#" {
parser := newParser(strings.NewReader(fmt.Sprintf("{%s:1}", forbiddenChar)))
parser.advance()
expectedError := invalidKeyError(forbiddenChar, 1, 2)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
}
})
}
t.Run("return a leadingPeriodError if the key starts with a period '.'", func(t *testing.T) {
parser := newParser(strings.NewReader("{.a:1}"))
parser.advance()
expectedError := leadingPeriodError(1, 2)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return a adjacentPeriodsError if the key contains two adjacent periods", func(t *testing.T) {
parser := newParser(strings.NewReader("{a..b:1}"))
parser.advance()
expectedError := adjacentPeriodsError(1, 4)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return a trailingPeriodError if the ends with a period", func(t *testing.T) {
parser := newParser(strings.NewReader("{a.:1}"))
parser.advance()
expectedError := trailingPeriodError(1, 3)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return the error if any error occurs while extracting the sub-object (with object start token after key)", func(t *testing.T) {
parser := newParser(strings.NewReader("{a{.b:1}}"))
parser.advance()
expectedError := leadingPeriodError(1, 4)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return the error if any error occurs while extracting the sub-object (with path expression as key)", func(t *testing.T) {
parser := newParser(strings.NewReader("{a.b.:1}"))
parser.advance()
expectedError := trailingPeriodError(1, 5)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return the error if any error occurs in extractValue method after equals separator", func(t *testing.T) {
parser := newParser(strings.NewReader("{a=&}"))
parser.advance()
expectedError := invalidValueError(fmt.Sprintf("unknown value: %q", "&"), 1, 4)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return merged object if the current value (after equals separator) is object and there is an existing object with the same key", func(t *testing.T) {
parser := newParser(strings.NewReader("{a={b:1},a={c:2}}"))
parser.advance()
expected := Object{"a": Object{"b": Int(1), "c": Int(2)}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("override the existing value if the current value (after equals separator) is object and there is an existing non-object with the same key", func(t *testing.T) {
parser := newParser(strings.NewReader("{a=1,a={c:2}}"))
parser.advance()
expected := Object{"a": Object{"c": Int(2)}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("override the existing value if the current value (after equals separator) is not object", func(t *testing.T) {
parser := newParser(strings.NewReader("{a={b:1},a=2}"))
parser.advance()
expected := Object{"a": Int(2)}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return the error if any error occurs in extractValue method after colon separator", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:&}"))
parser.advance()
expectedError := invalidValueError(fmt.Sprintf("unknown value: %q", "&"), 1, 4)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return merged object if the current value (after colon separator) is object and there is an existing object with the same key", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:{b:1},a:{c:2}}"))
parser.advance()
expected := Object{"a": Object{"b": Int(1), "c": Int(2)}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return object containing a concatenation if the current value (after colon separator) is substitution and there is an existing substitution with the same key", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:1,b:2,c:${a},c:${b}}"))
parser.advance()
expected := Object{
"a": Int(1),
"b": Int(2),
"c": concatenation{&Substitution{path: "a", optional: false}, &Substitution{path: "b", optional: false}},
}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return object containing a concatenation if the current value (after colon separator) is substitution and there is an existing object with the same key", func(t *testing.T) {
parser := newParser(strings.NewReader("{b:2,c:{a:1},c:${b}}"))
parser.advance()
expected := Object{
"b": Int(2),
"c": concatenation{Object{"a": Int(1)}, &Substitution{path: "b", optional: false}},
}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return object containing a concatenation if the current value (after colon separator) is substitution and there is an existing object with the same key", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:1,c:${a},c:{b:2}}"))
parser.advance()
expected := Object{
"a": Int(1),
"c": concatenation{&Substitution{path: "a", optional: false}, Object{"b": Int(2)}},
}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return valueWithAlternative object if the current value (after colon separator) is substitution and the existing value is neither a substitution nor object", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:1,a:${?b}}"))
parser.advance()
expected := Object{
"a": &valueWithAlternative{
value: Int(1),
alternative: &Substitution{path: "b", optional: true},
},
}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("override the existing value if the current value (after colon separator) is object and there is an existing non-object with the same key", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:1,a:{c:2}}"))
parser.advance()
expected := Object{"a": Object{"c": Int(2)}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("override the existing value if the current value (after colon separator) is not object", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:{b:1},a:2}"))
parser.advance()
expected := Object{"a": Int(2)}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return merged object if the current value (without separator) is object and there is an existing object with the same key", func(t *testing.T) {
parser := newParser(strings.NewReader("{a{b:1},a{c:2}}"))
parser.advance()
expected := Object{"a": Object{"b": Int(1), "c": Int(2)}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return overwritten object if a key is repeated three times, and the first occurrence is not an object", func(t *testing.T) {
parser := newParser(strings.NewReader("{a=1,a{b:1},a{c:2}}"))
parser.advance()
expected := Object{"a": Object{"b": Int(1), "c": Int(2)}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return overwritten object if a key is repeated three times, and the second occurrence is not an object", func(t *testing.T) {
parser := newParser(strings.NewReader("{a{b:1},a=1,a{c:2}}"))
parser.advance()
expected := Object{"a": Object{"c": Int(2)}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return overwritten object if a key is repeated three times, and the last occurrence is not an object", func(t *testing.T) {
parser := newParser(strings.NewReader("{a{b:1},a{c:2},a=1}"))
parser.advance()
expected := Object{"a": Int(1)}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return the error if any error occurs in parsePlusEquals method", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:1,a+=2}"))
parser.advance()
expectedError := invalidValueError(fmt.Sprintf("value: %q of the key: %q is not an array", "1", "a"), 1, 10)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("extract object with the += separator", func(t *testing.T) {
parser := newParser(strings.NewReader("{a+=1}"))
parser.advance()
expected := Object{"a": Array{Int(1)}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("return error if '=' does not exist after '+'", func(t *testing.T) {
parser := newParser(strings.NewReader("{a+1}"))
parser.advance()
expectedError := invalidKeyError("+", 1, 3)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("extract only the sub-object and return if the isSubObject is given 'true'", func(t *testing.T) {
parser := newParser(strings.NewReader("{a.b:1,c:2}"))
advanceScanner(t, parser, "b")
got, err := parser.extractObject(true)
assertNoError(t, err)
assertDeepEqual(t, got, Object{"b": Int(1)})
})
t.Run("return the error if any error occurs while concatenating", func(t *testing.T) {
parser := newParser(strings.NewReader("a:b ${"))
parser.advance()
got, err := parser.extractObject()
assertError(t, err, invalidSubstitutionError("missing closing parenthesis", 1, 7))
assertNil(t, got)
})
t.Run("should break the concatenation loop if the checkAndConcatenate method returns false", func(t *testing.T) {
parser := newParser(strings.NewReader("a:[1] bb, c:d"))
parser.advance()
got, err := parser.extractObject()
assertError(t, err, missingCommaError(1, 7))
assertNil(t, got)
})
t.Run("concatenate multiple values if they are concatenable and in the same line", func(t *testing.T) {
parser := newParser(strings.NewReader("a:bb cc dd"))
parser.advance()
expected := Object{"a": concatenation{String("bb"), String(" "), String("cc"), String(" "), String("dd")}}
got, err := parser.extractObject()
assertNoError(t, err)
assertEquals(t, got.String(), expected.String())
})
t.Run("should parse properly if the line ends with a comment", func(t *testing.T) {
parser := newParser(strings.NewReader(`name: value #this is a comment`))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"name": String("value")})
})
t.Run("should parse properly if the comment contains a `'` character (which results golang scanner to append `\n` to the latest token instead of a separate token)", func(t *testing.T) {
config := `
# it's a comment
name: value
`
parser := newParser(strings.NewReader(config))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"name": String("value")})
})
t.Run("return missingCommaError if there is no comma or ASCII newline between the object elements", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:1 b:2}"))
parser.advance()
expectedError := missingCommaError(1, 7)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("skip comma between the object elements", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:1,b:2}"))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"a": Int(1), "b": Int(2)})
})
t.Run("return adjacentCommasError if there are two adjacent commas between the elements of the object", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:1,,b:2}"))
parser.advance()
expectedError := adjacentCommasError(1, 6)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return invalidObjectError if parenthesis do not match", func(t *testing.T) {
parser := newParser(strings.NewReader("{a:1"))
parser.advance()
expectedError := invalidObjectError("parenthesis do not match", 1, 5)
got, err := parser.extractObject()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("extract the object with unquoted string that starts with number and contains an 'e' (which causes Scanner library to recognize it as float)", func(t *testing.T) {
parser := newParser(strings.NewReader("uuid: 123e4567-e89b-12d3-a456-426614174000"))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"uuid": concatenation{String("123e4567"), String(""), String("-e89b-12d3-a456-426614174000")}})
})
t.Run("extract the object that contains an array with substitution and concatenation", func(t *testing.T) {
parser := newParser(strings.NewReader(`{x:a, y:b, arr: [${x}"."${y}]}`))
parser.advance()
got, err := parser.extractObject()
expected := Object{
"x": String("a"),
"y": String("b"),
"arr": Array{concatenation{
&Substitution{path: "x", optional: false},
String(""),
String("."),
String(""),
&Substitution{path: "y", optional: false},
}},
}
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("should parse properly when the path expression key ends with a number", func(t *testing.T) {
parser := newParser(strings.NewReader(`a.100:[1,2]`))
parser.advance()
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"a": Object{"100": Array{Int(1), Int(2)}}})
})
}
func TestMergeObjects(t *testing.T) {
t.Run("merge objects", func(t *testing.T) {
existing := Object{"b": Int(5)}
new := Object{"c": Int(3)}
expected := Object{"b": Int(5), "c": Int(3)}
mergeObjects(existing, new)
assertDeepEqual(t, existing, expected)
})
t.Run("merge objects recursively if both parameters contain the same key as of type Object", func(t *testing.T) {
existing := Object{"b": Object{"e": Int(5)}}
new := Object{"b": Object{"f": Int(7)}, "c": Int(3)}
expected := Object{"b": Object{"e": Int(5), "f": Int(7)}, "c": Int(3)}
mergeObjects(existing, new)
assertDeepEqual(t, existing, expected)
})
t.Run("merge objects recursively, value from the second parameter should override the first one if any of them are not of type Object", func(t *testing.T) {
existing := Object{"b": Object{"e": Int(5)}, "c": Int(3)}
new := Object{"b": Int(7)}
expected := Object{"b": Int(7), "c": Int(3)}
mergeObjects(existing, new)
assertDeepEqual(t, existing, expected)
})
}
func TestResolveSubstitutions(t *testing.T) {
t.Run("resolve valid substitution at the root level", func(t *testing.T) {
object := Object{"a": Int(5), "b": &Substitution{"a", false}}
err := resolveSubstitutions(object)
assertNoError(t, err)
})
t.Run("resolve to the environment variable if substitution path does not exist and an environment variable is set with the substitution path", func(t *testing.T) {
testEnv := "TEST_ENV"
substitution := &Substitution{testEnv, false}
object := Object{"a": Int(5), "b": substitution}
err := os.Setenv(testEnv, "test")
assertNoError(t, err)
err = resolveSubstitutions(object)
assertNoError(t, err)
err = os.Unsetenv(testEnv)
assertNoError(t, err)
})
t.Run("resolve to the environment variable if substitution path does not exist and environment variable is set and default value was provided", func(t *testing.T) {
testEnv := "TEST_ENV"
testEnvValue := "test"
envSubstitution := &Substitution{path: testEnv, optional: false}
staticWithEnv := &valueWithAlternative{value: String("static"), alternative: envSubstitution}
object := Object{"a": staticWithEnv}
err := os.Setenv(testEnv, testEnvValue)
assertNoError(t, err)
expected := String(testEnvValue)
err = resolveSubstitutions(object)
assertNoError(t, err)
err = os.Unsetenv(testEnv)
assertNoError(t, err)
if expected != object["a"] {
t.Errorf("expected value: %s from environment variable: %s, got: %s", expected, testEnv, object["a"])
}
})
t.Run("resolve to the static value if substitution path does not exist and environment variable is not set and default value was not provided", func(t *testing.T) {
defaultValue := String("default")
envSubstitution := &Substitution{path: "TEST_ENV", optional: true}
staticWithEnv := &valueWithAlternative{value: defaultValue, alternative: envSubstitution}
object := Object{"a": staticWithEnv}
err := resolveSubstitutions(object)
assertNoError(t, err)
if defaultValue != object["a"] {
t.Errorf("expected default value: %s, got: %s", defaultValue, object["a"])
}
})
t.Run("resolve transitive substitutions in unordered Object map", func(t *testing.T) {
value := Int(5)
object := Object{
"a": value,
"b": &Substitution{path: "a", optional: false},
"c": &Substitution{path: "b", optional: false},
}
var err error
visitedPaths := make(map[string]bool)
err = processSubstitution(object, object.find("c"), visitedPaths, func(foundValue Value) { object["c"] = foundValue })
assertNoError(t, err)
err = processSubstitution(object, object.find("b"), visitedPaths, func(foundValue Value) { object["b"] = foundValue })
assertNoError(t, err)
if value != object["b"] {
t.Errorf("expected default value: %s, got: %s", value, object["b"])
}
if value != object["c"] {
t.Errorf("expected default value: %s, got: %s", value, object["c"])
}
})
t.Run("return an error if substitution cycle detected", func(t *testing.T) {
object := Object{
"a": &Substitution{path: "b", optional: false},
"b": &Substitution{path: "c", optional: false},
"c": &Substitution{path: "a", optional: false},
}
var err error
visitedPaths := make(map[string]bool)
err = processSubstitution(object, object.find("a"), visitedPaths, func(foundValue Value) { object["c"] = foundValue })
expectedErr := errors.New("detected substitution cycle: ${b}")
assertError(t, err, expectedErr)
})
t.Run("return an error if cannot find required substitution and default value was provided", func(t *testing.T) {
defaultValue := String("default")
envSubstitution := &Substitution{path: "TEST_ENV", optional: false}
staticWithEnv := &valueWithAlternative{value: defaultValue, alternative: envSubstitution}
object := Object{"a": staticWithEnv}
err := resolveSubstitutions(object)
expectedErr := errors.New("could not resolve substitution: ${TEST_ENV} to a value")
assertError(t, err, expectedErr)
})
t.Run("return an error for non-existing substitution path", func(t *testing.T) {
substitution := &Substitution{"c", false}
object := Object{"a": Int(5), "b": substitution}
err := resolveSubstitutions(object)
expectedError := errors.New("could not resolve substitution: " + substitution.String() + " to a value")
assertError(t, err, expectedError)
})
t.Run("ignore the optional substitution if it's path does not exist", func(t *testing.T) {
object := Object{"a": Int(5), "b": &Substitution{"c", true}}
err := resolveSubstitutions(object)
assertNoError(t, err)
})
t.Run("resolve valid substitution at the non-root level", func(t *testing.T) {
subObject := Object{"c": &Substitution{"a", false}}
object := Object{"a": Int(5), "b": subObject}
err := resolveSubstitutions(object, subObject)
assertNoError(t, err)
})
t.Run("return invalid concatenation error if the concatenation contains an object and a different type", func(t *testing.T) {
substitution := &Substitution{"a", false}
object := Object{"a": Int(5), "b": concatenation{Object{"aa": Int(1)}, substitution}}
err := resolveSubstitutions(object)
assertError(t, err, invalidConcatenationError())
})
t.Run("resolve the substitution in concatenation and merge the objects if the concatenation's every element is object", func(t *testing.T) {
substitution := &Substitution{"a", false}
object := Object{"bb": Int(1)}
root := Object{"a": Object{"aa": Int(5)}, "b": concatenation{object, substitution}}
expected := Object{"aa": Int(5), "bb": Int(1)}
err := resolveSubstitutions(root)
got := root.find("b")
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("resolve valid substitution inside an array", func(t *testing.T) {
subArray := Array{&Substitution{"a", false}}
object := Object{"a": Int(5), "b": subArray}
err := resolveSubstitutions(object, subArray)
assertNoError(t, err)
})
t.Run("return error for non-existing substitution path inside an array", func(t *testing.T) {
substitution := &Substitution{"c", false}
subArray := Array{substitution}
object := Object{"a": Int(5), "b": subArray}
err := resolveSubstitutions(object, subArray)
expectedError := errors.New("could not resolve substitution: " + substitution.String() + " to a value")
assertError(t, err, expectedError)
})
t.Run("ignore the optional substitution inside an array if it's path does not exist", func(t *testing.T) {
subArray := Array{&Substitution{"a", true}}
object := Object{"a": Int(5), "b": subArray}
err := resolveSubstitutions(object, subArray)
assertNoError(t, err)
})
t.Run("resolve valid substitution inside a concatenation", func(t *testing.T) {
concatenation := concatenation{&Substitution{"a", false}}
object := Object{"a": Int(5), "b": concatenation}
err := resolveSubstitutions(object, concatenation)
assertNoError(t, err)
})
t.Run("return error for non-existing substitution path inside an concatenation", func(t *testing.T) {
substitution := &Substitution{"c", false}
concatenation := concatenation{substitution}
object := Object{"a": Int(5), "b": concatenation}
err := resolveSubstitutions(object, concatenation)
expectedError := errors.New("could not resolve substitution: " + substitution.String() + " to a value")
assertError(t, err, expectedError)
})
t.Run("ignore the optional substitution inside an concatenation if it's path does not exist", func(t *testing.T) {
concatenation := concatenation{&Substitution{"a", true}}
object := Object{"a": Int(5), "b": concatenation}
err := resolveSubstitutions(object, concatenation)
assertNoError(t, err)
})
t.Run("return error if subConfig is not an object, array or concatenation", func(t *testing.T) {
subInt := Int(42)
object := Object{"a": Int(5), "b": subInt}
err := resolveSubstitutions(object, subInt)
expectedError := invalidValueError("substitutions are only allowed in field values and array elements", 0, 0)
assertError(t, err, expectedError)
})
t.Run("extract valueWithAlternative value with string type", func(t *testing.T) {
parser := newParser(strings.NewReader("a: stringValue, a:${?b}"))
expected := Object{"a": &valueWithAlternative{
value: String("stringValue"),
alternative: &Substitution{path: "b", optional: true},
}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("extract valueWithAlternative value with number type", func(t *testing.T) {
parser := newParser(strings.NewReader("a: 1, a:${?b}"))
expected := Object{"a": &valueWithAlternative{
value: Int(1),
alternative: &Substitution{path: "b", optional: true},
}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("extract valueWithAlternative value with duration type", func(t *testing.T) {
parser := newParser(strings.NewReader("a: 1s, a:${?b}"))
expected := Object{"a": &valueWithAlternative{
value: Duration(time.Second),
alternative: &Substitution{path: "b", optional: true},
}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("extract valueWithAlternative value with boolean type", func(t *testing.T) {
parser := newParser(strings.NewReader("a: true, a:${?b}"))
expected := Object{"a": &valueWithAlternative{
value: Boolean(true),
alternative: &Substitution{path: "b", optional: true},
}}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
t.Run("extract valueWithAlternative value and overwrite alternatives", func(t *testing.T) {
parser := newParser(strings.NewReader("a: static, a:${?b}"))
expected := Object{
"a": &valueWithAlternative{value: String("static"), alternative: &Substitution{path: "b", optional: true}},
}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})
}
func TestParsePlusEqualsValue(t *testing.T) {
t.Run("create an array that contains the value if the existingItems map does not contain a value with the given key", func(t *testing.T) {
parser := newParser(strings.NewReader("a += 42"))
advanceScanner(t, parser, "42")
existingItems := Object{}
expected := Object{"a": Array{Int(42)}}
err := parser.parsePlusEqualsValue(existingItems, "a")
assertNoError(t, err)
assertDeepEqual(t, existingItems, expected)
})
t.Run("return the error received from extractValue method if any, if the existingItems map does not contain a value with the given key", func(t *testing.T) {
parser := newParser(strings.NewReader("a += [42"))
advanceScanner(t, parser, "[")
err := parser.parsePlusEqualsValue(Object{}, "a")
expectedError := invalidArrayError("parenthesis do not match", 1, 7)
assertError(t, err, expectedError)
})
t.Run("return an error if the existingItems map contains non-array value with the given key", func(t *testing.T) {
parser := newParser(strings.NewReader("a: 1, a += 42"))
advanceScanner(t, parser, "42")
existingItems := Object{"a": Int(1)}
err := parser.parsePlusEqualsValue(existingItems, "a")
expectedError := invalidValueError(fmt.Sprintf("value: %q of the key: %q is not an array", "1", "a"), 1, 14)
assertError(t, err, expectedError)
})
t.Run("return the error received from extractValue method if any, if the existingItems map contains an array with the given key", func(t *testing.T) {
parser := newParser(strings.NewReader("a: [5], a += {42"))
advanceScanner(t, parser, "{")
existingItems := Object{"a": Array{Int(5)}}
err := parser.parsePlusEqualsValue(existingItems, "a")
expectedError := invalidObjectError("parenthesis do not match", 1, 15)
assertError(t, err, expectedError)
})
t.Run("append the value if the existingItems map contains an array with the given key", func(t *testing.T) {
parser := newParser(strings.NewReader("a: [5], a += 42"))
advanceScanner(t, parser, "42")
existingItems := Object{"a": Array{Int(5)}}
expected := Object{"a": Array{Int(5), Int(42)}}
err := parser.parsePlusEqualsValue(existingItems, "a")
assertNoError(t, err)
assertDeepEqual(t, existingItems, expected)
})
}
func TestValidateIncludeValue(t *testing.T) {
t.Run("return error if the include value starts with 'file' but opening parenthesis is missing", func(t *testing.T) {
parser := newParser(strings.NewReader("include file[abc.conf]"))
advanceScanner(t, parser, "file")
expectedError := invalidValueError("missing opening parenthesis", 1, 13)
got, err := parser.validateIncludeValue()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return error if the include value starts with 'file' but closing parenthesis is missing", func(t *testing.T) {
parser := newParser(strings.NewReader("include file(abc.conf"))
advanceScanner(t, parser, "file")
expectedError := invalidValueError("missing closing parenthesis", 1, 17)
got, err := parser.validateIncludeValue()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return error if the include value starts with 'classpath' but opening parenthesis is missing", func(t *testing.T) {
parser := newParser(strings.NewReader("include classpath[abc.conf]"))
advanceScanner(t, parser, "classpath")
expectedError := invalidValueError("missing opening parenthesis", 1, 18)
got, err := parser.validateIncludeValue()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return error if the include value starts with 'classpath' but closing parenthesis is missing", func(t *testing.T) {
parser := newParser(strings.NewReader("include classpath(abc.conf"))
advanceScanner(t, parser, "classpath")
expectedError := invalidValueError("missing closing parenthesis", 1, 22)
got, err := parser.validateIncludeValue()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return error if the include value does not start with double quotes", func(t *testing.T) {
parser := newParser(strings.NewReader("include abc.conf"))
advanceScanner(t, parser, "abc")
expectedError := invalidValueError("expected quoted string, optionally wrapped in 'file(...)' or 'classpath(...)'", 1, 9)
got, err := parser.validateIncludeValue()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return error if the include value does not end with double quotes", func(t *testing.T) {
parser := newParser(strings.NewReader(`include "abc.conf`))
advanceScanner(t, parser, `"abc.conf`)
expectedError := invalidValueError("expected quoted string, optionally wrapped in 'file(...)' or 'classpath(...)'", 1, 9)
got, err := parser.validateIncludeValue()
assertError(t, err, expectedError)
assertNil(t, got)
})
t.Run("return error if the include value is just a double quotes", func(t *testing.T) {
parser := newParser(strings.NewReader(`include "`))