refactor: rewrite all unit test functions with assert

This commit is contained in:
dudaodong
2022-01-09 15:48:29 +08:00
parent b0e17c7bc4
commit 1199c30ef3
2 changed files with 26 additions and 28 deletions

View File

@@ -6,6 +6,8 @@ import (
"strings"
"testing"
"time"
"github.com/duke-git/lancet/internal"
)
func TestAfter(t *testing.T) {
@@ -32,6 +34,8 @@ func TestAfter(t *testing.T) {
}
func TestBefore(t *testing.T) {
assert := internal.NewAssert(t, "TestBefore")
arr := []string{"a", "b", "c", "d", "e"}
f := Before(3, func(i int) int {
return i
@@ -40,7 +44,6 @@ func TestBefore(t *testing.T) {
var res []int64
type cb func(args ...interface{}) []reflect.Value
appendStr := func(i int, s string, fn cb) {
fmt.Printf("appendStr: arr[%d] is %s \n", i, s)
v := fn(i)
res = append(res, v[0].Int())
}
@@ -49,28 +52,26 @@ func TestBefore(t *testing.T) {
appendStr(i, arr[i], f)
}
expect := []int64{0, 1, 2, 2, 2}
if !reflect.DeepEqual(expect, res) {
t.FailNow()
}
expected := []int64{0, 1, 2, 2, 2}
assert.Equal(expected, res)
}
func TestCurry(t *testing.T) {
assert := internal.NewAssert(t, "TestCurry")
add := func(a, b int) int {
return a + b
}
var addCurry Fn = func(values ...interface{}) interface{} {
return add(values[0].(int), values[1].(int))
}
add1 := addCurry.Curry(1)
v := add1(2)
if v != 3 {
t.FailNow()
}
assert.Equal(3, add1(2))
}
func TestCompose(t *testing.T) {
assert := internal.NewAssert(t, "TestCompose")
toUpper := func(a ...interface{}) interface{} {
return strings.ToUpper(a[0].(string))
}
@@ -78,27 +79,25 @@ func TestCompose(t *testing.T) {
return strings.ToLower(a[0].(string))
}
expect := toUpper(toLower("aBCde"))
expected := toUpper(toLower("aBCde"))
cf := Compose(toUpper, toLower)
res := cf("aBCde")
if res != expect {
t.FailNow()
}
assert.Equal(expected, res)
}
func TestDelay(t *testing.T) {
var print = func(s string) {
fmt.Println(s)
t.Log(s)
}
Delay(2*time.Second, print, "test delay")
}
func TestSchedule(t *testing.T) {
assert := internal.NewAssert(t, "TestSchedule")
var res []string
appendStr := func(s string) {
fmt.Println(s)
res = append(res, s)
}
@@ -106,9 +105,6 @@ func TestSchedule(t *testing.T) {
time.Sleep(5 * time.Second)
close(stop)
expect := []string{"*", "*", "*", "*", "*"}
if !reflect.DeepEqual(expect, res) {
t.FailNow()
}
fmt.Println("done")
expected := []string{"*", "*", "*", "*", "*"}
assert.Equal(expected, res)
}