github.com/docker/libcompose@v0.4.1-0.20210616120443-2a046c0bdbf2/utils/util_test.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 type jsonfrom struct { 11 Element1 string `json:"element2"` 12 Element2 int `json:"element1"` 13 } 14 type jsonto struct { 15 Elt1 int `json:"element1"` 16 Elt2 string `json:"element2"` 17 Elt3 int 18 } 19 20 func TestConvertByJSON(t *testing.T) { 21 valids := []struct { 22 src jsonfrom 23 expected jsonto 24 }{ 25 { 26 jsonfrom{Element2: 1}, 27 jsonto{1, "", 0}, 28 }, 29 { 30 jsonfrom{}, 31 jsonto{0, "", 0}, 32 }, 33 { 34 jsonfrom{"element1", 2}, 35 jsonto{2, "element1", 0}, 36 }, 37 } 38 for _, valid := range valids { 39 var target jsonto 40 err := ConvertByJSON(valid.src, &target) 41 if err != nil || target.Elt1 != valid.expected.Elt1 || target.Elt2 != valid.expected.Elt2 || target.Elt3 != 0 { 42 t.Fatalf("Expected %v from %v got %v, %v", valid.expected, valid.src, target, err) 43 } 44 } 45 } 46 47 func TestConvertByJSONInvalid(t *testing.T) { 48 invalids := []interface{}{ 49 // Incompatible struct 50 struct { 51 Element1 int `json:"element2"` 52 Element2 string `json:"element1"` 53 }{1, "element1"}, 54 // Not marshable struct 55 struct { 56 Element1 func(int) int 57 }{ 58 func(i int) int { return 0 }, 59 }, 60 } 61 for _, invalid := range invalids { 62 var target jsonto 63 if err := ConvertByJSON(invalid, &target); err == nil { 64 t.Fatalf("Expected an error converting %v to %v, got nothing", invalid, target) 65 } 66 } 67 } 68 69 type yamlfrom struct { 70 Element1 string `yaml:"element2"` 71 Element2 int `yaml:"element1"` 72 } 73 type yamlto struct { 74 Elt1 int `yaml:"element1"` 75 Elt2 string `yaml:"element2"` 76 Elt3 int 77 } 78 79 func TestConvert(t *testing.T) { 80 valids := []struct { 81 src yamlfrom 82 expected yamlto 83 }{ 84 { 85 yamlfrom{Element2: 1}, 86 yamlto{1, "", 0}, 87 }, 88 { 89 yamlfrom{}, 90 yamlto{0, "", 0}, 91 }, 92 { 93 yamlfrom{"element1", 2}, 94 yamlto{2, "element1", 0}, 95 }, 96 } 97 for _, valid := range valids { 98 var target yamlto 99 err := Convert(valid.src, &target) 100 if err != nil || target.Elt1 != valid.expected.Elt1 || target.Elt2 != valid.expected.Elt2 || target.Elt3 != 0 { 101 t.Fatalf("Expected %v from %v got %v, %v", valid.expected, valid.src, target, err) 102 } 103 } 104 } 105 106 func TestConvertInvalid(t *testing.T) { 107 invalids := []interface{}{ 108 // Incompatible struct 109 struct { 110 Element1 int `yaml:"element2"` 111 Element2 string `yaml:"element1"` 112 }{1, "element1"}, 113 // Not marshable struct 114 // This one panics :-| 115 // struct { 116 // Element1 func(int) int 117 // }{ 118 // func(i int) int { return 0 }, 119 // }, 120 } 121 for _, invalid := range invalids { 122 var target yamlto 123 if err := Convert(invalid, &target); err == nil { 124 t.Fatalf("Expected an error converting %v to %v, got nothing", invalid, target) 125 } 126 } 127 } 128 129 func TestFilterStringSet(t *testing.T) { 130 s := map[string]bool{"abcd": true, "b": true, "cde": true, "d": true, "ef": true} 131 expected := map[string]bool{"abcd": true, "cde": true} 132 result := FilterStringSet(s, func(x string) bool { return len(x) > 2 }) 133 assert.Equal(t, expected, result) 134 } 135 136 func TestFilterString(t *testing.T) { 137 datas := []struct { 138 value map[string][]string 139 expected string 140 }{ 141 { 142 map[string][]string{}, 143 "{}", 144 }, 145 { 146 map[string][]string{ 147 "key": {}, 148 }, 149 `{"key":[]}`, 150 }, 151 { 152 map[string][]string{ 153 "key": {"value1", "value2"}, 154 }, 155 `{"key":["value1","value2"]}`, 156 }, 157 { 158 map[string][]string{ 159 "key1": {"value1", "value2"}, 160 "key2": {"value3", "value4"}, 161 }, 162 `{"key1":["value1","value2"],"key2":["value3","value4"]}`, 163 }, 164 } 165 for _, data := range datas { 166 actual := FilterString(data.value) 167 if actual != data.expected { 168 t.Fatalf("Expected '%v' for %v, got '%v'", data.expected, data.value, actual) 169 } 170 } 171 } 172 173 func TestContains(t *testing.T) { 174 cases := []struct { 175 collection []string 176 key string 177 contains bool 178 }{ 179 { 180 []string{}, "", false, 181 }, 182 { 183 []string{""}, "", true, 184 }, 185 { 186 []string{"value1", "value2"}, "value3", false, 187 }, 188 { 189 []string{"value1", "value2"}, "value1", true, 190 }, 191 { 192 []string{"value1", "value2"}, "value2", true, 193 }, 194 } 195 for _, element := range cases { 196 actual := Contains(element.collection, element.key) 197 if actual != element.contains { 198 t.Fatalf("Expected contains to be %v for %v in %v, but was %v", element.contains, element.key, element.collection, actual) 199 } 200 } 201 } 202 203 func TestMerge(t *testing.T) { 204 a := []string{"a", "b", "c"} 205 b := []string{"b", "c", "d", "e"} 206 expected := []string{"a", "b", "c", "d", "e"} 207 r := Merge(a, b) 208 assert.Equal(t, len(expected), len(r)) 209 for _, v := range expected { 210 assert.True(t, Contains(r, v)) 211 } 212 assert.Equal(t, "a:b", fmt.Sprint("a", ":", "b")) 213 }