github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/common/assert/assert_test.go (about) 1 // Copyright 2020 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package assert 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "path" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestGreaterThanOrEqualTo(t *testing.T) { 27 tests := []struct { 28 value int 29 limit int 30 expectPanic bool 31 }{ 32 { 33 value: 0, 34 limit: 0, 35 }, 36 { 37 value: 100, 38 limit: 0, 39 }, 40 { 41 value: 0, 42 limit: 1, 43 expectPanic: true, 44 }, 45 } 46 for _, tt := range tests { 47 t.Run(fmt.Sprintf("%d >= %d", tt.value, tt.limit), func(t *testing.T) { 48 if tt.expectPanic { 49 assert.PanicsWithValue(t, fmt.Sprintf("Expected %d >= %d", tt.value, tt.limit), func() { 50 GreaterThanOrEqualTo(tt.value, tt.limit) 51 }) 52 } else { 53 GreaterThanOrEqualTo(tt.value, tt.limit) 54 } 55 }) 56 } 57 } 58 59 func TestContains(t *testing.T) { 60 tests := []struct { 61 element string 62 arr []string 63 expectPanic bool 64 }{ 65 { 66 element: "", 67 arr: []string{""}, 68 }, 69 { 70 element: "", 71 arr: []string{}, 72 expectPanic: true, 73 }, 74 { 75 element: "item", 76 arr: []string{}, 77 expectPanic: true, 78 }, 79 { 80 element: "item", 81 arr: []string{"item"}, 82 }, 83 { 84 element: "item", 85 arr: []string{"one", "item"}, 86 }, 87 { 88 element: "item", 89 arr: []string{"item", "one"}, 90 }, 91 } 92 for _, tt := range tests { 93 t.Run(fmt.Sprintf("%v contains %s", tt.arr, tt.element), func(t *testing.T) { 94 if tt.expectPanic { 95 assert.PanicsWithValue(t, fmt.Sprintf("%s is not a member of %v", tt.element, tt.arr), func() { 96 Contains(tt.element, tt.arr) 97 }) 98 } else { 99 Contains(tt.element, tt.arr) 100 } 101 }) 102 } 103 } 104 105 func TestDirectoryExists(t *testing.T) { 106 tmpDir, err := ioutil.TempDir("", "") 107 assert.NoError(t, err) 108 tmpFileObj, err := ioutil.TempFile("", "*.txt") 109 assert.NoError(t, err) 110 tmpFile := tmpFileObj.Name() 111 tests := []struct { 112 name string 113 dir string 114 expectPanic string 115 }{ 116 { 117 name: "Don't panic when directory exists", 118 dir: tmpDir, 119 }, 120 { 121 name: "Panic when dir doesn't exist", 122 dir: path.Join(tmpDir, "dir-doesn't-exist"), 123 expectPanic: fmt.Sprintf("%s/dir-doesn't-exist: Directory not found", tmpDir), 124 }, 125 { 126 name: "Panic when dir is a file", 127 dir: tmpFile, 128 expectPanic: fmt.Sprintf("%v: Directory not found", tmpFile), 129 }, 130 } 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 if tt.expectPanic != "" { 134 assert.PanicsWithValue(t, tt.expectPanic, func() { 135 DirectoryExists(tt.dir) 136 }) 137 } else { 138 DirectoryExists(tt.dir) 139 } 140 }) 141 } 142 } 143 144 func TestNotEmpty(t *testing.T) { 145 tests := []struct { 146 name string 147 obj interface{} 148 expectPanic bool 149 }{ 150 { 151 obj: "s", 152 name: "pass when non-empty string", 153 }, 154 { 155 obj: struct{ s string }{s: "hi"}, 156 name: "pass when non-default struct", 157 }, 158 { 159 obj: map[string]string{"hi": "there"}, 160 name: "pass when non-empty map", 161 }, 162 { 163 obj: [1]string{"hi"}, 164 name: "pass when non-empty array", 165 }, { 166 obj: []string{"hi"}, 167 name: "pass when non-empty slice", 168 }, 169 { 170 obj: nil, 171 name: "panic when object is untypped nil", 172 expectPanic: true, 173 }, 174 { 175 obj: "", 176 name: "pass when empty string", 177 expectPanic: true, 178 }, 179 { 180 obj: map[string]string{}, 181 name: "panic when object is empty map", 182 expectPanic: true, 183 }, 184 { 185 obj: map[string]map[string]string{}, 186 name: "panic when object is empty nested map", 187 expectPanic: true, 188 }, 189 { 190 obj: [0]string{}, 191 name: "panic when object is empty array", 192 expectPanic: true, 193 }, { 194 obj: []string{}, 195 name: "panic when object is empty slice", 196 expectPanic: true, 197 }, 198 { 199 obj: [][]string{}, 200 name: "panic when object is empty nested slice", 201 expectPanic: true, 202 }, 203 { 204 obj: struct{ s string }{}, 205 name: "panic when default struct", 206 expectPanic: true, 207 }, 208 } 209 for _, tt := range tests { 210 t.Run(tt.name, func(t *testing.T) { 211 if tt.expectPanic { 212 assert.PanicsWithValue(t, "Expected non-empty value", func() { 213 NotEmpty(tt.obj) 214 }) 215 } else { 216 NotEmpty(tt.obj) 217 } 218 }) 219 } 220 }