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