mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 06:02:27 +08:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c7e961704d | ||
|
|
cb7df1b57d | ||
|
|
eeff28606e | ||
|
|
86d4b25a2b | ||
|
|
ad287ed99a | ||
|
|
df9de3065b | ||
|
|
71a2ea3f20 | ||
|
|
955f2e6de6 | ||
|
|
4aef9d6d22 | ||
|
|
4752725dd6 | ||
|
|
07d1704cb2 | ||
|
|
74d262e609 | ||
|
|
97e0789ea4 | ||
|
|
bc39b0887b | ||
|
|
4fd8a18677 | ||
|
|
3de906d7c9 | ||
|
|
56fc12c660 | ||
|
|
7a9b0847f9 | ||
|
|
9266d99249 | ||
|
|
f15131f032 | ||
|
|
b4a49fccfd |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,4 +3,7 @@
|
||||
.DS_Store
|
||||
cryptor/*.txt
|
||||
fileutil/*.txt
|
||||
fileutil/*.zip
|
||||
fileutil/*.link
|
||||
fileutil/unzip/*
|
||||
cryptor/*.pem
|
||||
20
README.md
20
README.md
@@ -6,7 +6,7 @@
|
||||
<div align="center" style="text-align: center;">
|
||||
|
||||

|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||
[](https://codecov.io/gh/duke-git/lancet)
|
||||
@@ -21,9 +21,9 @@ English | [简体中文](./README_zh-CN.md)
|
||||
### Feature
|
||||
|
||||
- 👏 Comprehensive, efficient and reusable.
|
||||
- 💪 100+ common go util functions, support string, slice, datetime, net, crypt...
|
||||
- 💪 140+ common go util functions, support string, slice, datetime, net, crypt...
|
||||
- 💅 Only depend on the go standard library.
|
||||
- 🌍 Unit test for exery exported function.
|
||||
- 🌍 Unit test for every exported function.
|
||||
|
||||
### Installation
|
||||
|
||||
@@ -215,12 +215,17 @@ func main() {
|
||||
func ClearFile(path string) error //write empty string to path file
|
||||
func CreateFile(path string) bool // create a file in path
|
||||
func CopyFile(srcFilePath string, dstFilePath string) error //copy src file to dst file
|
||||
func FileMode(path string) (fs.FileMode, error) //return file's mode and permission
|
||||
func MiMeType(file interface{}) string //return file mime type, file should be string or *os.File
|
||||
func IsExist(path string) bool //checks if a file or directory exists
|
||||
func IsLink(path string) bool //checks if a file is symbol link or not
|
||||
func IsDir(path string) bool //checks if the path is directy or not
|
||||
func ListFileNames(path string) ([]string, error) //return all file names in the path
|
||||
func RemoveFile(path string) error //remove the path file
|
||||
func ReadFileToString(path string) (string, error) //return string of file content
|
||||
func ReadFileByLine(path string)([]string, error) //read file content by line
|
||||
func Zip(fpath string, destPath string) error //create zip file, fpath could be a single file or a directory
|
||||
func UnZip(zipFile string, destPath string) error //unzip the file and save it to destPath
|
||||
```
|
||||
|
||||
#### 5. formatter is for data format
|
||||
@@ -281,7 +286,7 @@ func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool //
|
||||
func (w *Watcher) Start() //start the watch timer.
|
||||
func (w *Watcher) Stop() //stop the watch timer
|
||||
func (w *Watcher) Reset() {} //reset the watch timer.
|
||||
func (w *Watcher) GetElapsedTime() time.Duration //获取代码段运行时间
|
||||
func (w *Watcher) GetElapsedTime() time.Duration //return time duration from watcher start to end.
|
||||
```
|
||||
|
||||
#### 7. netutil is for net process
|
||||
@@ -394,9 +399,11 @@ func Difference(slice1, slice2 interface{}) interface{} //creates an slice of wh
|
||||
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //delete the element of slice from start index to end index - 1
|
||||
func Drop(slice interface{}, n int) interface{} //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0
|
||||
func Every(slice, function interface{}) bool //return true if all of the values in the slice pass the predicate function, function signature should be func(index int, value interface{}) bool
|
||||
func None(slice, function interface{}) bool // return true if all the values in the slice mismatch the criteria
|
||||
func Filter(slice, function interface{}) interface{} //filter slice, function signature should be func(index int, value interface{}) bool
|
||||
func Find(slice, function interface{}) interface{} //iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool .
|
||||
func Find(slice, function interface{}) (interface{}, bool) //iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool .
|
||||
func FlattenDeep(slice interface{}) interface{} //flattens slice recursive
|
||||
func ForEach(slice, function interface{}) //iterates over elements of slice and invokes function for each element, function signature should be func(index int, value interface{})
|
||||
func IntSlice(slice interface{}) ([]int, error) //convert value to int slice
|
||||
func InterfaceSlice(slice interface{}) []interface{} //convert value to interface{} slice
|
||||
func Intersection(slices ...interface{}) interface{} //creates a slice of unique values that included by all slices.
|
||||
@@ -412,6 +419,7 @@ func Unique(slice interface{}) interface{} //remove duplicate elements in slice
|
||||
func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index.
|
||||
func Without(slice interface{}, values ...interface{}) interface{} //creates a slice excluding all given values
|
||||
func GroupBy(slice, function interface{}) (interface{}, interface{}) // groups slice into two categories
|
||||
```
|
||||
|
||||
#### 10. strutil is for processing string
|
||||
@@ -452,6 +460,8 @@ func PadEnd(source string, size int, padStr string) string //pads string on the
|
||||
func PadStart(source string, size int, padStr string) string//pads string on the left side if it's shorter than size
|
||||
func ReverseStr(s string) string //return string whose char order is reversed to the given string
|
||||
func SnakeCase(s string) string //covert string to snake_case "fooBar" -> "foo_bar"
|
||||
func Wrap(str string, wrapWith string) string //wrap a string with another string.
|
||||
func Unwrap(str string, wrapToken string) string //unwrap a given string from anther string. will change str value
|
||||
```
|
||||
|
||||
#### 11. validator is for data validation
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<div align="center" style="text-align: center;">
|
||||
|
||||

|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://github.com/duke-git/lancet/releases)
|
||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||
[](https://codecov.io/gh/duke-git/lancet)
|
||||
@@ -21,7 +21,7 @@
|
||||
### 特性
|
||||
|
||||
- 👏 全面、高效、可复用
|
||||
- 💪 100+常用go工具函数,支持string、slice、datetime、net、crypt...
|
||||
- 💪 140+常用go工具函数,支持string、slice、datetime、net、crypt...
|
||||
- 💅 只依赖go标准库
|
||||
- 🌍 所有导出函数单元测试覆盖率100%
|
||||
|
||||
@@ -214,14 +214,19 @@ func main() {
|
||||
|
||||
```go
|
||||
func ClearFile(path string) error //清空文件内容
|
||||
func IsExist(path string) bool //判断文件/目录是否存在
|
||||
func CreateFile(path string) bool //创建文件
|
||||
func FileMode(path string) (fs.FileMode, error) //返回文件mode信息
|
||||
func MiMeType(file interface{}) string //返回文件mime类型
|
||||
func IsExist(path string) bool //判断文件/目录是否存在
|
||||
func IsDir(path string) bool //判断是否为目录
|
||||
func IsLink(path string) bool //检查文件是否为符号链接文件
|
||||
func RemoveFile(path string) error //删除文件
|
||||
func CopyFile(srcFilePath string, dstFilePath string) error //复制文件
|
||||
func ListFileNames(path string) ([]string, error) //列出目录下所有文件名称
|
||||
func ReadFileToString(path string) (string, error) //读取文件内容为字符串
|
||||
func ReadFileByLine(path string)([]string, error) //按行读取文件内容
|
||||
func Zip(fpath string, destPath string) error //压缩文件fpath参数可以是文件或目录,destPath是压缩后目标文件
|
||||
func UnZip(zipFile string, destPath string) error //解压文件,并将文件存储在destPath目录中
|
||||
```
|
||||
|
||||
#### 5. formatter格式化处理包
|
||||
@@ -395,9 +400,11 @@ func Difference(slice1, slice2 interface{}) interface{} //返回
|
||||
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //删除切片中start到end位置的值
|
||||
func Drop(slice interface{}, n int) interface{} //创建一个新切片,当n大于0时删除原切片前n个元素,当n小于0时删除原切片后n个元素
|
||||
func Every(slice, function interface{}) bool //slice中所有元素都符合函数条件时返回true, 否则返回false. 函数签名:func(index int, value interface{}) bool
|
||||
func Find(slice, function interface{}) interface{} //查找slice中第一个符合条件的元素,函数签名:func(index int, value interface{}) bool
|
||||
func None(slice, function interface{}) bool //slice中所有元素都不符合函数条件时返回true, 否则返回false. 函数签名:func(index int, value interface{}) bool
|
||||
func Find(slice, function interface{}) (interface{}, bool)//查找slice中第一个符合条件的元素,函数签名:func(index int, value interface{}) bool
|
||||
func Filter(slice, function interface{}) interface{} //过滤slice, 函数签名:func(index int, value interface{}) bool
|
||||
func FlattenDeep(slice interface{}) interface{} //将slice递归为一维切片。
|
||||
func ForEach(slice, function interface{}) //遍历切片,在每个元素上执行函数,函数签名:func(index int, value interface{})
|
||||
func IntSlice(slice interface{}) ([]int, error) //转成int切片
|
||||
func InterfaceSlice(slice interface{}) []interface{} //转成interface{}切片
|
||||
func Intersection(slices ...interface{}) interface{} //slice交集,去重
|
||||
@@ -413,6 +420,7 @@ func Unique(slice interface{}) interface{} //去重切片
|
||||
func Union(slices ...interface{}) interface{} //slice并集, 去重
|
||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置更新value
|
||||
func Without(slice interface{}, values ...interface{}) interface{} //slice去除values
|
||||
func GroupBy(slice, function interface{}) (interface{}, interface{}) //根据函数function的逻辑分slice为两组slice
|
||||
```
|
||||
|
||||
#### 10. strutil字符串处理包
|
||||
@@ -451,7 +459,9 @@ func KebabCase(s string) string //字符串转为KebabCase, "foo_Bar" -> "foo-ba
|
||||
func LowerFirst(s string) string //字符串的第一个字母转为小写字母
|
||||
func PadEnd(source string, size int, padStr string) string //字符串末尾填充size个字符
|
||||
func PadStart(source string, size int, padStr string) string//字符串开头填充size个字符
|
||||
func ReverseStr(s string) string //字符串逆袭
|
||||
func ReverseStr(s string) string //字符串逆序
|
||||
func Wrap(str string, wrapWith string) string //包裹字符串 Wrap("abc", "*") -> *abc*.
|
||||
func Unwrap(str string, wrapToken string) string //解包裹字符串 Wrap("*abc*", "*") -> abc.
|
||||
func SnakeCase(s string) string //字符串转为SnakeCase, "fooBar" -> "foo_bar"
|
||||
```
|
||||
|
||||
|
||||
@@ -45,44 +45,34 @@ func ToChar(s string) []string {
|
||||
|
||||
// ToString convert value to string
|
||||
func ToString(value interface{}) string {
|
||||
var res string
|
||||
res := ""
|
||||
if value == nil {
|
||||
return res
|
||||
}
|
||||
switch v := value.(type) {
|
||||
case float64:
|
||||
res = strconv.FormatFloat(v, 'f', -1, 64)
|
||||
case float32:
|
||||
res = strconv.FormatFloat(float64(v), 'f', -1, 64)
|
||||
case int:
|
||||
res = strconv.Itoa(v)
|
||||
case uint:
|
||||
res = strconv.Itoa(int(v))
|
||||
case int8:
|
||||
res = strconv.Itoa(int(v))
|
||||
case uint8:
|
||||
res = strconv.Itoa(int(v))
|
||||
case int16:
|
||||
res = strconv.Itoa(int(v))
|
||||
case uint16:
|
||||
res = strconv.Itoa(int(v))
|
||||
case int32:
|
||||
res = strconv.Itoa(int(v))
|
||||
case uint32:
|
||||
res = strconv.Itoa(int(v))
|
||||
case int64:
|
||||
res = strconv.FormatInt(v, 10)
|
||||
case uint64:
|
||||
res = strconv.FormatUint(v, 10)
|
||||
|
||||
v := reflect.ValueOf(value)
|
||||
|
||||
switch value.(type) {
|
||||
case float32, float64:
|
||||
res = strconv.FormatFloat(v.Float(), 'f', -1, 64)
|
||||
return res
|
||||
case int, int8, int16, int32, int64:
|
||||
res = strconv.FormatInt(v.Int(), 10)
|
||||
return res
|
||||
case uint, uint8, uint16, uint32, uint64:
|
||||
res = strconv.FormatUint(v.Uint(), 10)
|
||||
return res
|
||||
case string:
|
||||
res = value.(string)
|
||||
res = v.String()
|
||||
return res
|
||||
case []byte:
|
||||
res = string(value.([]byte))
|
||||
res = string(v.Bytes())
|
||||
return res
|
||||
default:
|
||||
newValue, _ := json.Marshal(value)
|
||||
res = string(newValue)
|
||||
return res
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// ToJson convert value to a valid json string
|
||||
|
||||
@@ -108,14 +108,19 @@ func TestToString(t *testing.T) {
|
||||
aStruct := TestStruct{Name: "TestStruct"}
|
||||
|
||||
cases := []interface{}{
|
||||
"", nil,
|
||||
int(0), int8(1), int16(-1), int32(123), int64(123),
|
||||
uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
|
||||
float64(12.3), float32(12.3),
|
||||
true, false,
|
||||
[]int{1, 2, 3}, aMap, aStruct, []byte{104, 101, 108, 108, 111}}
|
||||
|
||||
expected := []string{"0", "1", "-1", "123", "123", "123", "123", "123",
|
||||
"123", "123", "12.3", "12.300000190734863", "true", "false",
|
||||
expected := []string{
|
||||
"", "",
|
||||
"0", "1", "-1",
|
||||
"123", "123", "123", "123", "123", "123", "123",
|
||||
"12.3", "12.300000190734863",
|
||||
"true", "false",
|
||||
"[1,2,3]", "{\"a\":1,\"b\":2,\"c\":3}", "{\"Name\":\"TestStruct\"}", "hello"}
|
||||
|
||||
for i := 0; i < len(cases); i++ {
|
||||
|
||||
148
fileutil/file.go
148
fileutil/file.go
@@ -5,11 +5,16 @@
|
||||
package fileutil
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bufio"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IsExist checks if a file or directory exists
|
||||
@@ -148,3 +153,146 @@ func ListFileNames(path string) ([]string, error) {
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Zip create zip file, fpath could be a single file or a directory
|
||||
func Zip(fpath string, destPath string) error {
|
||||
zipFile, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer zipFile.Close()
|
||||
|
||||
archive := zip.NewWriter(zipFile)
|
||||
defer archive.Close()
|
||||
|
||||
filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
header, err := zip.FileInfoHeader(info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
header.Name = strings.TrimPrefix(path, filepath.Dir(fpath)+"/")
|
||||
|
||||
if info.IsDir() {
|
||||
header.Name += "/"
|
||||
} else {
|
||||
header.Method = zip.Deflate
|
||||
}
|
||||
|
||||
writer, err := archive.CreateHeader(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = io.Copy(writer, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnZip unzip the file and save it to destPath
|
||||
func UnZip(zipFile string, destPath string) error {
|
||||
zipReader, err := zip.OpenReader(zipFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer zipReader.Close()
|
||||
|
||||
for _, f := range zipReader.File {
|
||||
path := filepath.Join(destPath, f.Name)
|
||||
if f.FileInfo().IsDir() {
|
||||
os.MkdirAll(path, os.ModePerm)
|
||||
} else {
|
||||
if err = os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
inFile, err := f.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer inFile.Close()
|
||||
|
||||
outFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
_, err = io.Copy(outFile, inFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsLink checks if a file is symbol link or not
|
||||
func IsLink(path string) bool {
|
||||
fi, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return fi.Mode()&os.ModeSymlink != 0
|
||||
}
|
||||
|
||||
// FileMode return file's mode and permission
|
||||
func FileMode(path string) (fs.FileMode, error) {
|
||||
fi, err := os.Lstat(path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return fi.Mode(), nil
|
||||
}
|
||||
|
||||
// MiMeType return file mime type
|
||||
// file should be string or *os.File
|
||||
func MiMeType(file interface{}) string {
|
||||
var mediatype string
|
||||
|
||||
readBuffer := func(f *os.File) ([]byte, error) {
|
||||
buffer := make([]byte, 512)
|
||||
_, err := f.Read(buffer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buffer, nil
|
||||
}
|
||||
|
||||
if filePath, ok := file.(string); ok {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return mediatype
|
||||
}
|
||||
buffer, err := readBuffer(f)
|
||||
if err != nil {
|
||||
return mediatype
|
||||
}
|
||||
return http.DetectContentType(buffer)
|
||||
}
|
||||
|
||||
if f, ok := file.(*os.File); ok {
|
||||
buffer, err := readBuffer(f)
|
||||
if err != nil {
|
||||
return mediatype
|
||||
}
|
||||
return http.DetectContentType(buffer)
|
||||
}
|
||||
return mediatype
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ func TestCreateFile(t *testing.T) {
|
||||
internal.LogFailedTestInfo(t, "CreateFile", f, f, "create file error")
|
||||
t.FailNow()
|
||||
}
|
||||
os.Remove(f)
|
||||
}
|
||||
|
||||
func TestIsDir(t *testing.T) {
|
||||
@@ -70,20 +71,22 @@ func TestCopyFile(t *testing.T) {
|
||||
srcFile := "./text.txt"
|
||||
CreateFile(srcFile)
|
||||
|
||||
dstFile := "./text_copy.txt"
|
||||
destFile := "./text_copy.txt"
|
||||
|
||||
err := CopyFile(srcFile, dstFile)
|
||||
err := CopyFile(srcFile, destFile)
|
||||
if err != nil {
|
||||
file, err := os.Open(dstFile)
|
||||
file, err := os.Open(destFile)
|
||||
if err != nil {
|
||||
internal.LogFailedTestInfo(t, "CopyFile", srcFile, dstFile, "create file error: "+err.Error())
|
||||
internal.LogFailedTestInfo(t, "CopyFile", srcFile, destFile, "create file error: "+err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
if file.Name() != dstFile {
|
||||
internal.LogFailedTestInfo(t, "CopyFile", srcFile, dstFile, file.Name())
|
||||
if file.Name() != destFile {
|
||||
internal.LogFailedTestInfo(t, "CopyFile", srcFile, destFile, file.Name())
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
os.Remove(srcFile)
|
||||
os.Remove(destFile)
|
||||
}
|
||||
|
||||
func TestListFileNames(t *testing.T) {
|
||||
@@ -101,32 +104,41 @@ func TestListFileNames(t *testing.T) {
|
||||
func TestReadFileToString(t *testing.T) {
|
||||
path := "./text.txt"
|
||||
CreateFile(path)
|
||||
|
||||
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
||||
f.WriteString("hello world")
|
||||
|
||||
res, _ := ReadFileToString(path)
|
||||
if res != "hello world" {
|
||||
internal.LogFailedTestInfo(t, "ReadFileToString", path, "hello world", res)
|
||||
t.FailNow()
|
||||
}
|
||||
os.Remove(path)
|
||||
}
|
||||
|
||||
func TestClearFile(t *testing.T) {
|
||||
path := "./text.txt"
|
||||
CreateFile(path)
|
||||
|
||||
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
||||
f.WriteString("hello world")
|
||||
|
||||
CreateFile(path)
|
||||
|
||||
res, _ := ReadFileToString(path)
|
||||
if res != "" {
|
||||
internal.LogFailedTestInfo(t, "CreateFile", path, "", res)
|
||||
err := ClearFile(path)
|
||||
if err != nil {
|
||||
t.Error("Clear file error: ", err)
|
||||
}
|
||||
fileContent, _ := ReadFileToString(path)
|
||||
if fileContent != "" {
|
||||
internal.LogFailedTestInfo(t, "ClearFile", path, "", fileContent)
|
||||
t.FailNow()
|
||||
}
|
||||
os.Remove(path)
|
||||
}
|
||||
|
||||
func TestReadFileByLine(t *testing.T) {
|
||||
path := "./text.txt"
|
||||
CreateFile(path)
|
||||
|
||||
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
|
||||
f.WriteString("hello\nworld")
|
||||
|
||||
@@ -134,5 +146,88 @@ func TestReadFileByLine(t *testing.T) {
|
||||
res, _ := ReadFileByLine(path)
|
||||
if !reflect.DeepEqual(res, expected) {
|
||||
internal.LogFailedTestInfo(t, "ReadFileByLine", path, expected, res)
|
||||
t.FailNow()
|
||||
}
|
||||
os.Remove(path)
|
||||
}
|
||||
|
||||
func TestZipAndUnZip(t *testing.T) {
|
||||
srcFile := "./text.txt"
|
||||
CreateFile(srcFile)
|
||||
|
||||
file, _ := os.OpenFile(srcFile, os.O_WRONLY|os.O_TRUNC, 0777)
|
||||
file.WriteString("hello\nworld")
|
||||
|
||||
zipFile := "./text.zip"
|
||||
err := Zip(srcFile, zipFile)
|
||||
if err != nil {
|
||||
internal.LogFailedTestInfo(t, "Zip", srcFile, zipFile, err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
unZipPath := "./unzip"
|
||||
err = UnZip(zipFile, unZipPath)
|
||||
if err != nil {
|
||||
internal.LogFailedTestInfo(t, "UnZip", srcFile, unZipPath, err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
unZipFile := "./unzip/text.txt"
|
||||
if !IsExist(unZipFile) {
|
||||
internal.LogFailedTestInfo(t, "UnZip", zipFile, zipFile, err)
|
||||
t.FailNow()
|
||||
}
|
||||
os.Remove(srcFile)
|
||||
os.Remove(zipFile)
|
||||
os.RemoveAll(unZipPath)
|
||||
}
|
||||
|
||||
func TestFileMode(t *testing.T) {
|
||||
srcFile := "./text.txt"
|
||||
CreateFile(srcFile)
|
||||
|
||||
mode, err := FileMode(srcFile)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
t.Log(mode)
|
||||
|
||||
os.Remove(srcFile)
|
||||
}
|
||||
|
||||
func TestIsLink(t *testing.T) {
|
||||
srcFile := "./text.txt"
|
||||
CreateFile(srcFile)
|
||||
|
||||
linkFile := "./text.link"
|
||||
if !IsExist(linkFile) {
|
||||
_ = os.Symlink(srcFile, linkFile)
|
||||
}
|
||||
if !IsLink(linkFile) {
|
||||
internal.LogFailedTestInfo(t, "IsLink", linkFile, "true", "false")
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if IsLink("./file.go") {
|
||||
internal.LogFailedTestInfo(t, "IsLink", "./file.go", "false", "true")
|
||||
t.FailNow()
|
||||
}
|
||||
os.Remove(srcFile)
|
||||
os.Remove(linkFile)
|
||||
}
|
||||
|
||||
func TestMiMeType(t *testing.T) {
|
||||
mt1 := MiMeType("./file.go")
|
||||
expected := "text/plain; charset=utf-8"
|
||||
|
||||
if mt1 != expected {
|
||||
internal.LogFailedTestInfo(t, "MiMeType", "./file.go", expected, mt1)
|
||||
t.FailNow()
|
||||
}
|
||||
f, _ := os.Open("./file.go")
|
||||
mt2 := MiMeType(f)
|
||||
if mt2 != expected {
|
||||
internal.LogFailedTestInfo(t, "MiMeType", "./file.go", expected, mt2)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
// After creates a function that invokes func once it's called n or more times
|
||||
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value {
|
||||
// Catch programming error while constructing the closure
|
||||
MustBeFunction(fn)
|
||||
mustBeFunction(fn)
|
||||
return func(args ...interface{}) []reflect.Value {
|
||||
n--
|
||||
if n < 1 {
|
||||
@@ -25,7 +25,7 @@ func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value {
|
||||
// Before creates a function that invokes func once it's called less than n times
|
||||
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value {
|
||||
// Catch programming error while constructing the closure
|
||||
MustBeFunction(fn)
|
||||
mustBeFunction(fn)
|
||||
var res []reflect.Value
|
||||
return func(args ...interface{}) []reflect.Value {
|
||||
if n > 0 {
|
||||
@@ -73,7 +73,7 @@ func Delay(delay time.Duration, fn interface{}, args ...interface{}) {
|
||||
// Schedule invoke function every duration time, util close the returned bool chan
|
||||
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool {
|
||||
// Catch programming error while constructing the closure
|
||||
MustBeFunction(fn)
|
||||
mustBeFunction(fn)
|
||||
quit := make(chan bool)
|
||||
go func() {
|
||||
for {
|
||||
|
||||
@@ -31,7 +31,7 @@ func functionValue(function interface{}) reflect.Value {
|
||||
return v
|
||||
}
|
||||
|
||||
func MustBeFunction(function interface{}) {
|
||||
func mustBeFunction(function interface{}) {
|
||||
v := reflect.ValueOf(function)
|
||||
if v.Kind() != reflect.Func {
|
||||
panic(fmt.Sprintf("Invalid function type, value of type %T", function))
|
||||
|
||||
@@ -25,9 +25,8 @@ func (w *Watcher) Stop() {
|
||||
func (w *Watcher) GetElapsedTime() time.Duration {
|
||||
if w.excuting {
|
||||
return time.Duration(time.Now().UnixNano() - w.startTime)
|
||||
} else {
|
||||
return time.Duration(w.stopTime - w.startTime)
|
||||
}
|
||||
return time.Duration(w.stopTime - w.startTime)
|
||||
}
|
||||
|
||||
// Reset the watch timer.
|
||||
|
||||
@@ -22,27 +22,27 @@ import (
|
||||
|
||||
//HttpGet send get http request
|
||||
func HttpGet(url string, params ...interface{}) (*http.Response, error) {
|
||||
return request(http.MethodGet, url, params...)
|
||||
return doHttpRequest(http.MethodGet, url, params...)
|
||||
}
|
||||
|
||||
//HttpPost send post http request
|
||||
func HttpPost(url string, params ...interface{}) (*http.Response, error) {
|
||||
return request(http.MethodPost, url, params...)
|
||||
return doHttpRequest(http.MethodPost, url, params...)
|
||||
}
|
||||
|
||||
//HttpPut send put http request
|
||||
func HttpPut(url string, params ...interface{}) (*http.Response, error) {
|
||||
return request(http.MethodPut, url, params...)
|
||||
return doHttpRequest(http.MethodPut, url, params...)
|
||||
}
|
||||
|
||||
//HttpDelete send delete http request
|
||||
func HttpDelete(url string, params ...interface{}) (*http.Response, error) {
|
||||
return request(http.MethodDelete, url, params...)
|
||||
return doHttpRequest(http.MethodDelete, url, params...)
|
||||
}
|
||||
|
||||
// HttpPatch send patch http request
|
||||
func HttpPatch(url string, params ...interface{}) (*http.Response, error) {
|
||||
return request(http.MethodPatch, url, params...)
|
||||
return doHttpRequest(http.MethodPatch, url, params...)
|
||||
}
|
||||
|
||||
// ParseHttpResponse decode http response to specified interface
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func request(method, reqUrl string, params ...interface{}) (*http.Response, error) {
|
||||
func doHttpRequest(method, reqUrl string, params ...interface{}) (*http.Response, error) {
|
||||
if len(reqUrl) == 0 {
|
||||
return nil, errors.New("url should be specified")
|
||||
}
|
||||
@@ -24,53 +24,29 @@ func request(method, reqUrl string, params ...interface{}) (*http.Response, erro
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
err := setUrl(req, reqUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch len(params) {
|
||||
case 0:
|
||||
err := setUrl(req, reqUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case 1:
|
||||
err := setUrl(req, reqUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = setHeader(req, params[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case 2:
|
||||
err := setHeader(req, params[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = setQueryParam(req, reqUrl, params[1])
|
||||
err := setHeaderAndQueryParam(req, reqUrl, params[0], params[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case 3:
|
||||
err := setHeader(req, params[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = setQueryParam(req, reqUrl, params[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = setBodyByte(req, params[2])
|
||||
err := setHeaderAndQueryAndBody(req, reqUrl, params[0], params[1], params[2])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case 4:
|
||||
err := setHeader(req, params[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = setQueryParam(req, reqUrl, params[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = setBodyByte(req, params[2])
|
||||
err := setHeaderAndQueryAndBody(req, reqUrl, params[0], params[1], params[2])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -78,13 +54,40 @@ func request(method, reqUrl string, params ...interface{}) (*http.Response, erro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
resp, e := client.Do(req)
|
||||
return resp, e
|
||||
}
|
||||
|
||||
func setHeaderAndQueryParam(req *http.Request, reqUrl string, header, queryParam interface{}) error {
|
||||
err := setHeader(req, header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = setQueryParam(req, reqUrl, queryParam)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryParam, body interface{}) error {
|
||||
err := setHeader(req, header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = setQueryParam(req, reqUrl, queryParam)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = setBodyByte(req, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setHeader(req *http.Request, header interface{}) error {
|
||||
if header != nil {
|
||||
switch v := header.(type) {
|
||||
@@ -109,6 +112,7 @@ func setHeader(req *http.Request, header interface{}) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setUrl(req *http.Request, reqUrl string) error {
|
||||
u, err := url.Parse(reqUrl)
|
||||
if err != nil {
|
||||
@@ -117,6 +121,7 @@ func setUrl(req *http.Request, reqUrl string) error {
|
||||
req.URL = u
|
||||
return nil
|
||||
}
|
||||
|
||||
func setQueryParam(req *http.Request, reqUrl string, queryParam interface{}) error {
|
||||
var values url.Values
|
||||
if queryParam != nil {
|
||||
|
||||
100
slice/slice.go
100
slice/slice.go
@@ -121,6 +121,28 @@ func Every(slice, function interface{}) bool {
|
||||
return currentLength == sv.Len()
|
||||
}
|
||||
|
||||
// None return true if all the values in the slice mismatch the criteria
|
||||
// The function signature should be func(index int, value interface{}) bool .
|
||||
func None(slice, function interface{}) bool {
|
||||
sv := sliceValue(slice)
|
||||
fn := functionValue(function)
|
||||
|
||||
elemType := sv.Type().Elem()
|
||||
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
|
||||
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
}
|
||||
|
||||
var currentLength int
|
||||
for i := 0; i < sv.Len(); i++ {
|
||||
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
|
||||
if !flag.Bool() {
|
||||
currentLength++
|
||||
}
|
||||
}
|
||||
|
||||
return currentLength == sv.Len()
|
||||
}
|
||||
|
||||
// Some return true if any of the values in the list pass the predicate function.
|
||||
// The function signature should be func(index int, value interface{}) bool .
|
||||
func Some(slice, function interface{}) bool {
|
||||
@@ -154,24 +176,20 @@ func Filter(slice, function interface{}) interface{} {
|
||||
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
}
|
||||
|
||||
var indexes []int
|
||||
res := reflect.MakeSlice(sv.Type(), 0, 0)
|
||||
for i := 0; i < sv.Len(); i++ {
|
||||
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
|
||||
if flag.Bool() {
|
||||
indexes = append(indexes, i)
|
||||
res = reflect.Append(res, sv.Index(i))
|
||||
}
|
||||
}
|
||||
|
||||
res := reflect.MakeSlice(sv.Type(), len(indexes), len(indexes))
|
||||
for i := range indexes {
|
||||
res.Index(i).Set(sv.Index(indexes[i]))
|
||||
}
|
||||
return res.Interface()
|
||||
}
|
||||
|
||||
// Find iterates over elements of slice, returning the first one that passes a truth test on function.
|
||||
// GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices
|
||||
// The function signature should be func(index int, value interface{}) bool .
|
||||
func Find(slice, function interface{}) interface{} {
|
||||
func GroupBy(slice, function interface{}) (interface{}, interface{}) {
|
||||
sv := sliceValue(slice)
|
||||
fn := functionValue(function)
|
||||
|
||||
@@ -180,7 +198,33 @@ func Find(slice, function interface{}) interface{} {
|
||||
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
}
|
||||
|
||||
var index int
|
||||
groupB := reflect.MakeSlice(sv.Type(), 0, 0)
|
||||
groupA := reflect.MakeSlice(sv.Type(), 0, 0)
|
||||
|
||||
for i := 0; i < sv.Len(); i++ {
|
||||
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
|
||||
if flag.Bool() {
|
||||
groupA = reflect.Append(groupA, sv.Index(i))
|
||||
} else {
|
||||
groupB = reflect.Append(groupB, sv.Index(i))
|
||||
}
|
||||
}
|
||||
|
||||
return groupA.Interface(), groupB.Interface()
|
||||
}
|
||||
|
||||
// Find iterates over elements of slice, returning the first one that passes a truth test on function.
|
||||
// The function signature should be func(index int, value interface{}) bool .
|
||||
func Find(slice, function interface{}) (interface{}, bool) {
|
||||
sv := sliceValue(slice)
|
||||
fn := functionValue(function)
|
||||
|
||||
elemType := sv.Type().Elem()
|
||||
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
|
||||
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||
}
|
||||
|
||||
index := -1
|
||||
for i := 0; i < sv.Len(); i++ {
|
||||
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
|
||||
if flag.Bool() {
|
||||
@@ -188,7 +232,13 @@ func Find(slice, function interface{}) interface{} {
|
||||
break
|
||||
}
|
||||
}
|
||||
return sv.Index(index).Interface()
|
||||
|
||||
if index == -1 {
|
||||
var none interface{}
|
||||
return none, false
|
||||
}
|
||||
|
||||
return sv.Index(index).Interface(), true
|
||||
}
|
||||
|
||||
// FlattenDeep flattens slice recursive
|
||||
@@ -215,6 +265,22 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
|
||||
return result
|
||||
}
|
||||
|
||||
// ForEach iterates over elements of slice and invokes function for each element
|
||||
// The function signature should be func(index int, value interface{}).
|
||||
func ForEach(slice, function interface{}) {
|
||||
sv := sliceValue(slice)
|
||||
fn := functionValue(function)
|
||||
|
||||
elemType := sv.Type().Elem()
|
||||
if checkSliceCallbackFuncSignature(fn, elemType, nil) {
|
||||
panic("function param should be of type func(int, " + elemType.String() + ")" + elemType.String())
|
||||
}
|
||||
|
||||
for i := 0; i < sv.Len(); i++ {
|
||||
fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})
|
||||
}
|
||||
}
|
||||
|
||||
// Map creates an slice of values by running each element of `slice` thru `function`.
|
||||
// The function signature should be func(index int, value interface{}) interface{}.
|
||||
func Map(slice, function interface{}) interface{} {
|
||||
@@ -378,15 +444,15 @@ func Drop(slice interface{}, n int) interface{} {
|
||||
res.Index(i).Set(sv.Index(i + n))
|
||||
}
|
||||
|
||||
return res.Interface()
|
||||
} else {
|
||||
res := reflect.MakeSlice(sv.Type(), svLen+n, svLen+n)
|
||||
for i := 0; i < res.Len(); i++ {
|
||||
res.Index(i).Set(sv.Index(i))
|
||||
}
|
||||
|
||||
return res.Interface()
|
||||
}
|
||||
|
||||
res := reflect.MakeSlice(sv.Type(), svLen+n, svLen+n)
|
||||
for i := 0; i < res.Len(); i++ {
|
||||
res.Index(i).Set(sv.Index(i))
|
||||
}
|
||||
|
||||
return res.Interface()
|
||||
}
|
||||
|
||||
// InsertByIndex insert the element into slice at index.
|
||||
|
||||
@@ -108,6 +108,18 @@ func TestEvery(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNone(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 5}
|
||||
check := func(i, num int) bool {
|
||||
return num%2 == 1
|
||||
}
|
||||
res := None(nums, check)
|
||||
if res != false {
|
||||
internal.LogFailedTestInfo(t, "None", nums, false, res)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSome(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 5}
|
||||
isEven := func(i, num int) bool {
|
||||
@@ -161,18 +173,57 @@ func TestFilter(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestGroupBy(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 4, 5, 6}
|
||||
evenFunc := func(i, num int) bool {
|
||||
return (num % 2) == 0
|
||||
}
|
||||
expectedEven := []int{2, 4, 6}
|
||||
even, odd := GroupBy(nums, evenFunc)
|
||||
|
||||
t.Log("odd", odd)
|
||||
|
||||
t.Log("even", even)
|
||||
|
||||
if !reflect.DeepEqual(IntSlice(even), expectedEven) {
|
||||
internal.LogFailedTestInfo(t, "GroupBy even", nums, expectedEven, even)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
expectedOdd := []int{1, 3, 5}
|
||||
if !reflect.DeepEqual(IntSlice(odd), expectedOdd) {
|
||||
internal.LogFailedTestInfo(t, "GroupBy odd", nums, expectedOdd, odd)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestFind(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 4, 5}
|
||||
even := func(i, num int) bool {
|
||||
return num%2 == 0
|
||||
}
|
||||
res := Find(nums, even)
|
||||
res, ok := Find(nums, even)
|
||||
if !ok {
|
||||
t.Fatal("found nothing")
|
||||
}
|
||||
|
||||
if res != 2 {
|
||||
internal.LogFailedTestInfo(t, "Find", nums, 2, res)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindFoundNothing(t *testing.T) {
|
||||
nums := []int{1, 1, 1, 1, 1, 1}
|
||||
findFunc := func(i, num int) bool {
|
||||
return num > 1
|
||||
}
|
||||
_, ok := Find(nums, findFunc)
|
||||
if ok {
|
||||
t.Fatal("found something")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlattenDeep(t *testing.T) {
|
||||
input := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
||||
expected := []string{"a", "b", "c", "d"}
|
||||
@@ -184,6 +235,22 @@ func TestFlattenDeep(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestForEach(t *testing.T) {
|
||||
numbers := []int{1, 2, 3, 4, 5}
|
||||
expected := []int{3, 4, 5, 6, 7}
|
||||
|
||||
var numbersAddTwo []int
|
||||
ForEach(numbers, func(index int, value int) {
|
||||
numbersAddTwo = append(numbersAddTwo, value+2)
|
||||
})
|
||||
|
||||
if !reflect.DeepEqual(numbersAddTwo, expected) {
|
||||
internal.LogFailedTestInfo(t, "ForEach", numbers, expected, numbersAddTwo)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
s1 := []int{1, 2, 3, 4}
|
||||
multiplyTwo := func(i, num int) int {
|
||||
|
||||
@@ -205,3 +205,34 @@ func ReverseStr(s string) string {
|
||||
}
|
||||
return string(r)
|
||||
}
|
||||
|
||||
// Wrap a string with another string.
|
||||
func Wrap(str string, wrapWith string) string {
|
||||
if str == "" || wrapWith == "" {
|
||||
return str
|
||||
}
|
||||
var sb strings.Builder
|
||||
sb.WriteString(wrapWith)
|
||||
sb.WriteString(str)
|
||||
sb.WriteString(wrapWith)
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// Unwrap a given string from anther string. will change str value
|
||||
func Unwrap(str string, wrapToken string) string {
|
||||
if str == "" || wrapToken == "" {
|
||||
return str
|
||||
}
|
||||
|
||||
firstIndex := strings.Index(str, wrapToken)
|
||||
lastIndex := strings.LastIndex(str, wrapToken)
|
||||
|
||||
if firstIndex == 0 && lastIndex > 0 && lastIndex <= len(str)-1 {
|
||||
if len(wrapToken) <= lastIndex {
|
||||
str = str[len(wrapToken):lastIndex]
|
||||
}
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
@@ -187,9 +187,6 @@ func isString(t *testing.T, test interface{}, expected bool) {
|
||||
func TestReverseStr(t *testing.T) {
|
||||
reverseStr(t, "abc", "cba")
|
||||
reverseStr(t, "12345", "54321")
|
||||
|
||||
//failed
|
||||
//reverseStr(t, "abc", "abc")
|
||||
}
|
||||
|
||||
func reverseStr(t *testing.T, test string, expected string) {
|
||||
@@ -199,3 +196,42 @@ func reverseStr(t *testing.T, test string, expected string) {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrap(t *testing.T) {
|
||||
wrap(t, "ab", "", "ab")
|
||||
wrap(t, "", "*", "")
|
||||
wrap(t, "ab", "*", "*ab*")
|
||||
wrap(t, "ab", "\"", "\"ab\"")
|
||||
wrap(t, "ab", "'", "'ab'")
|
||||
}
|
||||
|
||||
func wrap(t *testing.T, test string, wrapWith string, expected string) {
|
||||
res := Wrap(test, wrapWith)
|
||||
if res != expected {
|
||||
internal.LogFailedTestInfo(t, "Wrap", test, expected, res)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnwrap(t *testing.T) {
|
||||
unwrap(t, "", "*", "")
|
||||
unwrap(t, "ab", "", "ab")
|
||||
unwrap(t, "ab", "*", "ab")
|
||||
unwrap(t, "**ab**", "*", "*ab*")
|
||||
unwrap(t, "**ab**", "**", "ab")
|
||||
unwrap(t, "\"ab\"", "\"", "ab")
|
||||
unwrap(t, "*ab", "*", "*ab")
|
||||
unwrap(t, "ab*", "*", "ab*")
|
||||
unwrap(t, "***", "*", "*")
|
||||
unwrap(t, "**", "*", "")
|
||||
unwrap(t, "***", "**", "***")
|
||||
unwrap(t, "**", "**", "**")
|
||||
}
|
||||
|
||||
func unwrap(t *testing.T, test string, wrapToken string, expected string) {
|
||||
res := Unwrap(test, wrapToken)
|
||||
if res != expected {
|
||||
internal.LogFailedTestInfo(t, "Unwrap", test+"->"+wrapToken, expected, res)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user