github.com/gogf/gf/v2@v2.7.4/internal/empty/empty_z_unit_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 empty_test 8 9 import ( 10 "testing" 11 "time" 12 13 "github.com/gogf/gf/v2/container/gvar" 14 "github.com/gogf/gf/v2/frame/g" 15 "github.com/gogf/gf/v2/internal/empty" 16 "github.com/gogf/gf/v2/test/gtest" 17 "github.com/gogf/gf/v2/util/gconv" 18 ) 19 20 type TestInt int 21 22 type TestString string 23 24 type TestPerson interface { 25 Say() string 26 } 27 28 type TestWoman struct { 29 } 30 31 func (woman TestWoman) Say() string { 32 return "nice" 33 } 34 35 func TestIsEmpty(t *testing.T) { 36 gtest.C(t, func(t *gtest.T) { 37 tmpT1 := "0" 38 tmpT2 := func() {} 39 tmpT2 = nil 40 tmpT3 := make(chan int) 41 var ( 42 tmpT4 TestPerson = nil 43 tmpT5 *TestPerson = nil 44 tmpT6 TestPerson = TestWoman{} 45 tmpT7 TestInt = 0 46 tmpT8 TestString = "" 47 ) 48 tmpF1 := "1" 49 tmpF2 := func(a string) string { return "1" } 50 tmpF3 := make(chan int, 1) 51 tmpF3 <- 1 52 var ( 53 tmpF4 TestPerson = &TestWoman{} 54 tmpF5 TestInt = 1 55 tmpF6 TestString = "1" 56 ) 57 58 // true 59 t.Assert(empty.IsEmpty(nil), true) 60 t.Assert(empty.IsEmpty(0), true) 61 t.Assert(empty.IsEmpty(gconv.Int(tmpT1)), true) 62 t.Assert(empty.IsEmpty(gconv.Int8(tmpT1)), true) 63 t.Assert(empty.IsEmpty(gconv.Int16(tmpT1)), true) 64 t.Assert(empty.IsEmpty(gconv.Int32(tmpT1)), true) 65 t.Assert(empty.IsEmpty(gconv.Int64(tmpT1)), true) 66 t.Assert(empty.IsEmpty(gconv.Uint64(tmpT1)), true) 67 t.Assert(empty.IsEmpty(gconv.Uint(tmpT1)), true) 68 t.Assert(empty.IsEmpty(gconv.Uint16(tmpT1)), true) 69 t.Assert(empty.IsEmpty(gconv.Uint32(tmpT1)), true) 70 t.Assert(empty.IsEmpty(gconv.Uint64(tmpT1)), true) 71 t.Assert(empty.IsEmpty(gconv.Float32(tmpT1)), true) 72 t.Assert(empty.IsEmpty(gconv.Float64(tmpT1)), true) 73 t.Assert(empty.IsEmpty(false), true) 74 t.Assert(empty.IsEmpty([]byte("")), true) 75 t.Assert(empty.IsEmpty(""), true) 76 t.Assert(empty.IsEmpty(g.Map{}), true) 77 t.Assert(empty.IsEmpty(g.Slice{}), true) 78 t.Assert(empty.IsEmpty(g.Array{}), true) 79 t.Assert(empty.IsEmpty(tmpT2), true) 80 t.Assert(empty.IsEmpty(tmpT3), true) 81 t.Assert(empty.IsEmpty(tmpT3), true) 82 t.Assert(empty.IsEmpty(tmpT4), true) 83 t.Assert(empty.IsEmpty(tmpT5), true) 84 t.Assert(empty.IsEmpty(tmpT6), true) 85 t.Assert(empty.IsEmpty(tmpT7), true) 86 t.Assert(empty.IsEmpty(tmpT8), true) 87 88 // false 89 t.Assert(empty.IsEmpty(gconv.Int(tmpF1)), false) 90 t.Assert(empty.IsEmpty(gconv.Int8(tmpF1)), false) 91 t.Assert(empty.IsEmpty(gconv.Int16(tmpF1)), false) 92 t.Assert(empty.IsEmpty(gconv.Int32(tmpF1)), false) 93 t.Assert(empty.IsEmpty(gconv.Int64(tmpF1)), false) 94 t.Assert(empty.IsEmpty(gconv.Uint(tmpF1)), false) 95 t.Assert(empty.IsEmpty(gconv.Uint8(tmpF1)), false) 96 t.Assert(empty.IsEmpty(gconv.Uint16(tmpF1)), false) 97 t.Assert(empty.IsEmpty(gconv.Uint32(tmpF1)), false) 98 t.Assert(empty.IsEmpty(gconv.Uint64(tmpF1)), false) 99 t.Assert(empty.IsEmpty(gconv.Float32(tmpF1)), false) 100 t.Assert(empty.IsEmpty(gconv.Float64(tmpF1)), false) 101 t.Assert(empty.IsEmpty(true), false) 102 t.Assert(empty.IsEmpty(tmpT1), false) 103 t.Assert(empty.IsEmpty([]byte("1")), false) 104 t.Assert(empty.IsEmpty(g.Map{"a": 1}), false) 105 t.Assert(empty.IsEmpty(g.Slice{"1"}), false) 106 t.Assert(empty.IsEmpty(g.Array{"1"}), false) 107 t.Assert(empty.IsEmpty(tmpF2), false) 108 t.Assert(empty.IsEmpty(tmpF3), false) 109 t.Assert(empty.IsEmpty(tmpF4), false) 110 t.Assert(empty.IsEmpty(tmpF5), false) 111 t.Assert(empty.IsEmpty(tmpF6), false) 112 }) 113 } 114 115 func TestIsNil(t *testing.T) { 116 gtest.C(t, func(t *gtest.T) { 117 t.Assert(empty.IsNil(nil), true) 118 }) 119 gtest.C(t, func(t *gtest.T) { 120 var i int 121 t.Assert(empty.IsNil(i), false) 122 }) 123 gtest.C(t, func(t *gtest.T) { 124 var i *int 125 t.Assert(empty.IsNil(i), true) 126 }) 127 gtest.C(t, func(t *gtest.T) { 128 var i *int 129 t.Assert(empty.IsNil(&i), false) 130 t.Assert(empty.IsNil(&i, true), true) 131 }) 132 } 133 134 type Issue3362St struct { 135 time.Time 136 } 137 138 func Test_Issue3362(t *testing.T) { 139 gtest.C(t, func(t *gtest.T) { 140 type A struct { 141 Issue3362 *Issue3362St `json:"issue,omitempty"` 142 } 143 m := gvar.New( 144 &A{}, 145 ).Map( 146 gvar.MapOption{ 147 OmitEmpty: true, 148 }, 149 ) 150 t.Assert(m, nil) 151 }) 152 gtest.C(t, func(t *gtest.T) { 153 var i int 154 t.Assert(empty.IsNil(i), false) 155 }) 156 gtest.C(t, func(t *gtest.T) { 157 var i *int 158 t.Assert(empty.IsNil(i), true) 159 }) 160 gtest.C(t, func(t *gtest.T) { 161 var i *int 162 t.Assert(empty.IsNil(&i), false) 163 t.Assert(empty.IsNil(&i, true), true) 164 }) 165 }