-
Notifications
You must be signed in to change notification settings - Fork 1
/
configurama_test.go
977 lines (908 loc) · 32.7 KB
/
configurama_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
package configurama
import (
"errors"
"fmt"
"reflect"
"testing"
)
// empty is a convenience map that refers to the empty configuration pool.
var empty = map[string]map[string]string{}
func TestTypes(t *testing.T) {
type Types struct {
NegInt, ZeroInt, PosInt int
NegFloat, ZeroFloat, PosFloat float32
ZeroBool, IntBool, TrueBool bool
ZeroString, MixedString string
InRange uint8
}
params := map[string]map[string]string{
"default": {
"NegInt": "-50",
"ZeroInt": "0",
"PosInt": "9000",
"NegFloat": "-0.5",
"ZeroFloat": "0",
"PosFloat": "0.5",
"ZeroBool": "false",
"IntBool": "1",
"TrueBool": "true",
"ZeroString": "",
"MixedString": "abc123ÆØÅÖ -- 9.0True",
"InRange": "12",
},
}
c := New(params)
var types Types
err := c.Extract("default", "", &types)
verifyNil(t, err)
if types.NegInt != -50 {
t.Errorf("expected value %d, got %d", -50, types.NegInt)
}
if types.ZeroInt != 0 {
t.Errorf("expected value %d, got %d", 0, types.ZeroInt)
}
if types.PosInt != 9000 {
t.Errorf("expected value %d, got %d", 9000, types.PosInt)
}
if types.NegFloat != -0.5 {
t.Errorf("expected value %.2f, got %.2f", -0.5, types.NegFloat)
}
if types.ZeroFloat != 0.0 {
t.Errorf("expected value %.2f, got %.2f", 0.0, types.ZeroFloat)
}
if types.PosFloat != 0.5 {
t.Errorf("expected value %.2f, got %.2f", 0.5, types.PosFloat)
}
if types.ZeroBool != false {
t.Errorf("expected value %t, got %t", false, types.ZeroBool)
}
if types.IntBool != true {
t.Errorf("expected value %t, got %t", true, types.IntBool)
}
if types.TrueBool != true {
t.Errorf("expected value %t, got %t", true, types.TrueBool)
}
if types.ZeroString != "" {
t.Errorf("expected value %q, got %q", "", types.ZeroString)
}
if types.MixedString != "abc123ÆØÅÖ -- 9.0True" {
t.Errorf("expected value %q, got %q", "abc123ÆØÅÖ -- 9.0True", types.MixedString)
}
if types.InRange != 12 {
t.Errorf("expected value %d, got %d", 12, types.InRange)
}
}
func TestRaw(t *testing.T) {
params := map[string]map[string]string{
"dev": {
"db.host": "localhost",
"db.username": "root",
"db.password": "secret",
"db.connstring": "",
},
}
cnf := New(params)
actual := cnf.Raw()
if !reflect.DeepEqual(params, actual) {
t.Error("expected raw pool data to equal given pool data")
}
}
func TestGet(t *testing.T) {
cnf := New(map[string]map[string]string{
"dev": {
"db.host": "localhost",
"db.username": "root",
"db.password": "secret",
"db.connstring": "",
},
})
tt := map[string]struct {
section, key, expected string
ok bool
}{
"empty case": {
"", "", "", false,
},
"missing key": {
"dev", "unknown", "", false,
},
"matching key": {
"dev", "db.username", "root", true,
},
"matching key, empty value": {
"dev", "db.connstring", "", true,
},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
actual, ok := cnf.Get(tc.section, tc.key)
if ok != tc.ok {
t.Errorf("expected boolean %t, got %t", tc.ok, ok)
}
if actual != tc.expected {
t.Errorf("expected value %q, got %q", tc.expected, actual)
}
})
}
}
func TestUnset(t *testing.T) {
type Hero struct {
Name, Alias string
WorldSavedLast int
Score float32
}
c := New(map[string]map[string]string{"Hero": {
"name": "Peter Parker",
"alias": "Spiderman",
"score": "0.8",
"worldSavedLast": "2018",
}, "Enemy": {
"name": "Harry Osborne",
"alias": "The Green Goblin",
"score": "0.7",
}})
ok := c.Unset("Hero", "worldSavedLast")
if !ok {
t.Error("expected c.Unset() to return true, got false")
}
var h Hero
err := c.Extract("Hero", "", &h)
verifyNil(t, err)
if h.Name != "Peter Parker" {
t.Fatalf("expected Name to equal %q, got %q", "Peter Parker", h.Name)
}
if h.Alias != "Spiderman" {
t.Fatalf("expected Alias to equal %q, got %q", "Spiderman", h.Alias)
}
if h.WorldSavedLast != 0 {
t.Fatalf("expected WorldSavedLast to equal %d, got %d", 0, h.WorldSavedLast)
}
if h.Score != 0.8 {
t.Fatalf("expected Score to equal %.5f, got %.5f", 0.8, h.Score)
}
}
func TestExtract(t *testing.T) {
type Hero struct {
Name, Alias string
WorldSavedLast int
Score float32
WearsMask bool
}
t.Run("it handles the case of a missing section", func(t *testing.T) {
var h Hero
c := New(map[string]map[string]string{"Hero": {
"name": "Peter Parker",
"alias": "Spiderman",
"worldSavedLast": "2018",
"score": "0.12345",
}})
err := c.Extract("Bad guy", "", &h)
if err == nil {
t.Fatal("expected error, got nil")
}
expected := "unknown section name: Bad guy"
if err.Error() != expected {
t.Fatalf("expected error to equal %q, got %q", expected, err.Error())
}
})
t.Run("it correctly extracts all parameters", func(t *testing.T) {
var h Hero
c := New(map[string]map[string]string{"Hero": {
"name": "Peter Parker",
"alias": "Spiderman",
"worldSavedLast": "2018",
"score": "0.12345",
"wearsmask": "true",
}})
err := c.Extract("Hero", "", &h)
verifyNil(t, err)
if h.Name != "Peter Parker" {
t.Errorf("expected Name to equal %q, got %q", "Peter Parker", h.Name)
}
if h.Alias != "Spiderman" {
t.Errorf("expected Alias to equal %q, got %q", "Spiderman", h.Alias)
}
if h.WorldSavedLast != 2018 {
t.Errorf("expected WorldSavedLast to equal %d, got %d", 2018, h.WorldSavedLast)
}
if h.Score != 0.12345 {
t.Errorf("expected Score to equal %.5f, got %.5f", 0.12345, h.Score)
}
if !h.WearsMask {
t.Error("expected WearsMask to be true, got false")
}
})
t.Run("it ignores unmatched parameters", func(t *testing.T) {
var h Hero
c := New(map[string]map[string]string{"Hero": {
"name": "Peter Parker",
"alias": "Spiderman",
"worldSavedLast": "2018",
"score": "0.12345",
"other": "Nothing to see here",
}})
err := c.Extract("Hero", "", &h)
verifyNil(t, err)
if h.Name != "Peter Parker" {
t.Fatalf("expected Name to equal %q, got %q", "Peter Parker", h.Name)
}
if h.Alias != "Spiderman" {
t.Fatalf("expected Alias to equal %q, got %q", "Spiderman", h.Alias)
}
if h.WorldSavedLast != 2018 {
t.Fatalf("expected WorldSavedLast to equal %d, got %d", 2018, h.WorldSavedLast)
}
if h.Score != 0.12345 {
t.Fatalf("expected Score to equal %.5f, got %.5f", 0.12345, h.Score)
}
})
t.Run("it leaves unset parameters at their zero values", func(t *testing.T) {
var h Hero
c := New(map[string]map[string]string{"Hero": {
"name": "Peter Parker",
"worldSavedLast": "2018",
}})
err := c.Extract("Hero", "", &h)
verifyNil(t, err)
if h.Name != "Peter Parker" {
t.Fatalf("expected Name to equal %q, got %q", "Peter Parker", h.Name)
}
if h.Alias != "" {
t.Fatalf("expected Alias to equal %q, got %q", "", h.Alias)
}
if h.WorldSavedLast != 2018 {
t.Fatalf("expected WorldSavedLast to equal %d, got %d", 2018, h.WorldSavedLast)
}
if h.Score != 0.0 {
t.Fatalf("expected Score to equal %.5f, got %.5f", 0.0, h.Score)
}
})
t.Run("it errors for parameters that cannot be parsed", func(t *testing.T) {
var h Hero
c := New(map[string]map[string]string{"Hero": {
"worldSavedLast": "yesterday", // Not a number.
}})
err := c.Extract("Hero", "", &h)
if err == nil {
t.Fatal("expected a parsing error, got nil")
}
if h.WorldSavedLast != 0 {
t.Fatalf("expected WorldSavedLast to equal %d, got %d", 0, h.WorldSavedLast)
}
})
t.Run("it handles sections correctly", func(t *testing.T) {
var h1, h2 Hero
c := New(map[string]map[string]string{"Hero": {
"name": "Peter Parker",
"alias": "Spiderman",
"score": "0.8",
"worldSavedLast": "2018",
}, "Enemy": {
"name": "Harry Osborne",
"alias": "The Green Goblin",
"score": "0.7",
}})
err1 := c.Extract("Hero", "", &h1)
verifyNil(t, err1)
if h1.Name != "Peter Parker" {
t.Fatalf("expected Name to equal %q, got %q", "Peter Parker", h1.Name)
}
if h1.Alias != "Spiderman" {
t.Fatalf("expected Alias to equal %q, got %q", "Spiderman", h1.Alias)
}
if h1.WorldSavedLast != 2018 {
t.Fatalf("expected WorldSavedLast to equal %d, got %d", 2018, h1.WorldSavedLast)
}
if h1.Score != 0.8 {
t.Fatalf("expected Score to equal %.5f, got %.5f", 0.8, h1.Score)
}
err2 := c.Extract("Enemy", "", &h2)
verifyNil(t, err2)
if h2.Name != "Harry Osborne" {
t.Fatalf("expected Name to equal %q, got %q", "Harry Osborne", h2.Name)
}
if h2.Alias != "The Green Goblin" {
t.Fatalf("expected Alias to equal %q, got %q", "The Green Goblin", h2.Alias)
}
if h2.WorldSavedLast != 0 {
t.Fatalf("expected WorldSavedLast to equal %d, got %d", 0, h2.WorldSavedLast)
}
if h2.Score != 0.7 {
t.Fatalf("expected Score to equal %.5f, got %.5f", 0.8, h2.Score)
}
})
t.Run("it does case insensitive matching", func(t *testing.T) {
c := New(map[string]map[string]string{"MyConfig": {
"serviceName": "MySQL", // Lowercased first letter.
"PORTNUMBER": "3306", // Uppercase letters.
"username": "root", // Lowercase letters.
"dbpassword": "secret", // Mismatched case.
"realpassword": "more secret", // Unexported field in struct.
}})
type TitleCaseConfig struct {
ServiceName string
PortNumber int
UserName string
DBPassword string
realPassword string
}
var tcConfig TitleCaseConfig
err1 := c.Extract("MyConfig", "", &tcConfig)
verifyNil(t, err1)
if tcConfig.ServiceName != "MySQL" {
t.Errorf("expected ServiceName to equal %q, got %q", "MySQL", tcConfig.ServiceName)
}
if tcConfig.PortNumber != 3306 {
t.Errorf("expected PortNumber to equal %d, got %d", 3306, tcConfig.PortNumber)
}
if tcConfig.UserName != "root" {
t.Errorf("expected UserName to equal %q, got %q", "root", tcConfig.UserName)
}
if tcConfig.DBPassword != "secret" {
t.Errorf("expected DBPassword to equal %q, got %q", "secret", tcConfig.DBPassword)
}
if tcConfig.realPassword != "" {
t.Errorf("expected realPassword to equal %q, got %q", "", tcConfig.realPassword)
}
})
t.Run("it handles prefixes correctly", func(t *testing.T) {
c := New(map[string]map[string]string{"DBConfig": {
"db.mysql.servicename": "MySQL",
"db.mysql.portnumber": "3306",
"db.mysql.username": "root",
"db.mysql.password": "secret",
"unrelated.service.name": "MyService",
"unrelated.service.username": "Martin",
}})
type DBConfig struct {
ServiceName string
PortNumber int
UserName string
Password string
}
var dbConfig DBConfig
err1 := c.Extract("DBConfig", "db.mysql.", &dbConfig)
verifyNil(t, err1)
if dbConfig.ServiceName != "MySQL" {
t.Errorf("expected ServiceName to equal %q, got %q", "MySQL", dbConfig.ServiceName)
}
if dbConfig.PortNumber != 3306 {
t.Errorf("expected PortNumber to equal %d, got %d", 3306, dbConfig.PortNumber)
}
if dbConfig.UserName != "root" {
t.Errorf("expected UserName to equal %q, got %q", "root", dbConfig.UserName)
}
if dbConfig.Password != "secret" {
t.Errorf("expected Password to equal %q, got %q", "secret", dbConfig.Password)
}
})
}
func TestExtractWithHooks(t *testing.T) {
type Contact struct {
Name, Gender, Email, Phone, Address, Zip, City, Country string
}
params := map[string]map[string]string{"main": {
"name": "Guy Friendly",
"gender": "male",
"email": "[email protected]",
"phone": "+45 25205016",
"address": "Overthere Street 18",
"zip": "2200",
"city": "Copenhagen",
"country": "Denmark",
}, "extra": {
"name": "Kim Kindly",
"gender": "female",
"email": "[email protected]",
"phone": "",
"address": "Elsewhere Street 22",
"zip": "2200",
"city": "Copenhagen",
"country": "Denmark",
}, "prefixed": {
"one.var1": "var1.1",
"one.var2": "var1.2",
"two.var1": "var2.1",
"two.var2": "var2.2",
}}
t.Run("it works with nil hooks", func(t *testing.T) {
var con Contact
c := New(params)
err := c.ExtractWithHooks("main", "", &con, nil, nil)
verifyNil(t, err)
})
t.Run("it calls the pre hook with correct parameters", func(t *testing.T) {
var con Contact
var called bool
c := New(params)
verifyContact := func(t *testing.T, m map[string]string) {
t.Helper()
if len(m) != 8 {
t.Errorf("expected %d parameters, got %d", 8, len(m))
}
if !reflect.DeepEqual(params["main"], m) {
t.Errorf("expected pre-hook parameters to equal parameters from section %q", "main")
}
}
pre := func(m map[string]string) error {
verifyContact(t, m)
called = true
return nil
}
err := c.ExtractWithHooks("main", "", &con, pre, nil)
verifyNil(t, err)
if !called {
t.Errorf("expected pre-hook to have been called")
}
})
t.Run("it calls the pre hook with only prefix parameters", func(t *testing.T) {
var con Contact
var called bool
c := New(params)
verifyContact := func(t *testing.T, m map[string]string) {
t.Helper()
if len(m) != 2 {
t.Errorf("expected %d parameters, got %d", 2, len(m))
}
if m["var1"] != "var1.1" {
t.Errorf("expected pre-hook parameter %q to equal %q, got %q", "var1", "var1.1", m["var1"])
}
if m["var2"] != "var1.2" {
t.Errorf("expected pre-hook parameter %q to equal %q, got %q", "var2", "var1.2", m["var2"])
}
}
pre := func(m map[string]string) error {
verifyContact(t, m)
called = true
return nil
}
err := c.ExtractWithHooks("prefixed", "one.", &con, pre, nil)
verifyNil(t, err)
if !called {
t.Errorf("expected pre-hook to have been called")
}
})
t.Run("it returns the same error as the pre-hook", func(t *testing.T) {
var con Contact
var called bool
c := New(params)
pre := func(m map[string]string) error {
called = true
return errors.New("I made a boo-boo")
}
err := c.ExtractWithHooks("main", "", &con, pre, nil)
if !called {
t.Errorf("expected pre-hook to have been called")
}
if err == nil {
t.Error("expected an error")
}
if err.Error() != "I made a boo-boo" {
t.Errorf("expected error to contain %q, got %q", "I made a boo-boo", err.Error())
}
})
t.Run("it calls the post hook", func(t *testing.T) {
var con Contact
var called bool
c := New(params)
pre := func(m map[string]string) error {
return errors.New("I made a boo-boo")
}
post := func() {
called = true
}
err := c.ExtractWithHooks("main", "", &con, pre, post)
if err == nil {
t.Error("expected an error")
}
if called {
t.Error("expected post-hook to not have been called")
}
})
t.Run("it does not call the post hook when the pre hook returns an error", func(t *testing.T) {
var con Contact
var called bool
c := New(params)
post := func() {
called = true
}
err := c.ExtractWithHooks("main", "", &con, nil, post)
verifyNil(t, err)
if !called {
t.Errorf("expected post-hook to have been called")
}
})
}
func TestMergeOverwriteStrategy(t *testing.T) {
tt := map[string]struct {
first map[string]map[string]string
second map[string]map[string]string
expected map[string]map[string]string
}{
"empty case": {
first: empty,
second: empty,
expected: empty,
},
"trivial case, first": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
second: empty,
expected: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
},
"trivial case, second": {
first: empty,
second: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
},
"trivial case, overlapping": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
},
"trivial case, overlapping, diff value": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value two"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value two"}},
},
"simple case, overlapping, diff keys": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field two": "Value two"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field three": "Value three"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field two": "Value two", "Field three": "Value three"}},
},
"simple case, overlapping reverse, diff keys": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field three": "Value three"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field two": "Value two"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field two": "Value two", "Field three": "Value three"}},
},
"complex case, sections with overlapping keys": {
first: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field three": "Value one-three", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field three": "Value two-three", "Field six": "Value two-six"},
},
second: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field two": "Value two-two"},
"Params two": {"Field six": "Value two-six-new"},
"Params three": {"Field one": "Value two-one"},
},
expected: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field two": "Value two-two", "Field three": "Value one-three", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field three": "Value two-three", "Field six": "Value two-six-new"},
"Params three": {"Field one": "Value two-one"},
},
},
"complex case, sections with disjoint keys": {
first: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field three": "Value one-three", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field three": "Value two-three", "Field six": "Value two-six"},
},
second: map[string]map[string]string{
"Params one": {"Field two": "Value two-two", "Field four": "Value one-four"},
"Params two": {"Field two": "Value two-two", "Field four": "Value two-four", "Field five": "Value two-five"},
"Params three": {"Field one": "Value three-one"},
},
expected: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field two": "Value two-two", "Field three": "Value one-three", "Field four": "Value one-four", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field two": "Value two-two", "Field three": "Value two-three", "Field four": "Value two-four", "Field five": "Value two-five", "Field six": "Value two-six"},
"Params three": {"Field one": "Value three-one"},
},
},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
actual, err := merge(tc.first, tc.second, Overwrite)
verifyNil(t, err)
verifyEqual(t, tc.expected, actual)
})
}
}
func TestMergeKeepStrategy(t *testing.T) {
tt := map[string]struct {
first map[string]map[string]string
second map[string]map[string]string
expected map[string]map[string]string
}{
"empty case": {
first: empty,
second: empty,
expected: empty,
},
"trivial case, first": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
second: empty,
expected: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
},
"trivial case, second": {
first: empty,
second: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
},
"trivial case, overlapping": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
},
"trivial case, overlapping, diff value": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value two"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
},
"simple case, overlapping, diff keys": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field two": "Value two"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field three": "Value three"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field two": "Value two", "Field three": "Value three"}},
},
"simple case, overlapping reverse, diff keys": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field three": "Value three"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field two": "Value two"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field two": "Value two", "Field three": "Value three"}},
},
"complex case, sections with overlapping keys": {
first: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field three": "Value one-three", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field three": "Value two-three", "Field six": "Value two-six"},
},
second: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field two": "Value two-two"},
"Params two": {"Field six": "Value two-six-new"},
"Params three": {"Field one": "Value two-one"},
},
expected: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field two": "Value two-two", "Field three": "Value one-three", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field three": "Value two-three", "Field six": "Value two-six"},
"Params three": {"Field one": "Value two-one"},
},
},
"complex case, sections with disjoint keys": {
first: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field three": "Value one-three", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field three": "Value two-three", "Field six": "Value two-six"},
},
second: map[string]map[string]string{
"Params one": {"Field two": "Value two-two", "Field four": "Value one-four"},
"Params two": {"Field two": "Value two-two", "Field four": "Value two-four", "Field five": "Value two-five"},
"Params three": {"Field one": "Value three-one"},
},
expected: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field two": "Value two-two", "Field three": "Value one-three", "Field four": "Value one-four", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field two": "Value two-two", "Field three": "Value two-three", "Field four": "Value two-four", "Field five": "Value two-five", "Field six": "Value two-six"},
"Params three": {"Field one": "Value three-one"},
},
},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
actual, err := merge(tc.first, tc.second, Keep)
verifyNil(t, err)
verifyEqual(t, tc.expected, actual)
})
}
}
func TestMergeReportStrategy(t *testing.T) {
tt := map[string]struct {
first map[string]map[string]string
second map[string]map[string]string
expected map[string]map[string]string
expectedError error
}{
"empty case": {
first: empty,
second: empty,
expected: empty,
expectedError: nil,
},
"trivial case, first": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
second: empty,
expected: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
expectedError: nil,
},
"trivial case, second": {
first: empty,
second: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
expected: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
expectedError: nil,
},
"trivial case, overlapping, diff value": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value two"}},
expected: empty,
expectedError: fmt.Errorf("section %q, key %q already exists", "Params one", "Field one"),
},
"simple case, overlapping, diff keys": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field two": "Value two"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field three": "Value three"}},
expected: empty,
expectedError: fmt.Errorf("section %q, key %q already exists", "Params one", "Field one"),
},
"simple case, overlapping reverse, diff keys": {
first: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field three": "Value three"}},
second: map[string]map[string]string{"Params one": {"Field one": "Value one", "Field two": "Value two"}},
expected: empty,
expectedError: fmt.Errorf("section %q, key %q already exists", "Params one", "Field one"),
},
"complex case, sections with overlapping keys": {
first: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field three": "Value one-three", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field three": "Value two-three", "Field six": "Value two-six"},
},
second: map[string]map[string]string{
"Params one": {"Field two": "Value two-two", "Field four": "Value one-four"},
"Params two": {"Field six": "Value two-six-new"},
"Params three": {"Field one": "Value two-one"},
},
expected: empty,
expectedError: fmt.Errorf("section %q, key %q already exists", "Params two", "Field six"),
},
"complex case, sections with disjoint keys": {
first: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field three": "Value one-three", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field three": "Value two-three", "Field six": "Value two-six"},
},
second: map[string]map[string]string{
"Params one": {"Field two": "Value two-two", "Field four": "Value one-four"},
"Params two": {"Field two": "Value two-two", "Field four": "Value two-four", "Field five": "Value two-five"},
"Params three": {"Field one": "Value three-one"},
},
expected: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field two": "Value two-two", "Field three": "Value one-three", "Field four": "Value one-four", "Field five": "Value one-five"},
"Params two": {"Field one": "Value two-one", "Field two": "Value two-two", "Field three": "Value two-three", "Field four": "Value two-four", "Field five": "Value two-five", "Field six": "Value two-six"},
"Params three": {"Field one": "Value three-one"},
},
expectedError: nil,
},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
actual, err := merge(tc.first, tc.second, Report)
if tc.expectedError != nil {
if err == nil {
t.Error("expected an error, got nil")
} else if err.Error() != tc.expectedError.Error() {
t.Errorf("expected error message to match %q, got %q", tc.expectedError.Error(), err.Error())
}
} else {
verifyNil(t, err)
verifyEqual(t, tc.expected, actual)
}
})
}
}
func TestCompare(t *testing.T) {
tt := map[string]struct {
p1, p2, expected map[string]map[string]string
}{
"empty case": {
p1: empty,
p2: empty,
expected: empty,
},
"trivial case": {
p1: map[string]map[string]string{"default": {"key": "val"}},
p2: map[string]map[string]string{"default": {"key": "val"}},
expected: empty,
},
"new keys": {
p1: map[string]map[string]string{"default": {}},
p2: map[string]map[string]string{"default": {"key1": "val1", "key2": "val2"}},
expected: map[string]map[string]string{"default": {"key1": "val1", "key2": "val2"}},
},
"empty values": {
p1: map[string]map[string]string{"default": {"key3": "val3"}},
p2: map[string]map[string]string{"default": {"key1": "", "key2": ""}},
expected: map[string]map[string]string{"default": {"key1": "", "key2": ""}},
},
"one diff in one section": {
p1: map[string]map[string]string{"default": {"key": "val"}},
p2: map[string]map[string]string{"default": {"key": "val2"}},
expected: map[string]map[string]string{"default": {"key": "val2"}},
},
"two diffs in one section": {
p1: map[string]map[string]string{"default": {"key": "val"}},
p2: map[string]map[string]string{"default": {"key1": "val1", "key2": "val2"}},
expected: map[string]map[string]string{"default": {"key1": "val1", "key2": "val2"}},
},
"two diffs in one section, reverse": {
p1: map[string]map[string]string{"default": {"key1": "val1", "key2": "val2"}},
p2: map[string]map[string]string{"default": {"key": "val"}},
expected: map[string]map[string]string{"default": {"key": "val"}},
},
"two diffs in two sections": {
p1: map[string]map[string]string{"section1": {"key1": "val1"}, "section2": {"key2": "val2", "key3": "val3"}},
p2: map[string]map[string]string{"section1": {"KEY1": "val1"}, "section2": {"key2": "VAL2", "key3": "val3", "key4": "val4"}},
expected: map[string]map[string]string{"section1": {"KEY1": "val1"}, "section2": {"key2": "VAL2", "key4": "val4"}},
},
"two identical sections": {
p1: map[string]map[string]string{"section1": {"key1": "val1"}, "section2": {"key2": "val2", "key3": "val3"}},
p2: map[string]map[string]string{"section1": {"key1": "val1"}, "section2": {"key2": "val2", "key3": "val3"}},
expected: empty,
},
}
for name, tc := range tt {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
c1 := New(tc.p1)
c2 := New(tc.p2)
actual := c1.Compare(*c2)
if !reflect.DeepEqual(actual, tc.expected) {
t.Errorf("expected c1 to equal c2, got diff: %v", actual)
}
})
}
}
func TestMustPrettyPrint(t *testing.T) {
tt := map[string]struct {
config map[string]map[string]string
expected string
}{
"empty case": {
config: empty,
expected: "",
},
"trivial case": {
config: map[string]map[string]string{"Params one": {"Field one": "Value one"}},
expected: `[Params one]
Field one: Value one`,
},
"trivial edge case": {
config: map[string]map[string]string{"Params one": {}},
expected: `[Params one]`,
},
"two-section case": {
config: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field two": "Value one-two", "Field three": "Value one-three"},
"Params two": {"Field one": "Value two-one", "Field two": "Value two-two", "Field three": "Value two-three"},
},
expected: `[Params one]
Field one: Value one-one
Field three: Value one-three
Field two: Value one-two
[Params two]
Field one: Value two-one
Field three: Value two-three
Field two: Value two-two`,
},
"multi-section case": {
config: map[string]map[string]string{
"Params one": {"Field one": "Value one-one", "Field two": "Value one-two", "Field three": "Value one-three"},
"Params two": {"Field one": "Value two-one", "Field two": "Value two-two", "Field three": "Value two-three"},
"Params three": {},
"Params four": {"Field one": "Value four-one", "Field two": "Value four-two", "Field three": "Value four-three"},
},
expected: `[Params four]
Field one: Value four-one
Field three: Value four-three
Field two: Value four-two
[Params one]
Field one: Value one-one
Field three: Value one-three
Field two: Value one-two
[Params three]
[Params two]
Field one: Value two-one
Field three: Value two-three
Field two: Value two-two`,
},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
actual := MustPrettyPrint(tc.config, " ")
if actual != tc.expected {
t.Errorf("expected output to match %q, got %q", tc.expected, actual)
}
})
}
}
func verifyNil(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
}
func verifyEqual(t *testing.T, first, second map[string]map[string]string) {
if !reflect.DeepEqual(first, second) {
fmt.Println(MustPrettyPrint(first, " "))
fmt.Println(" != ")
fmt.Println(MustPrettyPrint(second, " "))
t.Error("expected the two maps to be identical")
}
}