github.com/serbaut/terraform@v0.6.12-0.20160607213102-ac2d195cc560/flatmap/map_test.go (about) 1 package flatmap 2 3 import ( 4 "reflect" 5 "sort" 6 "testing" 7 ) 8 9 func TestMapContains(t *testing.T) { 10 cases := []struct { 11 Input map[string]string 12 Key string 13 Result bool 14 }{ 15 { 16 Input: map[string]string{ 17 "foo": "bar", 18 "bar": "nope", 19 }, 20 Key: "foo", 21 Result: true, 22 }, 23 24 { 25 Input: map[string]string{ 26 "foo": "bar", 27 "bar": "nope", 28 }, 29 Key: "baz", 30 Result: false, 31 }, 32 } 33 34 for i, tc := range cases { 35 actual := Map(tc.Input).Contains(tc.Key) 36 if actual != tc.Result { 37 t.Fatalf("case %d bad: %#v", i, tc.Input) 38 } 39 } 40 } 41 42 func TestMapDelete(t *testing.T) { 43 m := Flatten(map[string]interface{}{ 44 "foo": "bar", 45 "routes": []map[string]string{ 46 map[string]string{ 47 "foo": "bar", 48 }, 49 }, 50 }) 51 52 m.Delete("routes") 53 54 expected := Map(map[string]string{"foo": "bar"}) 55 if !reflect.DeepEqual(m, expected) { 56 t.Fatalf("bad: %#v", m) 57 } 58 } 59 60 func TestMapKeys(t *testing.T) { 61 cases := []struct { 62 Input map[string]string 63 Output []string 64 }{ 65 { 66 Input: map[string]string{ 67 "foo": "bar", 68 "bar.#": "bar", 69 "bar.0.foo": "bar", 70 "bar.0.baz": "bar", 71 }, 72 Output: []string{ 73 "bar", 74 "foo", 75 }, 76 }, 77 } 78 79 for _, tc := range cases { 80 actual := Map(tc.Input).Keys() 81 82 // Sort so we have a consistent view of the output 83 sort.Strings(actual) 84 85 if !reflect.DeepEqual(actual, tc.Output) { 86 t.Fatalf("input: %#v\n\nbad: %#v", tc.Input, actual) 87 } 88 } 89 } 90 91 func TestMapMerge(t *testing.T) { 92 cases := []struct { 93 One map[string]string 94 Two map[string]string 95 Result map[string]string 96 }{ 97 { 98 One: map[string]string{ 99 "foo": "bar", 100 "bar": "nope", 101 }, 102 Two: map[string]string{ 103 "bar": "baz", 104 "baz": "buz", 105 }, 106 Result: map[string]string{ 107 "foo": "bar", 108 "bar": "baz", 109 "baz": "buz", 110 }, 111 }, 112 } 113 114 for i, tc := range cases { 115 Map(tc.One).Merge(Map(tc.Two)) 116 if !reflect.DeepEqual(tc.One, tc.Result) { 117 t.Fatalf("case %d bad: %#v", i, tc.One) 118 } 119 } 120 }