github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/agent/config/patch_hcl_test.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "reflect" 7 "testing" 8 ) 9 10 func parse(s string) map[string]interface{} { 11 var m map[string]interface{} 12 if err := json.Unmarshal([]byte(s), &m); err != nil { 13 panic(s + ":" + err.Error()) 14 } 15 return m 16 } 17 18 func TestPatchSliceOfMaps(t *testing.T) { 19 tests := []struct { 20 in, out string 21 skip []string 22 }{ 23 { 24 in: `{"a":{"b":"c"}}`, 25 out: `{"a":{"b":"c"}}`, 26 }, 27 { 28 in: `{"a":[{"b":"c"}]}`, 29 out: `{"a":{"b":"c"}}`, 30 }, 31 { 32 in: `{"a":[{"b":[{"c":"d"}]}]}`, 33 out: `{"a":{"b":{"c":"d"}}}`, 34 }, 35 { 36 in: `{"a":[{"b":"c"}]}`, 37 out: `{"a":[{"b":"c"}]}`, 38 skip: []string{"a"}, 39 }, 40 { 41 in: `{ 42 "services": [ 43 { 44 "checks": [ 45 { 46 "header": [ 47 {"a":"b"} 48 ] 49 } 50 ] 51 } 52 ] 53 }`, 54 out: `{ 55 "services": [ 56 { 57 "checks": [ 58 { 59 "header": {"a":"b"} 60 } 61 ] 62 } 63 ] 64 }`, 65 skip: []string{"services", "services.checks"}, 66 }, 67 } 68 69 for i, tt := range tests { 70 desc := fmt.Sprintf("%02d: %s -> %s skip: %v", i, tt.in, tt.out, tt.skip) 71 t.Run(desc, func(t *testing.T) { 72 out := patchSliceOfMaps(parse(tt.in), tt.skip) 73 if got, want := out, parse(tt.out); !reflect.DeepEqual(got, want) { 74 t.Fatalf("\ngot %#v\nwant %#v", got, want) 75 } 76 }) 77 } 78 }