github.imxd.top/hashicorp/consul@v1.4.5/agent/config/translate_test.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/pascaldekloe/goe/verify" 8 ) 9 10 func TestTranslateKeys(t *testing.T) { 11 fromJSON := func(s string) map[string]interface{} { 12 var m map[string]interface{} 13 if err := json.Unmarshal([]byte(s), &m); err != nil { 14 t.Fatal(err) 15 } 16 return m 17 } 18 19 tests := []struct { 20 desc string 21 in map[string]interface{} 22 out map[string]interface{} 23 dict map[string]string 24 }{ 25 { 26 desc: "x->y", 27 in: map[string]interface{}{"a": "aa", "x": "xx"}, 28 out: map[string]interface{}{"a": "aa", "y": "xx"}, 29 dict: map[string]string{"x": "y"}, 30 }, 31 { 32 desc: "discard x", 33 in: map[string]interface{}{"a": "aa", "x": "xx", "y": "yy"}, 34 out: map[string]interface{}{"a": "aa", "y": "yy"}, 35 dict: map[string]string{"x": "y"}, 36 }, 37 { 38 desc: "b.x->b.y", 39 in: map[string]interface{}{"a": "aa", "b": map[string]interface{}{"x": "xx"}}, 40 out: map[string]interface{}{"a": "aa", "b": map[string]interface{}{"y": "xx"}}, 41 dict: map[string]string{"x": "y"}, 42 }, 43 { 44 desc: "json: x->y", 45 in: fromJSON(`{"a":"aa","x":"xx"}`), 46 out: fromJSON(`{"a":"aa","y":"xx"}`), 47 dict: map[string]string{"x": "y"}, 48 }, 49 { 50 desc: "json: X->y", 51 in: fromJSON(`{"a":"aa","X":"xx"}`), 52 out: fromJSON(`{"a":"aa","y":"xx"}`), 53 dict: map[string]string{"x": "y"}, 54 }, 55 { 56 desc: "json: discard x", 57 in: fromJSON(`{"a":"aa","x":"xx","y":"yy"}`), 58 out: fromJSON(`{"a":"aa","y":"yy"}`), 59 dict: map[string]string{"x": "y"}, 60 }, 61 { 62 desc: "json: b.x->b.y", 63 in: fromJSON(`{"a":"aa","b":{"x":"xx"}}`), 64 out: fromJSON(`{"a":"aa","b":{"y":"xx"}}`), 65 dict: map[string]string{"x": "y"}, 66 }, 67 { 68 desc: "json: b[0].x->b[0].y", 69 in: fromJSON(`{"a":"aa","b":[{"x":"xx"}]}`), 70 out: fromJSON(`{"a":"aa","b":[{"y":"xx"}]}`), 71 dict: map[string]string{"x": "y"}, 72 }, 73 } 74 75 for _, tt := range tests { 76 t.Run(tt.desc, func(t *testing.T) { 77 TranslateKeys(tt.in, tt.dict) 78 if got, want := tt.in, tt.out; !verify.Values(t, "", got, want) { 79 t.Fail() 80 } 81 }) 82 } 83 }