github.com/containerd/nerdctl@v1.7.7/pkg/strutil/strutil_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package strutil 18 19 import ( 20 "reflect" 21 "testing" 22 23 "gotest.tools/v3/assert" 24 ) 25 26 func TestDedupeStrSlice(t *testing.T) { 27 assert.DeepEqual(t, 28 []string{"apple", "banana", "chocolate"}, 29 DedupeStrSlice([]string{"apple", "banana", "apple", "chocolate"})) 30 31 assert.DeepEqual(t, 32 []string{"apple", "banana", "chocolate"}, 33 DedupeStrSlice([]string{"apple", "apple", "banana", "chocolate", "apple"})) 34 35 } 36 37 func TestSliceToSet(t *testing.T) { 38 assert.DeepEqual(t, 39 map[string]bool{"apple": true, "banana": true, "chocolate": true}, 40 SliceToSet([]string{"apple", "banana", "apple", "chocolate"})) 41 42 assert.DeepEqual(t, 43 map[string]bool{"apple": true, "banana": true, "chocolate": true}, 44 SliceToSet([]string{"apple", "apple", "banana", "chocolate", "apple"})) 45 46 } 47 48 func TestTrimStrSliceRight(t *testing.T) { 49 assert.DeepEqual(t, 50 []string{"foo", "bar", "baz"}, 51 TrimStrSliceRight([]string{"foo", "bar", "baz", "qux", "quux"}, []string{"qux", "quux"})) 52 assert.DeepEqual(t, 53 []string{"foo", "bar", "baz", "qux", "quux"}, 54 TrimStrSliceRight([]string{"foo", "bar", "baz", "qux", "quux"}, []string{"bar", "baz"})) 55 assert.DeepEqual(t, 56 []string{}, 57 TrimStrSliceRight([]string{"foo", "bar", "baz", "qux", "quux"}, []string{"foo", "bar", "baz", "qux", "quux"})) 58 assert.DeepEqual(t, 59 []string{"foo", "bar", "baz", "qux", "quux"}, 60 TrimStrSliceRight([]string{"foo", "bar", "baz", "qux", "quux"}, []string{})) 61 assert.DeepEqual(t, 62 []string{"foo", "bar", "baz", "qux", "quux"}, 63 TrimStrSliceRight([]string{"foo", "bar", "baz", "qux", "quux"}, []string{"aaa"})) 64 assert.DeepEqual(t, 65 []string{"foo", "bar", "baz", "qux"}, 66 TrimStrSliceRight([]string{"foo", "bar", "baz", "qux", "quux"}, []string{"quux"})) 67 68 } 69 70 func TestReverseStrSlice(t *testing.T) { 71 assert.DeepEqual(t, 72 []string{"foo", "bar", "baz"}, 73 ReverseStrSlice([]string{"baz", "bar", "foo"})) 74 } 75 76 func TestParseBoolOrAuto(t *testing.T) { 77 var xtrue = true 78 var xfalse = false 79 80 type args struct { 81 s string 82 } 83 tests := []struct { 84 name string 85 args args 86 want *bool 87 wantErr bool 88 }{ 89 { 90 name: "normal-1", 91 args: args{ 92 s: "true", 93 }, 94 want: &xtrue, 95 wantErr: false, 96 }, 97 { 98 name: "normal-2", 99 args: args{ 100 s: "false", 101 }, 102 want: &xfalse, 103 wantErr: false, 104 }, 105 { 106 name: "blank", 107 args: args{ 108 s: "", 109 }, 110 want: nil, 111 wantErr: false, 112 }, 113 { 114 name: "auto", 115 args: args{ 116 s: "auto", 117 }, 118 want: nil, 119 wantErr: false, 120 }, 121 } 122 for _, tt := range tests { 123 t.Run(tt.name, func(t *testing.T) { 124 got, err := ParseBoolOrAuto(tt.args.s) 125 if (err != nil) != tt.wantErr { 126 t.Errorf("ParseBoolOrAuto() error = %v, wantErr %v", err, tt.wantErr) 127 return 128 } 129 if tt.want != nil { 130 if *got != *tt.want { 131 t.Errorf("ParseBoolOrAuto() = %v, want %v", got, tt.want) 132 } 133 } else if got != nil { 134 t.Errorf("ParseBoolOrAuto() = %v, want %v", got, nil) 135 } 136 }) 137 } 138 } 139 140 func TestConvertKVStringsToMap(t *testing.T) { 141 type args struct { 142 values []string 143 } 144 tests := []struct { 145 name string 146 args args 147 want map[string]string 148 }{ 149 { 150 name: "normal", 151 args: args{ 152 values: []string{"foo=bar", "baz=qux"}, 153 }, 154 want: map[string]string{ 155 "foo": "bar", 156 "baz": "qux", 157 }, 158 }, 159 { 160 name: "normal-1", 161 args: args{ 162 values: []string{"foo"}, 163 }, 164 want: map[string]string{ 165 "foo": "", 166 }, 167 }, 168 { 169 name: "normal-2", 170 args: args{ 171 values: []string{"foo=bar=baz"}, 172 }, 173 want: map[string]string{ 174 "foo": "bar=baz", 175 }, 176 }, 177 { 178 name: "empty", 179 args: args{ 180 values: []string{}, 181 }, 182 want: map[string]string{}, 183 }, 184 } 185 for _, tt := range tests { 186 t.Run(tt.name, func(t *testing.T) { 187 if got := ConvertKVStringsToMap(tt.args.values); !reflect.DeepEqual(got, tt.want) { 188 t.Errorf("ConvertKVStringsToMap() = %v, want %v", got, tt.want) 189 } 190 }) 191 } 192 } 193 194 func TestInStringSlice(t *testing.T) { 195 type args struct { 196 ss []string 197 str string 198 } 199 tests := []struct { 200 name string 201 args args 202 want bool 203 }{ 204 { 205 name: "normal", 206 args: args{ 207 ss: []string{"foo", "bar", "baz"}, 208 str: "bar", 209 }, 210 want: true, 211 }, 212 { 213 name: "normal-1", 214 args: args{ 215 ss: []string{"foo", "bar", "baz"}, 216 str: "qux", 217 }, 218 want: false, 219 }, 220 } 221 for _, tt := range tests { 222 t.Run(tt.name, func(t *testing.T) { 223 if got := InStringSlice(tt.args.ss, tt.args.str); got != tt.want { 224 t.Errorf("InStringSlice() = %v, want %v", got, tt.want) 225 } 226 }) 227 } 228 } 229 230 func TestParseCSVMap(t *testing.T) { 231 type args struct { 232 s string 233 } 234 tests := []struct { 235 name string 236 args args 237 want map[string]string 238 wantErr bool 239 }{ 240 { 241 name: "normal", 242 args: args{ 243 s: "foo=x,bar=y,baz=z,qux", 244 }, 245 want: map[string]string{ 246 "foo": "x", 247 "bar": "y", 248 "baz": "z", 249 "qux": "", 250 }, 251 wantErr: false, 252 }, 253 { 254 name: "normal-1", 255 args: args{ 256 s: "\"foo=x,bar=y\",baz=z,qux", 257 }, 258 want: map[string]string{ 259 "foo": "x,bar=y", 260 "baz": "z", 261 "qux": "", 262 }, 263 wantErr: false, 264 }, 265 { 266 name: "normal-2", 267 args: args{ 268 s: "foo=\"x,bar=y,baz=\"z\",qux", 269 }, 270 want: map[string]string{ 271 "foo": "\"x", 272 "bar": "y", 273 "baz": "\"z\"", 274 "qux": "", 275 }, 276 wantErr: false, 277 }, 278 { 279 name: "invalid", 280 args: args{ 281 s: "sssssss\nsss", 282 }, 283 want: nil, 284 wantErr: true, 285 }, 286 { 287 name: "invalid-1", 288 args: args{ 289 s: "sssssss\n\"\nsss", 290 }, 291 want: nil, 292 wantErr: true, 293 }, 294 } 295 for _, tt := range tests { 296 t.Run(tt.name, func(t *testing.T) { 297 got, err := ParseCSVMap(tt.args.s) 298 if (err != nil) != tt.wantErr { 299 t.Errorf("ParseCSVMap() error = %v, wantErr %v", err, tt.wantErr) 300 return 301 } 302 if !reflect.DeepEqual(got, tt.want) { 303 t.Errorf("ParseCSVMap() = %v, want %v", got, tt.want) 304 } 305 }) 306 } 307 }