github.com/containerd/nerdctl@v1.7.7/pkg/containerutil/container_network_manager_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 containerutil 18 19 import ( 20 "fmt" 21 "testing" 22 23 "gotest.tools/v3/assert" 24 ) 25 26 func TestZeroMapValues(t *testing.T) { 27 emptyString := "" 28 testCases := []struct { 29 key string 30 value interface{} 31 shouldBeZero bool 32 }{ 33 { 34 key: "false", 35 value: false, 36 shouldBeZero: true, 37 }, 38 { 39 key: "true", 40 value: true, 41 shouldBeZero: false, 42 }, 43 { 44 key: "zeroInt", 45 value: int(0), 46 shouldBeZero: true, 47 }, 48 { 49 key: "nonZeroInt", 50 value: int(1), 51 shouldBeZero: false, 52 }, 53 { 54 key: "zeroString", 55 value: "", 56 shouldBeZero: true, 57 }, 58 { 59 key: "nonZeroString", 60 value: "non-zero", 61 shouldBeZero: false, 62 }, 63 { 64 key: "nilPointer", 65 value: (*string)(nil), 66 shouldBeZero: true, 67 }, 68 { 69 key: "pointerToEmpty", 70 value: &emptyString, 71 // technically just a nil pointer check, so any value should be non-Zero: 72 shouldBeZero: false, 73 }, 74 { 75 key: "nilSlice", 76 value: []string(nil), 77 shouldBeZero: true, 78 }, 79 { 80 key: "emptySlice", 81 value: []string{}, 82 shouldBeZero: false, 83 }, 84 { 85 key: "nonEmptySlice", 86 value: []string{"non-empty"}, 87 shouldBeZero: false, 88 }, 89 { 90 key: "nilMap", 91 value: map[string]int(nil), 92 shouldBeZero: true, 93 }, 94 { 95 key: "emptyMap", 96 value: map[string]int{}, 97 shouldBeZero: false, 98 }, 99 { 100 key: "nonEmptyMap", 101 value: map[string]int{"non-empty": 42}, 102 shouldBeZero: false, 103 }, 104 } 105 106 for _, tc := range testCases { 107 testName := fmt.Sprintf("%s=%t", tc.key, tc.shouldBeZero) 108 t.Run(testName, func(tt *testing.T) { 109 result := nonZeroMapValues(map[string]interface{}{tc.key: tc.value}) 110 if tc.shouldBeZero { 111 assert.Equal(tt, len(result), 0) 112 } else { 113 assert.Equal(tt, len(result), 1) 114 } 115 }) 116 } 117 }