1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 09:12:28 +08:00

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

View File

@@ -2,26 +2,28 @@ package function
import ( import (
"testing" "testing"
"github.com/duke-git/lancet/internal"
) )
func TestWatcher(t *testing.T) { func TestWatcher(t *testing.T) {
assert := internal.NewAssert(t, "TestWatcher")
w := &Watcher{} w := &Watcher{}
w.Start() w.Start()
longRunningTask() longRunningTask()
if !w.excuting { assert.Equal(true, w.excuting)
t.FailNow()
}
w.Stop() w.Stop()
eapsedTime := w.GetElapsedTime().Milliseconds() eapsedTime := w.GetElapsedTime().Milliseconds()
t.Log("Elapsed Time (milsecond)", eapsedTime) t.Log("Elapsed Time (milsecond)", eapsedTime)
if w.excuting { assert.Equal(false, w.excuting)
t.FailNow()
} w.Reset()
} }
func longRunningTask() { func longRunningTask() {