github.com/gogf/gf@v1.16.9/container/gvar/gvar_z_unit_is_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/gogf/gf. 6 7 package gvar_test 8 9 import ( 10 "github.com/gogf/gf/frame/g" 11 "github.com/gogf/gf/test/gtest" 12 "testing" 13 ) 14 15 func TestVar_IsNil(t *testing.T) { 16 gtest.C(t, func(t *gtest.T) { 17 t.Assert(g.NewVar(0).IsNil(), false) 18 t.Assert(g.NewVar(nil).IsNil(), true) 19 t.Assert(g.NewVar(g.Map{}).IsNil(), false) 20 t.Assert(g.NewVar(g.Slice{}).IsNil(), false) 21 }) 22 gtest.C(t, func(t *gtest.T) { 23 t.Assert(g.NewVar(1).IsNil(), false) 24 t.Assert(g.NewVar(0.1).IsNil(), false) 25 t.Assert(g.NewVar(g.Map{"k": "v"}).IsNil(), false) 26 t.Assert(g.NewVar(g.Slice{0}).IsNil(), false) 27 }) 28 } 29 30 func TestVar_IsEmpty(t *testing.T) { 31 gtest.C(t, func(t *gtest.T) { 32 t.Assert(g.NewVar(0).IsEmpty(), true) 33 t.Assert(g.NewVar(nil).IsEmpty(), true) 34 t.Assert(g.NewVar(g.Map{}).IsEmpty(), true) 35 t.Assert(g.NewVar(g.Slice{}).IsEmpty(), true) 36 }) 37 gtest.C(t, func(t *gtest.T) { 38 t.Assert(g.NewVar(1).IsEmpty(), false) 39 t.Assert(g.NewVar(0.1).IsEmpty(), false) 40 t.Assert(g.NewVar(g.Map{"k": "v"}).IsEmpty(), false) 41 t.Assert(g.NewVar(g.Slice{0}).IsEmpty(), false) 42 }) 43 } 44 45 func TestVar_IsInt(t *testing.T) { 46 gtest.C(t, func(t *gtest.T) { 47 t.Assert(g.NewVar(0).IsInt(), true) 48 t.Assert(g.NewVar(nil).IsInt(), false) 49 t.Assert(g.NewVar(g.Map{}).IsInt(), false) 50 t.Assert(g.NewVar(g.Slice{}).IsInt(), false) 51 }) 52 gtest.C(t, func(t *gtest.T) { 53 t.Assert(g.NewVar(1).IsInt(), true) 54 t.Assert(g.NewVar(-1).IsInt(), true) 55 t.Assert(g.NewVar(0.1).IsInt(), false) 56 t.Assert(g.NewVar(g.Map{"k": "v"}).IsInt(), false) 57 t.Assert(g.NewVar(g.Slice{0}).IsInt(), false) 58 }) 59 gtest.C(t, func(t *gtest.T) { 60 t.Assert(g.NewVar(int8(1)).IsInt(), true) 61 t.Assert(g.NewVar(uint8(1)).IsInt(), false) 62 }) 63 } 64 65 func TestVar_IsUint(t *testing.T) { 66 gtest.C(t, func(t *gtest.T) { 67 t.Assert(g.NewVar(0).IsUint(), false) 68 t.Assert(g.NewVar(nil).IsUint(), false) 69 t.Assert(g.NewVar(g.Map{}).IsUint(), false) 70 t.Assert(g.NewVar(g.Slice{}).IsUint(), false) 71 }) 72 gtest.C(t, func(t *gtest.T) { 73 t.Assert(g.NewVar(1).IsUint(), false) 74 t.Assert(g.NewVar(-1).IsUint(), false) 75 t.Assert(g.NewVar(0.1).IsUint(), false) 76 t.Assert(g.NewVar(g.Map{"k": "v"}).IsUint(), false) 77 t.Assert(g.NewVar(g.Slice{0}).IsUint(), false) 78 }) 79 gtest.C(t, func(t *gtest.T) { 80 t.Assert(g.NewVar(int8(1)).IsUint(), false) 81 t.Assert(g.NewVar(uint8(1)).IsUint(), true) 82 }) 83 } 84 85 func TestVar_IsFloat(t *testing.T) { 86 gtest.C(t, func(t *gtest.T) { 87 t.Assert(g.NewVar(0).IsFloat(), false) 88 t.Assert(g.NewVar(nil).IsFloat(), false) 89 t.Assert(g.NewVar(g.Map{}).IsFloat(), false) 90 t.Assert(g.NewVar(g.Slice{}).IsFloat(), false) 91 }) 92 gtest.C(t, func(t *gtest.T) { 93 t.Assert(g.NewVar(1).IsFloat(), false) 94 t.Assert(g.NewVar(-1).IsFloat(), false) 95 t.Assert(g.NewVar(0.1).IsFloat(), true) 96 t.Assert(g.NewVar(float64(1)).IsFloat(), true) 97 t.Assert(g.NewVar(g.Map{"k": "v"}).IsFloat(), false) 98 t.Assert(g.NewVar(g.Slice{0}).IsFloat(), false) 99 }) 100 gtest.C(t, func(t *gtest.T) { 101 t.Assert(g.NewVar(int8(1)).IsFloat(), false) 102 t.Assert(g.NewVar(uint8(1)).IsFloat(), false) 103 }) 104 } 105 106 func TestVar_IsSlice(t *testing.T) { 107 gtest.C(t, func(t *gtest.T) { 108 t.Assert(g.NewVar(0).IsSlice(), false) 109 t.Assert(g.NewVar(nil).IsSlice(), false) 110 t.Assert(g.NewVar(g.Map{}).IsSlice(), false) 111 t.Assert(g.NewVar(g.Slice{}).IsSlice(), true) 112 }) 113 gtest.C(t, func(t *gtest.T) { 114 t.Assert(g.NewVar(1).IsSlice(), false) 115 t.Assert(g.NewVar(-1).IsSlice(), false) 116 t.Assert(g.NewVar(0.1).IsSlice(), false) 117 t.Assert(g.NewVar(float64(1)).IsSlice(), false) 118 t.Assert(g.NewVar(g.Map{"k": "v"}).IsSlice(), false) 119 t.Assert(g.NewVar(g.Slice{0}).IsSlice(), true) 120 }) 121 gtest.C(t, func(t *gtest.T) { 122 t.Assert(g.NewVar(int8(1)).IsSlice(), false) 123 t.Assert(g.NewVar(uint8(1)).IsSlice(), false) 124 }) 125 } 126 127 func TestVar_IsMap(t *testing.T) { 128 gtest.C(t, func(t *gtest.T) { 129 t.Assert(g.NewVar(0).IsMap(), false) 130 t.Assert(g.NewVar(nil).IsMap(), false) 131 t.Assert(g.NewVar(g.Map{}).IsMap(), true) 132 t.Assert(g.NewVar(g.Slice{}).IsMap(), false) 133 }) 134 gtest.C(t, func(t *gtest.T) { 135 t.Assert(g.NewVar(1).IsMap(), false) 136 t.Assert(g.NewVar(-1).IsMap(), false) 137 t.Assert(g.NewVar(0.1).IsMap(), false) 138 t.Assert(g.NewVar(float64(1)).IsMap(), false) 139 t.Assert(g.NewVar(g.Map{"k": "v"}).IsMap(), true) 140 t.Assert(g.NewVar(g.Slice{0}).IsMap(), false) 141 }) 142 gtest.C(t, func(t *gtest.T) { 143 t.Assert(g.NewVar(int8(1)).IsMap(), false) 144 t.Assert(g.NewVar(uint8(1)).IsMap(), false) 145 }) 146 } 147 148 func TestVar_IsStruct(t *testing.T) { 149 gtest.C(t, func(t *gtest.T) { 150 t.Assert(g.NewVar(0).IsStruct(), false) 151 t.Assert(g.NewVar(nil).IsStruct(), false) 152 t.Assert(g.NewVar(g.Map{}).IsStruct(), false) 153 t.Assert(g.NewVar(g.Slice{}).IsStruct(), false) 154 }) 155 gtest.C(t, func(t *gtest.T) { 156 t.Assert(g.NewVar(1).IsStruct(), false) 157 t.Assert(g.NewVar(-1).IsStruct(), false) 158 t.Assert(g.NewVar(0.1).IsStruct(), false) 159 t.Assert(g.NewVar(float64(1)).IsStruct(), false) 160 t.Assert(g.NewVar(g.Map{"k": "v"}).IsStruct(), false) 161 t.Assert(g.NewVar(g.Slice{0}).IsStruct(), false) 162 }) 163 gtest.C(t, func(t *gtest.T) { 164 a := &struct { 165 }{} 166 t.Assert(g.NewVar(a).IsStruct(), true) 167 t.Assert(g.NewVar(*a).IsStruct(), true) 168 t.Assert(g.NewVar(&a).IsStruct(), true) 169 }) 170 }