github.com/gogf/gf/v2@v2.7.4/internal/utils/utils_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 utils_test 8 9 import ( 10 "io" 11 "reflect" 12 "testing" 13 "unsafe" 14 15 "github.com/gogf/gf/v2/internal/utils" 16 "github.com/gogf/gf/v2/test/gtest" 17 ) 18 19 func Test_ReadCloser(t *testing.T) { 20 gtest.C(t, func(t *gtest.T) { 21 var ( 22 n int 23 b = make([]byte, 3) 24 body = utils.NewReadCloser([]byte{1, 2, 3, 4}, false) 25 ) 26 n, _ = body.Read(b) 27 t.Assert(b[:n], []byte{1, 2, 3}) 28 n, _ = body.Read(b) 29 t.Assert(b[:n], []byte{4}) 30 31 n, _ = body.Read(b) 32 t.Assert(b[:n], []byte{}) 33 n, _ = body.Read(b) 34 t.Assert(b[:n], []byte{}) 35 }) 36 gtest.C(t, func(t *gtest.T) { 37 var ( 38 r []byte 39 body = utils.NewReadCloser([]byte{1, 2, 3, 4}, false) 40 ) 41 r, _ = io.ReadAll(body) 42 t.Assert(r, []byte{1, 2, 3, 4}) 43 r, _ = io.ReadAll(body) 44 t.Assert(r, []byte{}) 45 }) 46 gtest.C(t, func(t *gtest.T) { 47 var ( 48 n int 49 r []byte 50 b = make([]byte, 3) 51 body = utils.NewReadCloser([]byte{1, 2, 3, 4}, true) 52 ) 53 n, _ = body.Read(b) 54 t.Assert(b[:n], []byte{1, 2, 3}) 55 n, _ = body.Read(b) 56 t.Assert(b[:n], []byte{4}) 57 58 n, _ = body.Read(b) 59 t.Assert(b[:n], []byte{1, 2, 3}) 60 n, _ = body.Read(b) 61 t.Assert(b[:n], []byte{4}) 62 63 r, _ = io.ReadAll(body) 64 t.Assert(r, []byte{1, 2, 3, 4}) 65 r, _ = io.ReadAll(body) 66 t.Assert(r, []byte{1, 2, 3, 4}) 67 }) 68 } 69 70 func Test_RemoveSymbols(t *testing.T) { 71 gtest.C(t, func(t *gtest.T) { 72 t.Assert(utils.RemoveSymbols(`-a-b._a c1!@#$%^&*()_+:";'.,'01`), `abac101`) 73 t.Assert(utils.RemoveSymbols(`-a-b我._a c1!@#$%^&*是()_+:帅";'.,哥'01`), `ab我ac1是帅哥01`) 74 }) 75 } 76 77 func Test_CanCallIsNil(t *testing.T) { 78 gtest.C(t, func(t *gtest.T) { 79 var ( 80 iValue = "gf" 81 iChan = make(chan struct{}) 82 iFunc = func() {} 83 iMap = map[string]struct{}{} 84 iPtr = &iValue 85 iSlice = make([]struct{}, 0) 86 iUnsafePointer = unsafe.Pointer(&iValue) 87 ) 88 89 t.Assert(utils.CanCallIsNil(reflect.ValueOf(iValue)), false) 90 t.Assert(utils.CanCallIsNil(reflect.ValueOf(iChan)), true) 91 t.Assert(utils.CanCallIsNil(reflect.ValueOf(iFunc)), true) 92 t.Assert(utils.CanCallIsNil(reflect.ValueOf(iMap)), true) 93 t.Assert(utils.CanCallIsNil(reflect.ValueOf(iPtr)), true) 94 t.Assert(utils.CanCallIsNil(reflect.ValueOf(iSlice)), true) 95 t.Assert(utils.CanCallIsNil(reflect.ValueOf(iUnsafePointer)), true) 96 }) 97 } 98 99 func Test_IsNumeric(t *testing.T) { 100 gtest.C(t, func(t *gtest.T) { 101 t.Assert(utils.IsNumeric("12345"), true) 102 t.Assert(utils.IsNumeric("-12345"), true) 103 t.Assert(utils.IsNumeric("+12345"), true) 104 t.Assert(utils.IsNumeric("123.45"), true) 105 t.Assert(utils.IsNumeric("-123.45"), true) 106 t.Assert(utils.IsNumeric("+123.45"), true) 107 t.Assert(utils.IsNumeric("1+23"), false) 108 t.Assert(utils.IsNumeric("123a45"), false) 109 t.Assert(utils.IsNumeric("123.45.67"), false) 110 t.Assert(utils.IsNumeric(""), false) 111 t.Assert(utils.IsNumeric("1e10"), false) 112 t.Assert(utils.IsNumeric("123 45"), false) 113 t.Assert(utils.IsNumeric("!!!"), false) 114 t.Assert(utils.IsNumeric("-a23"), false) 115 t.Assert(utils.IsNumeric("+a23"), false) 116 t.Assert(utils.IsNumeric("1+23"), false) 117 t.Assert(utils.IsNumeric("1-23"), false) 118 t.Assert(utils.IsNumeric("123."), false) 119 t.Assert(utils.IsNumeric(".123"), false) 120 t.Assert(utils.IsNumeric("123.a"), false) 121 t.Assert(utils.IsNumeric("a.123"), false) 122 t.Assert(utils.IsNumeric("+"), false) 123 t.Assert(utils.IsNumeric("-"), false) 124 t.Assert(utils.IsNumeric("."), false) 125 t.Assert(utils.IsNumeric("-."), false) 126 t.Assert(utils.IsNumeric("+."), false) 127 }) 128 }