github.com/gogf/gf/v2@v2.7.4/util/gutil/gutil_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 gutil_test 8 9 import ( 10 "context" 11 "reflect" 12 "testing" 13 14 "github.com/gogf/gf/v2/errors/gerror" 15 "github.com/gogf/gf/v2/frame/g" 16 "github.com/gogf/gf/v2/test/gtest" 17 "github.com/gogf/gf/v2/util/gutil" 18 ) 19 20 var ( 21 ctx = context.TODO() 22 ) 23 24 func Test_Try(t *testing.T) { 25 gtest.C(t, func(t *gtest.T) { 26 s := `gutil Try test` 27 t.Assert(gutil.Try(ctx, func(ctx context.Context) { 28 panic(s) 29 }), s) 30 }) 31 gtest.C(t, func(t *gtest.T) { 32 s := `gutil Try test` 33 t.Assert(gutil.Try(ctx, func(ctx context.Context) { 34 panic(gerror.New(s)) 35 }), s) 36 }) 37 } 38 39 func Test_TryCatch(t *testing.T) { 40 gtest.C(t, func(t *gtest.T) { 41 gutil.TryCatch(ctx, func(ctx context.Context) { 42 panic("gutil TryCatch test") 43 }, nil) 44 }) 45 46 gtest.C(t, func(t *gtest.T) { 47 gutil.TryCatch(ctx, func(ctx context.Context) { 48 panic("gutil TryCatch test") 49 50 }, func(ctx context.Context, err error) { 51 t.Assert(err, "gutil TryCatch test") 52 }) 53 }) 54 55 gtest.C(t, func(t *gtest.T) { 56 gutil.TryCatch(ctx, func(ctx context.Context) { 57 panic(gerror.New("gutil TryCatch test")) 58 59 }, func(ctx context.Context, err error) { 60 t.Assert(err, "gutil TryCatch test") 61 }) 62 }) 63 } 64 65 func Test_Throw(t *testing.T) { 66 gtest.C(t, func(t *gtest.T) { 67 defer func() { 68 t.Assert(recover(), "gutil Throw test") 69 }() 70 71 gutil.Throw("gutil Throw test") 72 }) 73 } 74 75 func Test_Keys(t *testing.T) { 76 // not support int 77 gtest.C(t, func(t *gtest.T) { 78 var val int = 1 79 keys := gutil.Keys(reflect.ValueOf(val)) 80 t.AssertEQ(len(keys), 0) 81 }) 82 // map 83 gtest.C(t, func(t *gtest.T) { 84 keys := gutil.Keys(map[int]int{ 85 1: 10, 86 2: 20, 87 }) 88 t.AssertIN("1", keys) 89 t.AssertIN("2", keys) 90 91 strKeys := gutil.Keys(map[string]interface{}{ 92 "key1": 1, 93 "key2": 2, 94 }) 95 t.AssertIN("key1", strKeys) 96 t.AssertIN("key2", strKeys) 97 }) 98 // *map 99 gtest.C(t, func(t *gtest.T) { 100 keys := gutil.Keys(&map[int]int{ 101 1: 10, 102 2: 20, 103 }) 104 t.AssertIN("1", keys) 105 t.AssertIN("2", keys) 106 }) 107 // *struct 108 gtest.C(t, func(t *gtest.T) { 109 type T struct { 110 A string 111 B int 112 } 113 keys := gutil.Keys(new(T)) 114 t.Assert(keys, g.SliceStr{"A", "B"}) 115 }) 116 // *struct nil 117 gtest.C(t, func(t *gtest.T) { 118 type T struct { 119 A string 120 B int 121 } 122 var pointer *T 123 keys := gutil.Keys(pointer) 124 t.Assert(keys, g.SliceStr{"A", "B"}) 125 }) 126 // **struct nil 127 gtest.C(t, func(t *gtest.T) { 128 type T struct { 129 A string 130 B int 131 } 132 var pointer *T 133 keys := gutil.Keys(&pointer) 134 t.Assert(keys, g.SliceStr{"A", "B"}) 135 }) 136 } 137 138 func Test_Values(t *testing.T) { 139 // not support int 140 gtest.C(t, func(t *gtest.T) { 141 var val int = 1 142 keys := gutil.Values(reflect.ValueOf(val)) 143 t.AssertEQ(len(keys), 0) 144 }) 145 // map 146 gtest.C(t, func(t *gtest.T) { 147 values := gutil.Values(map[int]int{ 148 1: 10, 149 2: 20, 150 }) 151 t.AssertIN(10, values) 152 t.AssertIN(20, values) 153 154 values = gutil.Values(map[string]interface{}{ 155 "key1": 10, 156 "key2": 20, 157 }) 158 t.AssertIN(10, values) 159 t.AssertIN(20, values) 160 }) 161 // *map 162 gtest.C(t, func(t *gtest.T) { 163 keys := gutil.Values(&map[int]int{ 164 1: 10, 165 2: 20, 166 }) 167 t.AssertIN(10, keys) 168 t.AssertIN(20, keys) 169 }) 170 // struct 171 gtest.C(t, func(t *gtest.T) { 172 type T struct { 173 A string 174 B int 175 } 176 keys := gutil.Values(T{ 177 A: "1", 178 B: 2, 179 }) 180 t.Assert(keys, g.Slice{"1", 2}) 181 }) 182 } 183 184 func TestListToMapByKey(t *testing.T) { 185 gtest.C(t, func(t *gtest.T) { 186 listMap := []map[string]interface{}{ 187 {"key1": 1, "key2": 2}, 188 {"key3": 3, "key4": 4}, 189 } 190 t.Assert(gutil.ListToMapByKey(listMap, "key1"), "{\"1\":{\"key1\":1,\"key2\":2}}") 191 }) 192 } 193 194 func Test_GetOrDefaultStr(t *testing.T) { 195 gtest.C(t, func(t *gtest.T) { 196 t.Assert(gutil.GetOrDefaultStr("a", "b"), "b") 197 t.Assert(gutil.GetOrDefaultStr("a", "b", "c"), "b") 198 t.Assert(gutil.GetOrDefaultStr("a"), "a") 199 }) 200 } 201 202 func Test_GetOrDefaultAny(t *testing.T) { 203 gtest.C(t, func(t *gtest.T) { 204 t.Assert(gutil.GetOrDefaultAny("a", "b"), "b") 205 t.Assert(gutil.GetOrDefaultAny("a", "b", "c"), "b") 206 t.Assert(gutil.GetOrDefaultAny("a"), "a") 207 }) 208 }