github.com/searKing/golang/go@v1.2.117/reflect/value_test.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package reflect 6 7 import ( 8 "encoding/json" 9 "fmt" 10 "reflect" 11 "testing" 12 "time" 13 ) 14 15 type inputValue struct { 16 a reflect.Value 17 expect bool 18 } 19 20 func TestIsEmptyValue(t *testing.T) { 21 ins := []inputValue{ 22 { 23 a: reflect.ValueOf(nil), 24 expect: true, 25 }, 26 { 27 a: reflect.ValueOf(true), 28 expect: false, 29 }, 30 { 31 a: reflect.ValueOf(0), 32 expect: true, 33 }, 34 { 35 a: reflect.ValueOf(""), 36 expect: true, 37 }, 38 { 39 a: reflect.ValueOf(time.Now()), 40 expect: false, 41 }, 42 { 43 a: reflect.ValueOf(time.Time{}), 44 expect: true, 45 }, 46 { 47 a: reflect.ValueOf([]byte{}), 48 expect: true, 49 }, 50 { 51 a: reflect.ValueOf([]byte(nil)), 52 expect: true, 53 }, 54 { 55 a: reflect.ValueOf(map[int]string{}), 56 expect: true, 57 }, 58 { 59 a: reflect.ValueOf(map[int]string(nil)), 60 expect: true, 61 }, 62 { 63 a: reflect.ValueOf(any([]byte{})), 64 expect: true, 65 }, 66 { 67 a: reflect.ValueOf(any([]byte(nil))), 68 expect: true, 69 }, 70 { 71 a: reflect.ValueOf(any(map[int]string{})), 72 expect: true, 73 }, 74 { 75 a: reflect.ValueOf(any(map[int]string(nil))), 76 expect: true, 77 }, 78 { 79 a: reflect.ValueOf(struct{}{}), 80 expect: true, 81 }, 82 } 83 for idx, in := range ins { 84 if IsEmptyValue(in.a) != in.expect { 85 t.Errorf("#%d expect %t", idx, in.expect) 86 } 87 } 88 } 89 90 func TestIsZeroValue(t *testing.T) { 91 ins := []inputValue{ 92 { 93 a: reflect.ValueOf(nil), 94 expect: true, 95 }, 96 { 97 a: reflect.ValueOf(true), 98 expect: false, 99 }, 100 { 101 a: reflect.ValueOf(0), 102 expect: true, 103 }, 104 { 105 a: reflect.ValueOf(1), 106 expect: false, 107 }, 108 { 109 a: reflect.ValueOf(""), 110 expect: true, 111 }, 112 { 113 a: reflect.ValueOf(time.Now()), 114 expect: false, 115 }, 116 { 117 a: reflect.ValueOf(time.Time{}), 118 expect: true, 119 }, 120 { 121 a: reflect.ValueOf([]byte{}), 122 expect: false, 123 }, 124 { 125 a: reflect.ValueOf([]byte(nil)), 126 expect: true, 127 }, 128 { 129 a: reflect.ValueOf(map[int]string{}), 130 expect: false, 131 }, 132 { 133 a: reflect.ValueOf(map[int]string(nil)), 134 expect: true, 135 }, 136 { 137 a: reflect.ValueOf(any([]byte{})), 138 expect: false, 139 }, 140 { 141 a: reflect.ValueOf(any([]byte(nil))), 142 expect: true, 143 }, 144 { 145 a: reflect.ValueOf(any(map[int]string{})), 146 expect: false, 147 }, 148 { 149 a: reflect.ValueOf(any(map[int]string(nil))), 150 expect: true, 151 }, 152 { 153 a: reflect.ValueOf(struct{}{}), 154 expect: true, 155 }, 156 } 157 for idx, in := range ins { 158 if IsZeroValue(in.a) != in.expect { 159 t.Errorf("#%d expect %t", idx, in.expect) 160 } 161 } 162 } 163 164 func TestIsNilValue(t *testing.T) { 165 var nilTime *time.Time 166 ins := []inputValue{ 167 { 168 a: reflect.ValueOf(nil), 169 expect: true, 170 }, 171 { 172 a: reflect.ValueOf(true), 173 expect: false, 174 }, 175 { 176 a: reflect.ValueOf(0), 177 expect: false, 178 }, 179 { 180 a: reflect.ValueOf(""), 181 expect: false, 182 }, 183 { 184 a: reflect.ValueOf(time.Now()), 185 expect: false, 186 }, 187 { 188 a: reflect.ValueOf(nilTime), // typed nil 189 expect: true, 190 }, 191 } 192 for idx, in := range ins { 193 if IsNilValue(in.a) != in.expect { 194 t.Errorf("#%d expect %t", idx, in.expect) 195 } 196 } 197 } 198 199 type inputDumpValue struct { 200 a reflect.Value 201 expect string 202 } 203 204 func TestTypeDumpValueInfoDFS(t *testing.T) { 205 var nilError *json.SyntaxError 206 ins := []inputDumpValue{ 207 { 208 a: reflect.ValueOf(nil), 209 expect: `<invalid value>`, 210 }, 211 { 212 a: reflect.ValueOf(true), 213 expect: `[bool: true]`, 214 }, 215 { 216 a: reflect.ValueOf(0), 217 expect: `[int: 0]`, 218 }, 219 { 220 a: reflect.ValueOf("HelloWorld"), 221 expect: `[string: HelloWorld]`, 222 }, 223 { 224 a: reflect.ValueOf(json.SyntaxError{}), 225 expect: `[json.SyntaxError: {msg: Offset:0}] 226 [string: ] 227 [int64: 0]`, 228 }, 229 { 230 a: reflect.ValueOf(nilError), 231 expect: `[*json.SyntaxError: <nil>]`, 232 }, 233 } 234 for idx, in := range ins { 235 info := DumpValueInfoDFS(in.a) 236 if info != in.expect { 237 t.Errorf("#%d expect\n[\n%s\n]\nactual\n[\n%s\n]", idx, in.expect, info) 238 } 239 } 240 } 241 242 func TestTypeDumpValueInfoBFS(t *testing.T) { 243 var nilError *json.SyntaxError 244 ins := []inputDumpValue{ 245 { 246 a: reflect.ValueOf(nil), 247 expect: `<invalid value>`, 248 }, 249 { 250 a: reflect.ValueOf(true), 251 expect: `[bool: true]`, 252 }, 253 { 254 a: reflect.ValueOf(0), 255 expect: `[int: 0]`, 256 }, 257 { 258 a: reflect.ValueOf(""), 259 expect: `[string: ]`, 260 }, 261 { 262 a: reflect.ValueOf(json.SyntaxError{}), 263 expect: `[json.SyntaxError: {msg: Offset:0}] 264 [string: ] 265 [int64: 0]`, 266 }, 267 { 268 a: reflect.ValueOf(nilError), 269 expect: `[*json.SyntaxError: <nil>]`, 270 }, 271 } 272 for idx, in := range ins { 273 info := DumpValueInfoBFS(in.a) 274 if info != in.expect { 275 t.Errorf("#%d expect\n[\n%s\n]\nactual\n[\n%s\n]", idx, in.expect, info) 276 } 277 } 278 } 279 280 func TestDumpValueInfoBFS(t *testing.T) { 281 str := "HelloWorld" 282 s := &str 283 ss := &s 284 valueS := reflect.ValueOf(ss) 285 indirectValueS := reflect.Indirect(valueS) 286 fmt.Printf("valueS: %s\n", valueS.Kind().String()) 287 fmt.Printf("indirect valueS: %s\n", indirectValueS.Kind().String()) 288 }