github.com/wangyougui/gf/v2@v2.6.5/container/gvar/gvar_z_unit_basic_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gvar_test 8 9 import ( 10 "bytes" 11 "encoding/binary" 12 "testing" 13 "time" 14 15 "github.com/wangyougui/gf/v2/container/gvar" 16 "github.com/wangyougui/gf/v2/frame/g" 17 "github.com/wangyougui/gf/v2/test/gtest" 18 "github.com/wangyougui/gf/v2/util/gconv" 19 ) 20 21 func Test_Set(t *testing.T) { 22 gtest.C(t, func(t *gtest.T) { 23 var v gvar.Var 24 v.Set(123.456) 25 t.Assert(v.Val(), 123.456) 26 }) 27 gtest.C(t, func(t *gtest.T) { 28 var v gvar.Var 29 v.Set(123.456) 30 t.Assert(v.Val(), 123.456) 31 }) 32 33 gtest.C(t, func(t *gtest.T) { 34 objOne := gvar.New("old", true) 35 objOneOld, _ := objOne.Set("new").(string) 36 t.Assert(objOneOld, "old") 37 38 objTwo := gvar.New("old", false) 39 objTwoOld, _ := objTwo.Set("new").(string) 40 t.Assert(objTwoOld, "old") 41 }) 42 } 43 44 func Test_Val(t *testing.T) { 45 gtest.C(t, func(t *gtest.T) { 46 objOne := gvar.New(1, true) 47 objOneOld, _ := objOne.Val().(int) 48 t.Assert(objOneOld, 1) 49 50 objTwo := gvar.New(1, false) 51 objTwoOld, _ := objTwo.Val().(int) 52 t.Assert(objTwoOld, 1) 53 54 objOne = nil 55 t.Assert(objOne.Val(), nil) 56 }) 57 } 58 59 func Test_Interface(t *testing.T) { 60 gtest.C(t, func(t *gtest.T) { 61 objOne := gvar.New(1, true) 62 objOneOld, _ := objOne.Interface().(int) 63 t.Assert(objOneOld, 1) 64 65 objTwo := gvar.New(1, false) 66 objTwoOld, _ := objTwo.Interface().(int) 67 t.Assert(objTwoOld, 1) 68 }) 69 } 70 71 func Test_IsNil(t *testing.T) { 72 gtest.C(t, func(t *gtest.T) { 73 objOne := gvar.New(nil, true) 74 t.Assert(objOne.IsNil(), true) 75 76 objTwo := gvar.New("noNil", false) 77 t.Assert(objTwo.IsNil(), false) 78 79 }) 80 } 81 82 func Test_Bytes(t *testing.T) { 83 gtest.C(t, func(t *gtest.T) { 84 x := int32(1) 85 bytesBuffer := bytes.NewBuffer([]byte{}) 86 binary.Write(bytesBuffer, binary.BigEndian, x) 87 88 objOne := gvar.New(bytesBuffer.Bytes(), true) 89 90 bBuf := bytes.NewBuffer(objOne.Bytes()) 91 var y int32 92 binary.Read(bBuf, binary.BigEndian, &y) 93 94 t.Assert(x, y) 95 96 }) 97 } 98 99 func Test_String(t *testing.T) { 100 gtest.C(t, func(t *gtest.T) { 101 var str string = "hello" 102 objOne := gvar.New(str, true) 103 t.Assert(objOne.String(), str) 104 105 }) 106 } 107 108 func Test_Bool(t *testing.T) { 109 gtest.C(t, func(t *gtest.T) { 110 var ok bool = true 111 objOne := gvar.New(ok, true) 112 t.Assert(objOne.Bool(), ok) 113 114 ok = false 115 objTwo := gvar.New(ok, true) 116 t.Assert(objTwo.Bool(), ok) 117 118 }) 119 } 120 121 func Test_Int(t *testing.T) { 122 gtest.C(t, func(t *gtest.T) { 123 var num int = 1 124 objOne := gvar.New(num, true) 125 t.Assert(objOne.Int(), num) 126 127 }) 128 } 129 130 func Test_Int8(t *testing.T) { 131 gtest.C(t, func(t *gtest.T) { 132 var num int8 = 1 133 objOne := gvar.New(num, true) 134 t.Assert(objOne.Int8(), num) 135 136 }) 137 } 138 139 func Test_Int16(t *testing.T) { 140 gtest.C(t, func(t *gtest.T) { 141 var num int16 = 1 142 objOne := gvar.New(num, true) 143 t.Assert(objOne.Int16(), num) 144 145 }) 146 } 147 148 func Test_Int32(t *testing.T) { 149 gtest.C(t, func(t *gtest.T) { 150 var num int32 = 1 151 objOne := gvar.New(num, true) 152 t.Assert(objOne.Int32(), num) 153 154 }) 155 } 156 157 func Test_Int64(t *testing.T) { 158 gtest.C(t, func(t *gtest.T) { 159 var num int64 = 1 160 objOne := gvar.New(num, true) 161 t.Assert(objOne.Int64(), num) 162 163 }) 164 } 165 166 func Test_Uint(t *testing.T) { 167 gtest.C(t, func(t *gtest.T) { 168 var num uint = 1 169 objOne := gvar.New(num, true) 170 t.Assert(objOne.Uint(), num) 171 172 }) 173 } 174 175 func Test_Uint8(t *testing.T) { 176 gtest.C(t, func(t *gtest.T) { 177 var num uint8 = 1 178 objOne := gvar.New(num, true) 179 t.Assert(objOne.Uint8(), num) 180 181 }) 182 } 183 184 func Test_Uint16(t *testing.T) { 185 gtest.C(t, func(t *gtest.T) { 186 var num uint16 = 1 187 objOne := gvar.New(num, true) 188 t.Assert(objOne.Uint16(), num) 189 190 }) 191 } 192 193 func Test_Uint32(t *testing.T) { 194 gtest.C(t, func(t *gtest.T) { 195 var num uint32 = 1 196 objOne := gvar.New(num, true) 197 t.Assert(objOne.Uint32(), num) 198 199 }) 200 } 201 202 func Test_Uint64(t *testing.T) { 203 gtest.C(t, func(t *gtest.T) { 204 var num uint64 = 1 205 objOne := gvar.New(num, true) 206 t.Assert(objOne.Uint64(), num) 207 208 }) 209 } 210 211 func Test_Float32(t *testing.T) { 212 gtest.C(t, func(t *gtest.T) { 213 var num float32 = 1.1 214 objOne := gvar.New(num, true) 215 t.Assert(objOne.Float32(), num) 216 217 }) 218 } 219 220 func Test_Float64(t *testing.T) { 221 gtest.C(t, func(t *gtest.T) { 222 var num float64 = 1.1 223 objOne := gvar.New(num, true) 224 t.Assert(objOne.Float64(), num) 225 226 }) 227 } 228 229 func Test_Time(t *testing.T) { 230 gtest.C(t, func(t *gtest.T) { 231 var timeUnix int64 = 1556242660 232 objOne := gvar.New(timeUnix, true) 233 t.Assert(objOne.Time().Unix(), timeUnix) 234 }) 235 } 236 237 func Test_GTime(t *testing.T) { 238 gtest.C(t, func(t *gtest.T) { 239 var timeUnix int64 = 1556242660 240 objOne := gvar.New(timeUnix, true) 241 t.Assert(objOne.GTime().Unix(), timeUnix) 242 }) 243 } 244 245 func Test_Duration(t *testing.T) { 246 gtest.C(t, func(t *gtest.T) { 247 var timeUnix int64 = 1556242660 248 objOne := gvar.New(timeUnix, true) 249 t.Assert(objOne.Duration(), time.Duration(timeUnix)) 250 }) 251 } 252 253 func Test_UnmarshalJson(t *testing.T) { 254 gtest.C(t, func(t *gtest.T) { 255 type V struct { 256 Name string 257 Var *gvar.Var 258 } 259 var v *V 260 err := gconv.Struct(map[string]interface{}{ 261 "name": "john", 262 "var": "v", 263 }, &v) 264 t.AssertNil(err) 265 t.Assert(v.Name, "john") 266 t.Assert(v.Var.String(), "v") 267 }) 268 gtest.C(t, func(t *gtest.T) { 269 type V struct { 270 Name string 271 Var gvar.Var 272 } 273 var v *V 274 err := gconv.Struct(map[string]interface{}{ 275 "name": "john", 276 "var": "v", 277 }, &v) 278 t.AssertNil(err) 279 t.Assert(v.Name, "john") 280 t.Assert(v.Var.String(), "v") 281 }) 282 } 283 284 func Test_UnmarshalValue(t *testing.T) { 285 gtest.C(t, func(t *gtest.T) { 286 type V struct { 287 Name string 288 Var *gvar.Var 289 } 290 var v *V 291 err := gconv.Struct(map[string]interface{}{ 292 "name": "john", 293 "var": "v", 294 }, &v) 295 t.AssertNil(err) 296 t.Assert(v.Name, "john") 297 t.Assert(v.Var.String(), "v") 298 }) 299 gtest.C(t, func(t *gtest.T) { 300 type V struct { 301 Name string 302 Var gvar.Var 303 } 304 var v *V 305 err := gconv.Struct(map[string]interface{}{ 306 "name": "john", 307 "var": "v", 308 }, &v) 309 t.AssertNil(err) 310 t.Assert(v.Name, "john") 311 t.Assert(v.Var.String(), "v") 312 }) 313 } 314 315 func Test_Copy(t *testing.T) { 316 gtest.C(t, func(t *gtest.T) { 317 src := g.Map{ 318 "k1": "v1", 319 "k2": "v2", 320 } 321 srcVar := gvar.New(src) 322 dstVar := srcVar.Copy() 323 t.Assert(srcVar.Map(), src) 324 t.Assert(dstVar.Map(), src) 325 326 dstVar.Map()["k3"] = "v3" 327 t.Assert(srcVar.Map(), g.Map{ 328 "k1": "v1", 329 "k2": "v2", 330 }) 331 t.Assert(dstVar.Map(), g.Map{ 332 "k1": "v1", 333 "k2": "v2", 334 "k3": "v3", 335 }) 336 }) 337 } 338 339 func Test_DeepCopy(t *testing.T) { 340 gtest.C(t, func(t *gtest.T) { 341 src := g.Map{ 342 "k1": "v1", 343 "k2": "v2", 344 } 345 srcVar := gvar.New(src) 346 copyVar := srcVar.DeepCopy().(*gvar.Var) 347 copyVar.Set(g.Map{ 348 "k3": "v3", 349 "k4": "v4", 350 }) 351 t.AssertNE(srcVar, copyVar) 352 353 srcVar = nil 354 t.AssertNil(srcVar.DeepCopy()) 355 }) 356 }