From 454efd486df9ff5e1c33f3cfe508e15bf6d34160 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 19 Jan 2022 19:45:18 +0800 Subject: [PATCH 1/3] test: add test logic for assert --- internal/assert_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/internal/assert_test.go b/internal/assert_test.go index 185dd79..ae0bcfa 100644 --- a/internal/assert_test.go +++ b/internal/assert_test.go @@ -8,11 +8,31 @@ func TestAssert(t *testing.T) { assert := NewAssert(t, "TestAssert") assert.Equal(0, 0) assert.NotEqual(1, 0) + + assert.NotEqual("1", 1) + var uInt1 uint + var uInt2 uint + var uInt8 uint8 + var uInt16 uint16 + var uInt32 uint32 + var uInt64 uint64 + assert.NotEqual(uInt1, uInt8) + assert.NotEqual(uInt8, uInt16) + assert.NotEqual(uInt16, uInt32) + assert.NotEqual(uInt32, uInt64) + + assert.Equal(uInt1, uInt2) + + uInt1 = 1 + uInt2 = 2 + assert.Less(uInt1, uInt2) + assert.Greater(1, 0) assert.GreaterOrEqual(1, 1) assert.Less(0, 1) assert.LessOrEqual(0, 0) + assert.Equal(0.1, 0.1) assert.Greater(1.1, 0.1) assert.Less(0.1, 1.1) From 2ab898741dee4830c5aec0af00826a30b5fa439d Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 19 Jan 2022 20:18:04 +0800 Subject: [PATCH 2/3] test: add TestOsDetection --- system/os_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/system/os_test.go b/system/os_test.go index 6e04008..1d5ac1f 100644 --- a/system/os_test.go +++ b/system/os_test.go @@ -1,11 +1,24 @@ package system import ( + "strings" "testing" "github.com/duke-git/lancet/internal" ) +func TestOsDetection(t *testing.T) { + assert := internal.NewAssert(t, "TestOsJudgment") + + osType, _, _ := ExecCommand("echo $OSTYPE") + if strings.Index(osType, "linux") != -1 { + assert.Equal(true, IsLinux()) + } + if strings.Index(osType, "darwin") != -1 { + assert.Equal(true, IsMac()) + } +} + func TestOsEnvOperation(t *testing.T) { assert := internal.NewAssert(t, "TestOsEnvOperation") @@ -18,6 +31,14 @@ func TestOsEnvOperation(t *testing.T) { assert.Equal(true, CompareOsEnv("foo", "foo_value")) assert.Equal(false, CompareOsEnv("foo", "abc")) + assert.Equal(false, CompareOsEnv("abc", "abc")) + assert.Equal(false, CompareOsEnv("abc", "abc")) + + err := RemoveOsEnv("foo") + if err != nil { + t.Fail() + } + assert.Equal(false, CompareOsEnv("foo", "foo_value")) } func TestExecCommand(t *testing.T) { From 28317a168326e4ec3d348980c0d8843a881c01fe Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 19 Jan 2022 20:41:46 +0800 Subject: [PATCH 3/3] feat: remove ConvertSlice func --- README.md | 1 - README_zh-CN.md | 1 - slice/slice.go | 21 --------------------- slice/slice_test.go | 14 +------------- 4 files changed, 1 insertion(+), 36 deletions(-) diff --git a/README.md b/README.md index 9aa3765..3dfee18 100644 --- a/README.md +++ b/README.md @@ -434,7 +434,6 @@ func main() { func Contain(slice interface{}, value interface{}) bool //check if the value is in the slice or not func ContainSubSlice(slice interface{}, subslice interface{}) bool //check if the slice contain subslice or not func Chunk(slice []interface{}, size int) [][]interface{} //creates an slice of elements split into groups the length of `size` -func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //convert originalSlice to newSliceType func Difference(slice1, slice2 interface{}) interface{} //creates an slice of whose element not included in the other given slice 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 diff --git a/README_zh-CN.md b/README_zh-CN.md index fdc9a70..b9a653b 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -434,7 +434,6 @@ func main() { func Contain(slice interface{}, value interface{}) bool //判断slice是否包含value func ContainSubSlice(slice interface{}, subslice interface{}) bool //判断slice是否包含subslice func Chunk(slice []interface{}, size int) [][]interface{} //均分slice -func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //将originalSlice转换为 newSliceType func Difference(slice1, slice2 interface{}) interface{} //返回切片,其元素在slice1中,不在slice2中 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个元素 diff --git a/slice/slice.go b/slice/slice.go index 3af5f37..ea9e1f8 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -12,7 +12,6 @@ import ( "reflect" "sort" "strings" - "unsafe" ) // Contain check if the value is in the iterable type or not @@ -427,26 +426,6 @@ func IntSlice(slice interface{}) []int { return out } -// ConvertSlice convert original slice to new data type element of slice. -func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} { - sv := sliceValue(originalSlice) - if newSliceType.Kind() != reflect.Slice { - panic(fmt.Sprintf("Invalid newSliceType(non-slice type of type %T)", newSliceType)) - } - - newSlice := reflect.New(newSliceType) - - hdr := (*reflect.SliceHeader)(unsafe.Pointer(newSlice.Pointer())) - - var newElemSize = int(sv.Type().Elem().Size()) / int(newSliceType.Elem().Size()) - - hdr.Cap = sv.Cap() * newElemSize - hdr.Len = sv.Len() * newElemSize - hdr.Data = sv.Pointer() - - return newSlice.Elem().Interface() -} - // DeleteByIndex delete the element of slice from start index to end index - 1. // Delete i: s = append(s[:i], s[i+1:]...) // Delete i to j: s = append(s[:i], s[j:]...) diff --git a/slice/slice_test.go b/slice/slice_test.go index cb8763b..ea452d8 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -74,19 +74,6 @@ func TestChunk(t *testing.T) { assert.Equal(r5, Chunk(InterfaceSlice(arr), 5)) } -func TestConvertSlice(t *testing.T) { - //t1 := []string{"1","2"} - //aInt, _ := strconv.ParseInt("1", 10, 64) - //bInt, _ := strconv.ParseInt("2", 10, 64) - //expected :=[]int64{aInt, bInt} - // - //a := ConvertSlice(t1, reflect.TypeOf(expected)) - //if !reflect.DeepEqual(a, expected) { - // utils.LogFailedTestInfo(t, "ConvertSlice", t1, expected, a) - // t.FailNow() - //} -} - func TestEvery(t *testing.T) { nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool { @@ -430,6 +417,7 @@ func TestIntersection(t *testing.T) { for i := 0; i < len(res); i++ { assert.Equal(res[i], expected[i]) } + assert.IsNil(Intersection()) } func TestReverseSlice(t *testing.T) {