github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/helper/flatmap/flatmap_test.go (about) 1 package flatmap 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 type simpleTypes struct { 9 b bool 10 i int 11 i8 int8 12 i16 int16 13 i32 int32 14 i64 int64 15 ui uint 16 ui8 uint8 17 ui16 uint16 18 ui32 uint32 19 ui64 uint64 20 f32 float32 21 f64 float64 22 c64 complex64 23 c128 complex128 24 s string 25 } 26 27 type linkedList struct { 28 value string 29 next *linkedList 30 } 31 32 type containers struct { 33 myslice []int 34 mymap map[string]linkedList 35 } 36 37 type interfaceHolder struct { 38 value interface{} 39 } 40 41 func TestFlatMap(t *testing.T) { 42 cases := []struct { 43 Input interface{} 44 Expected map[string]string 45 Filter []string 46 PrimitiveOnly bool 47 }{ 48 { 49 Input: nil, 50 Expected: nil, 51 }, 52 { 53 Input: &simpleTypes{ 54 b: true, 55 i: -10, 56 i8: 88, 57 i16: 1616, 58 i32: 3232, 59 i64: 6464, 60 ui: 10, 61 ui8: 88, 62 ui16: 1616, 63 ui32: 3232, 64 ui64: 6464, 65 f32: 3232, 66 f64: 6464, 67 c64: 64, 68 c128: 128, 69 s: "foobar", 70 }, 71 Expected: map[string]string{ 72 "b": "true", 73 "i": "-10", 74 "i8": "88", 75 "i16": "1616", 76 "i32": "3232", 77 "i64": "6464", 78 "ui": "10", 79 "ui8": "88", 80 "ui16": "1616", 81 "ui32": "3232", 82 "ui64": "6464", 83 "f32": "3232", 84 "f64": "6464", 85 "c64": "(64+0i)", 86 "c128": "(128+0i)", 87 "s": "foobar", 88 }, 89 }, 90 { 91 Input: &simpleTypes{ 92 b: true, 93 i: -10, 94 i8: 88, 95 i16: 1616, 96 i32: 3232, 97 i64: 6464, 98 ui: 10, 99 ui8: 88, 100 ui16: 1616, 101 ui32: 3232, 102 ui64: 6464, 103 f32: 3232, 104 f64: 6464, 105 c64: 64, 106 c128: 128, 107 s: "foobar", 108 }, 109 Filter: []string{"i", "i8", "i16"}, 110 Expected: map[string]string{ 111 "b": "true", 112 "i32": "3232", 113 "i64": "6464", 114 "ui": "10", 115 "ui8": "88", 116 "ui16": "1616", 117 "ui32": "3232", 118 "ui64": "6464", 119 "f32": "3232", 120 "f64": "6464", 121 "c64": "(64+0i)", 122 "c128": "(128+0i)", 123 "s": "foobar", 124 }, 125 }, 126 { 127 Input: &linkedList{ 128 value: "foo", 129 next: &linkedList{ 130 value: "bar", 131 next: nil, 132 }, 133 }, 134 Expected: map[string]string{ 135 "value": "foo", 136 "next.value": "bar", 137 "next.next": "nil", 138 }, 139 }, 140 { 141 Input: &linkedList{ 142 value: "foo", 143 next: &linkedList{ 144 value: "bar", 145 next: nil, 146 }, 147 }, 148 PrimitiveOnly: true, 149 Expected: map[string]string{ 150 "value": "foo", 151 }, 152 }, 153 { 154 Input: linkedList{ 155 value: "foo", 156 next: &linkedList{ 157 value: "bar", 158 next: nil, 159 }, 160 }, 161 PrimitiveOnly: true, 162 Expected: map[string]string{ 163 "value": "foo", 164 }, 165 }, 166 { 167 Input: &containers{ 168 myslice: []int{1, 2}, 169 mymap: map[string]linkedList{ 170 "foo": linkedList{ 171 value: "l1", 172 }, 173 "bar": linkedList{ 174 value: "l2", 175 }, 176 }, 177 }, 178 Expected: map[string]string{ 179 "myslice[0]": "1", 180 "myslice[1]": "2", 181 "mymap[foo].value": "l1", 182 "mymap[foo].next": "nil", 183 "mymap[bar].value": "l2", 184 "mymap[bar].next": "nil", 185 }, 186 }, 187 { 188 Input: &containers{ 189 myslice: []int{1, 2}, 190 mymap: map[string]linkedList{ 191 "foo": linkedList{ 192 value: "l1", 193 }, 194 "bar": linkedList{ 195 value: "l2", 196 }, 197 }, 198 }, 199 PrimitiveOnly: true, 200 Expected: map[string]string{}, 201 }, 202 { 203 Input: &interfaceHolder{ 204 value: &linkedList{ 205 value: "foo", 206 next: nil, 207 }, 208 }, 209 Expected: map[string]string{ 210 "value.value": "foo", 211 "value.next": "nil", 212 }, 213 }, 214 { 215 Input: &interfaceHolder{ 216 value: &linkedList{ 217 value: "foo", 218 next: nil, 219 }, 220 }, 221 PrimitiveOnly: true, 222 Expected: map[string]string{}, 223 }, 224 } 225 226 for i, c := range cases { 227 act := Flatten(c.Input, c.Filter, c.PrimitiveOnly) 228 if !reflect.DeepEqual(act, c.Expected) { 229 t.Fatalf("case %d: got %#v; want %#v", i+1, act, c.Expected) 230 } 231 } 232 }