github.com/gogf/gf@v1.16.9/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 "github.com/gogf/gf/frame/g" 11 "testing" 12 13 "github.com/gogf/gf/test/gtest" 14 "github.com/gogf/gf/util/gutil" 15 ) 16 17 func Test_Dump(t *testing.T) { 18 gtest.C(t, func(t *gtest.T) { 19 gutil.Dump(map[int]int{ 20 100: 100, 21 }) 22 }) 23 } 24 25 func Test_Try(t *testing.T) { 26 gtest.C(t, func(t *gtest.T) { 27 s := `gutil Try test` 28 t.Assert(gutil.Try(func() { 29 panic(s) 30 }), s) 31 }) 32 } 33 34 func Test_TryCatch(t *testing.T) { 35 gtest.C(t, func(t *gtest.T) { 36 gutil.TryCatch(func() { 37 panic("gutil TryCatch test") 38 }) 39 }) 40 41 gtest.C(t, func(t *gtest.T) { 42 gutil.TryCatch(func() { 43 panic("gutil TryCatch test") 44 45 }, func(err error) { 46 t.Assert(err, "gutil TryCatch test") 47 }) 48 }) 49 } 50 51 func Test_IsEmpty(t *testing.T) { 52 gtest.C(t, func(t *gtest.T) { 53 t.Assert(gutil.IsEmpty(1), false) 54 }) 55 } 56 57 func Test_Throw(t *testing.T) { 58 gtest.C(t, func(t *gtest.T) { 59 defer func() { 60 t.Assert(recover(), "gutil Throw test") 61 }() 62 63 gutil.Throw("gutil Throw test") 64 }) 65 } 66 67 func Test_Keys(t *testing.T) { 68 // map 69 gtest.C(t, func(t *gtest.T) { 70 keys := gutil.Keys(map[int]int{ 71 1: 10, 72 2: 20, 73 }) 74 t.AssertIN("1", keys) 75 t.AssertIN("2", keys) 76 }) 77 // *map 78 gtest.C(t, func(t *gtest.T) { 79 keys := gutil.Keys(&map[int]int{ 80 1: 10, 81 2: 20, 82 }) 83 t.AssertIN("1", keys) 84 t.AssertIN("2", keys) 85 }) 86 // *struct 87 gtest.C(t, func(t *gtest.T) { 88 type T struct { 89 A string 90 B int 91 } 92 keys := gutil.Keys(new(T)) 93 t.Assert(keys, g.SliceStr{"A", "B"}) 94 }) 95 // *struct nil 96 gtest.C(t, func(t *gtest.T) { 97 type T struct { 98 A string 99 B int 100 } 101 var pointer *T 102 keys := gutil.Keys(pointer) 103 t.Assert(keys, g.SliceStr{"A", "B"}) 104 }) 105 // **struct nil 106 gtest.C(t, func(t *gtest.T) { 107 type T struct { 108 A string 109 B int 110 } 111 var pointer *T 112 keys := gutil.Keys(&pointer) 113 t.Assert(keys, g.SliceStr{"A", "B"}) 114 }) 115 } 116 117 func Test_Values(t *testing.T) { 118 gtest.C(t, func(t *gtest.T) { 119 values := gutil.Keys(map[int]int{ 120 1: 10, 121 2: 20, 122 }) 123 t.AssertIN("1", values) 124 t.AssertIN("2", values) 125 }) 126 127 gtest.C(t, func(t *gtest.T) { 128 type T struct { 129 A string 130 B int 131 } 132 keys := gutil.Values(T{ 133 A: "1", 134 B: 2, 135 }) 136 t.Assert(keys, g.Slice{"1", 2}) 137 }) 138 }