github.com/containerd/nerdctl@v1.7.7/pkg/maputil/maputil_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 maputil 18 19 import ( 20 "fmt" 21 "testing" 22 23 "gotest.tools/v3/assert" 24 ) 25 26 func TestMapBoolValueAsOpt(t *testing.T) { 27 tests := []struct { 28 m map[string]string 29 key string 30 want bool 31 wantErr bool 32 }{ 33 // cases that key does not exist 34 { 35 m: map[string]string{}, 36 key: "key", 37 want: false, 38 wantErr: false, 39 }, 40 // key exist, but has no value 41 { 42 m: map[string]string{"key": ""}, 43 key: "key", 44 want: true, 45 wantErr: false, 46 }, 47 // key exist, and set to true 48 { 49 m: map[string]string{"key": "true"}, 50 key: "key", 51 want: true, 52 wantErr: false, 53 }, 54 // key exist, and set to false 55 { 56 m: map[string]string{"key": "false"}, 57 key: "key", 58 want: false, 59 wantErr: false, 60 }, 61 // cases with error 62 { 63 m: map[string]string{"key": "abc"}, 64 key: "key", 65 want: false, 66 wantErr: true, 67 }, 68 } 69 70 for i, tt := range tests { 71 got, err := MapBoolValueAsOpt(tt.m, tt.key) 72 assert.Equal(t, got, tt.want, fmt.Sprintf("case %d", (i+1))) 73 if (err != nil) != tt.wantErr { 74 t.Errorf("MapBoolValueAsOpt() case %d error = %v, wantErr %v", (i + 1), err, tt.wantErr) 75 } 76 } 77 }