1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-05 21:32:27 +08:00

Compare commits

..

15 Commits

Author SHA1 Message Date
dudaodong
2e04a41f34 update version 2021-12-01 11:34:53 +08:00
dudaodong
acb7873832 fix: setQueryParam failed in request_util.go 2021-12-01 11:34:18 +08:00
dudaodong
8b3cc3266d fix misspel in README_zh-CN.md 2021-11-30 09:51:21 +08:00
dudaodong
62c570d29b update version 2021-11-29 20:24:28 +08:00
dudaodong
4e5d3c2603 fix: fix IsExist function in fileutil/file.go 2021-11-29 20:23:26 +08:00
dudaodong
561b590e13 update: fix misspell in readme file 2021-11-29 20:02:55 +08:00
dudaodong
f2ed3c6270 test: add some unit test function for convertor, random, slice, and formatter 2021-11-29 17:27:02 +08:00
dudaodong
fee6cb17f3 update: update validator test function 2021-11-29 15:53:48 +08:00
dudaodong
7d39d1319b add github workflow 2021-11-29 15:19:52 +08:00
dudaodong
59d7281967 Merge branch 'main' of github.com:duke-git/lancet into main 2021-11-29 15:06:59 +08:00
dudaodong
bdf052819d delete .travis.yml 2021-11-29 15:06:52 +08:00
dudaodong
0148af4839 update: readme file 2021-11-29 14:41:14 +08:00
Beyond
99b74e6fc6 add codecov.yml 2021-11-29 14:37:35 +08:00
dudaodong
499c1df9bd update: add godoc, goreport, lincense badges in readme file, add .travis.yml 2021-11-29 14:19:41 +08:00
dudaodong
6af13a01de update: add godoc, goreport, lincense badges in readme file, add .travis.yml 2021-11-29 14:19:23 +08:00
15 changed files with 139 additions and 74 deletions

22
.github/workflows/codecov.yml vendored Normal file
View File

