github.com/searKing/golang/go@v1.2.74/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 TestValueIsZeroValue(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(map[int]string{}), 52 expect: true, 53 }, 54 { 55 a: reflect.ValueOf(interface{}([]byte{})), 56 expect: true, 57 }, 58 { 59 a: reflect.ValueOf(interface{}(map[int]string{})), 60 expect: true, 61 }, 62 { 63 a: reflect.ValueOf(struct{}{}), 64 expect: true, 65 }, 66 } 67 for idx, in := range ins { 68 if IsZeroValue(in.a) != in.expect { 69 t.Errorf("#%d expect %t", idx, in.expect) 70 } 71 } 72 } 73 func TestValueIsNilValue(t *testing.T) { 74 var nilTime *time.Time 75 ins := []inputValue{ 76 { 77 a: reflect.ValueOf(nil), 78 expect: true, 79 }, 80 { 81 a: reflect.ValueOf(true), 82 expect: false, 83 }, 84 { 85 a: reflect.ValueOf(0), 86 expect: false, 87 }, 88 { 89 a: reflect.ValueOf(""), 90 expect: false, 91 }, 92 { 93 a: reflect.ValueOf(time.Now()), 94 expect: false, 95 }, 96 { 97 a: reflect.ValueOf(nilTime), 98 expect: true, 99 }, 100 } 101 for idx, in := range ins { 102 if IsNilValue(in.a) != in.expect { 103 t.Errorf("#%d expect %t", idx, in.expect) 104 } 105 } 106 } 107 108 type inputDumpValue struct { 109 a reflect.Value 110 expect string 111 } 112 113 func TestTypeDumpValueInfoDFS(t *testing.T) { 114 var nilError *json.SyntaxError 115 ins := []inputDumpValue{ 116 { 117 a: reflect.ValueOf(nil), 118 expect: `<invalid value>`, 119 }, 120 { 121 a: reflect.ValueOf(true), 122 expect: `[bool: true]`, 123 }, 124 { 125 a: reflect.ValueOf(0), 126 expect: `[int: 0]`, 127 }, 128 { 129 a: reflect.ValueOf("HelloWorld"), 130 expect: `[string: HelloWorld]`, 131 }, 132 { 133 a: reflect.ValueOf(json.SyntaxError{}), 134 expect: `[json.SyntaxError: {msg: Offset:0}] 135 [string: ] 136 [int64: 0]`, 137 }, 138 { 139 a: reflect.ValueOf(nilError), 140 expect: `[*json.SyntaxError: <nil>]`, 141 }, 142 } 143 for idx, in := range ins { 144 info := DumpValueInfoDFS(in.a) 145 if info != in.expect { 146 t.Errorf("#%d expect\n[\n%s\n]\nactual\n[\n%s\n]", idx, in.expect, info) 147 } 148 } 149 } 150 151 func TestTypeDumpValueInfoBFS(t *testing.T) { 152 var nilError *json.SyntaxError 153 ins := []inputDumpValue{ 154 { 155 a: reflect.ValueOf(nil), 156 expect: `<invalid value>`, 157 }, 158 { 159 a: reflect.ValueOf(true), 160 expect: `[bool: true]`, 161 }, 162 { 163 a: reflect.ValueOf(0), 164 expect: `[int: 0]`, 165 }, 166 { 167 a: reflect.ValueOf(""), 168 expect: `[string: ]`, 169 }, 170 { 171 a: reflect.ValueOf(json.SyntaxError{}), 172 expect: `[json.SyntaxError: {msg: Offset:0}] 173 [string: ] 174 [int64: 0]`, 175 }, 176 { 177 a: reflect.ValueOf(nilError), 178 expect: `[*json.SyntaxError: <nil>]`, 179 }, 180 } 181 for idx, in := range ins { 182 info := DumpValueInfoBFS(in.a) 183 if info != in.expect { 184 t.Errorf("#%d expect\n[\n%s\n]\nactual\n[\n%s\n]", idx, in.expect, info) 185 } 186 } 187 } 188 189 func TestDumpValueInfoBFS(t *testing.T) { 190 str := "HelloWorld" 191 s := &str 192 ss := &s 193 valueS := reflect.ValueOf(ss) 194 indirectValueS := reflect.Indirect(valueS) 195 fmt.Printf("valueS: %s\n", valueS.Kind().String()) 196 fmt.Printf("indirect valueS: %s\n", indirectValueS.Kind().String()) 197 }