@@ -0,0 +1,22 @@
name: Test and coverage
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2
- uses: actions/setup-go@v2
with:
go-version: "1.16"
- name: Run coverage
run: go test -v ./... -race -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage to Codecov
run: bash <(curl -s https://codecov.io/bash)

5
.gitignore vendored
View File

@@ -1,3 +1,6 @@
.idea/*
.vscode/*
.DS_Store
.DS_Store
cryptor/*.txt
fileutil/*.txt
cryptor/*.pem

View File

@@ -5,6 +5,13 @@
</p>
<div align="center" style="text-align: center;">
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-9cf)
[![Release](https://img.shields.io/badge/release-1.0.5-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet)
[![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/duke-git/lancet/blob/main/LICENSE)
</div>
English | [简体中文](./README_zh-CN.md)

View File

@@ -5,6 +5,12 @@
</p>
<div align="center" style="text-align: center;">
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-9cf)
[![Release](https://img.shields.io/badge/release-1.0.5-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet)
[![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/duke-git/lancet/blob/main/LICENSE)
</div>
@@ -17,7 +23,7 @@
- 👏 全面、高效、可复用
- 💪 100+常用go工具函数支持string、slice、datetime、net、crypt...
- 💅 只依赖go标准库
- 🌍 所有导出函数单测试覆盖率100%
- 🌍 所有导出函数单测试覆盖率100%
### 安装
@@ -57,7 +63,7 @@ func main() {
#### 1. convertor数据转换包
- 转换函数支持常用数据类型之间的转换
- 导入包import "github.com/duke-git/lancet/cryptor"
- 导入包import "github.com/duke-git/lancet/convertor"
```go
package main
@@ -94,7 +100,7 @@ func StructToMap(value interface{}) (map[string]interface{}, error) //struct串
#### 2. cryptor加解密包
- 加密函数支持md5, hmac, aes, des, ras
- 加密函数支持md5, hmac, aes, des, ras
- 导入包import "github.com/duke-git/lancet/cryptor"
```go

View File

@@ -2,9 +2,10 @@ package convertor
import (
"fmt"
"github.com/duke-git/lancet/utils"
"reflect"
"testing"
"github.com/duke-git/lancet/utils"
)
func TestToChar(t *testing.T) {
@@ -58,8 +59,12 @@ func TestToBytes(t *testing.T) {
}
func TestToInt(t *testing.T) {
cases := []interface{}{"123", "-123", 123, "abc", false, "111111111111111111111111111111111111111"}
expected := []int64{123, -123, 123, 0, 0, 0}
cases := []interface{}{"123", "-123", 123,
uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
float32(12.3), float64(12.3),
"abc", false, "111111111111111111111111111111111111111"}
expected := []int64{123, -123, 123, 123, 123, 123, 123, 123, 12, 12, 0, 0, 0}
for i := 0; i < len(cases); i++ {
res, _ := ToInt(cases[i])
@@ -71,8 +76,14 @@ func TestToInt(t *testing.T) {
}
func TestToFloat(t *testing.T) {
cases := []interface{}{"", "-1", "-.11", "1.23e3", ".123e10", "abc"}
expected := []float64{0, -1, -0.11, 1230, 0.123e10, 0}
cases := []interface{}{
"", "-1", "-.11", "1.23e3", ".123e10", "abc",
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),
}
expected := []float64{0, -1, -0.11, 1230, 0.123e10, 0,
0, 1, -1, 123, 123, 123, 123, 123, 123, 123, 12.3, 12.300000190734863}
for i := 0; i < len(cases); i++ {
res, _ := ToFloat(cases[i])
@@ -84,41 +95,37 @@ func TestToFloat(t *testing.T) {
}
func TestToString(t *testing.T) {
// basic type
toString(t, "a1", "a1")
toString(t, 111, "111")
toString(t, 111.01, "111.01")
toString(t, true, "true")
//toString(t, 1.5+10i, "(1.5+10i)")
// slice
aSlice := []int{1, 2, 3}
toString(t, aSlice, "[1,2,3]")
// map
aMap := make(map[string]int)
aMap["a"] = 1
aMap["b"] = 2
aMap["c"] = 3
toString(t, aMap, "{\"a\":1,\"b\":2,\"c\":3}")
// struct
type TestStruct struct {
Name string
}
aStruct := TestStruct{Name: "TestStruct"}
toString(t, aStruct, "{\"Name\":\"TestStruct\"}")
}
func toString(t *testing.T, test interface{}, expected string) {
res := ToString(test)
if res != expected {
utils.LogFailedTestInfo(t, "ToString", test, expected, res)
t.FailNow()
cases := []interface{}{
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",
"[1,2,3]", "{\"a\":1,\"b\":2,\"c\":3}", "{\"Name\":\"TestStruct\"}", "hello"}
for i := 0; i < len(cases); i++ {
res := ToString(cases[i])
if res != expected[i] {
utils.LogFailedTestInfo(t, "ToString", cases[i], expected[i], res)
t.FailNow()
}
}
}
func TestToJson(t *testing.T) {
// map
aMap := make(map[string]int)

View File

@@ -17,7 +17,7 @@ func IsExist(path string) bool {
if err == nil {
return true
}
if errors.Is(err, os.ErrExist) {
if errors.Is(err, os.ErrNotExist) {
return false
}
return false

View File

@@ -9,8 +9,8 @@ import (
)
func TestIsExist(t *testing.T) {
cases := []string{"./", "./a.txt"}
expected := []bool{true, false}
cases := []string{"./", "./file.go", "./a.txt"}
expected := []bool{true, true, false}
for i := 0; i < len(cases); i++ {
res := IsExist(cases[i])
@@ -87,11 +87,11 @@ func TestCopyFile(t *testing.T) {
}
func TestListFileNames(t *testing.T) {
filesInCurrentPath, err := ListFileNames("./")
filesInCurrentPath, err := ListFileNames("../datetime/")
if err != nil {
t.FailNow()
}
expected := []string{"file.go", "file_test.go"}
expected := []string{"datetime.go", "datetime_test.go"}
if !reflect.DeepEqual(filesInCurrentPath, expected) {
utils.LogFailedTestInfo(t, "ToChar", "./", expected, filesInCurrentPath)
t.FailNow()

View File

@@ -9,6 +9,8 @@ import (
func TestComma(t *testing.T) {
comma(t, "", "", "")
comma(t, "aa", "", "")
comma(t, "aa.a", "", "")
comma(t, []int{1}, "", "")
comma(t, "123", "", "123")
comma(t, "12345", "", "12,345")
comma(t, 12345, "", "12,345")

View File

@@ -11,6 +11,11 @@ import (
)
func TestHttpGet(t *testing.T) {
_, e := HttpGet("", nil)
if e == nil {
t.FailNow()
}
url := "https://gutendex.com/books?"
queryParams := make(map[string]interface{})
queryParams["ids"] = "1"

View File

@@ -124,7 +124,7 @@ func setQueryParam(req *http.Request, reqUrl string, queryParam interface{}) err
case map[string]interface{}:
values = url.Values{}
for k := range v {
values.Set(k, fmt.Sprintf("%s", v[k]))
values.Set(k, fmt.Sprintf("%v", v[k]))
}
case url.Values:
values = v

View File

@@ -2,10 +2,11 @@ package random
import (
"fmt"
"github.com/duke-git/lancet/utils"
"reflect"
"regexp"
"testing"
"github.com/duke-git/lancet/utils"
)
func TestRandString(t *testing.T) {
@@ -21,26 +22,44 @@ func TestRandString(t *testing.T) {
}
func TestRandInt(t *testing.T) {
randInt := RandInt(1, 10)
res1 := RandInt(1, 10)
if res1 < 1 || res1 >= 10 {
utils.LogFailedTestInfo(t, "RandInt", "RandInt(1, 10)", "RandInt(1, 10) should between [1, 10) ", res1)
t.FailNow()
}
if randInt < 1 || randInt >= 10 {
utils.LogFailedTestInfo(t, "RandInt", "RandInt(1, 10)", "RandInt(1, 10) should between [1, 10) ", randInt)
res2 := RandInt(1, 1)
if res2 != 1 {
utils.LogFailedTestInfo(t, "RandInt", "RandInt(1, 1)", "RandInt(1, 1) should be 1 ", res2)
t.FailNow()
}
res3 := RandInt(10, 1)
if res3 < 1 || res3 >= 10 {
utils.LogFailedTestInfo(t, "RandInt", "RandInt(10, 1)", "RandInt(10, 1) should between [1, 10) ", res3)
t.FailNow()
}
}
func TestRandBytes(t *testing.T) {
randBytes := RandBytes(4)
if len(randBytes) != 4 {
utils.LogFailedTestInfo(t, "RandBytes", "RandBytes(4)", "RandBytes(4) should return 4 element of []bytes", randBytes)
t.FailNow()
}
v := reflect.ValueOf(randBytes)
et := v.Type().Elem()
if v.Kind() != reflect.Slice || et.Kind() != reflect.Uint8 {
utils.LogFailedTestInfo(t, "RandBytes", "RandBytes(4)", "RandBytes(4) should return 4 element of []bytes", randBytes)
t.FailNow()
}
randErr := RandBytes(0)
if randErr != nil {
utils.LogFailedTestInfo(t, "RandBytes", "RandBytes(0)", "RandBytes(0) should return nil", randErr)
t.FailNow()
}
}

View File

@@ -137,17 +137,16 @@ func Map(slice, function interface{}) interface{} {
// The function signature should be func(index int, value1, value2 interface{}) interface{} .
func Reduce(slice, function, zero interface{}) interface{} {
sv := sliceValue(slice)
elementType := sv.Type().Elem()
len := sv.Len()
if len == 0 {
return zero
} else if len == 1 {
return sv.Index(0)
return sv.Index(0).Interface()
}
elementType := sv.Type().Elem()
fn := functionValue(function)
if checkSliceCallbackFuncSignature(fn, elementType, elementType, elementType) {
t := elementType.String()
panic("Reduce function must be of type func(int, " + t + ", " + t + ")" + t)

View File

@@ -14,6 +14,15 @@ func TestContain(t *testing.T) {
var t2 []string
contain(t, t2, "1", false)
m := make(map[string]int)
m["a"] = 1
contain(t, m, "a", true)
contain(t, m, "b", false)
s := "hello"
contain(t, s, "h", true)
contain(t, s, "s", false)
}
func contain(t *testing.T, test interface{}, value interface{}, expected bool) {
@@ -167,28 +176,21 @@ func TestMap(t *testing.T) {
}
func TestReduce(t *testing.T) {
s1 := []int{1, 2, 3, 4}
f1 := func(i, v1, v2 int) int {
cases := [][]int{
{},
{1},
{1, 2, 3, 4}}
expected := []int{0, 1, 10}
f := func(i, v1, v2 int) int {
return v1 + v2
}
e1 := 10
r1 := Reduce(s1, f1, 0)
if e1 != r1 {
utils.LogFailedTestInfo(t, "Reduce", s1, e1, r1)
t.FailNow()
for i := 0; i < len(cases); i++ {
res := Reduce(cases[i], f, 0)
if res != expected[i] {
utils.LogFailedTestInfo(t, "Reduce", cases[i], expected[i], res)
t.FailNow()
}
}
// failed Reduce function should be func(i int, v1, v2 int) int
//s1 := []int{1, 2, 3, 4}
//f1 := func(i string, v1, v2 int) int { //i should be int
// return v1+v2
//}
//e1 := 10
//r1 := Reduce(s1, f1, 0)
//if e1 != r1 {
// utils.LogFailedTestInfo(t, "Reduce", s1, e1, r1)
// t.FailNow()
//}
}
func TestIntSlice(t *testing.T) {

View File

@@ -26,11 +26,7 @@ func IsNumberStr(s string) bool {
// IsFloatStr check if the string can convert to a float.
func IsFloatStr(s string) bool {
_, e := strconv.ParseFloat(s, 64)
if e != nil {
return false
}
return true
return e == nil
}
// IsIntStr check if the string can convert to a integer.
@@ -42,10 +38,7 @@ func IsIntStr(s string) bool {
// IsIp check if the string is a ip address.
func IsIp(ipstr string) bool {
ip := net.ParseIP(ipstr)
if ip == nil {
return false
}
return true
return ip != nil
}
// IsIpV4 check if the string is a ipv4 address.

View File

@@ -190,7 +190,7 @@ func isCreditCard(t *testing.T, source string, expected bool) {
}
func TestIsBase64(t *testing.T) {
isBase64(t, "aGVsbG8", true)
isBase64(t, "aGVsbG8=", true)
isBase64(t, "123456", false)
}