From 1199c30ef3245b78eb6fbc7968c679bfd05058e6 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sun, 9 Jan 2022 15:48:29 +0800 Subject: [PATCH] refactor: rewrite all unit test functions with assert --- function/function_test.go | 40 ++++++++++++++++++--------------------- function/watcher_test.go | 14 ++++++++------ 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/function/function_test.go b/function/function_test.go index bed16d0..5ab909b 100644 --- a/function/function_test.go +++ b/function/function_test.go @@ -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) } diff --git a/function/watcher_test.go b/function/watcher_test.go index dec36eb..9df0036 100644 --- a/function/watcher_test.go +++ b/function/watcher_test.go @@ -2,26 +2,28 @@ package function import ( "testing" + + "github.com/duke-git/lancet/internal" ) func TestWatcher(t *testing.T) { + assert := internal.NewAssert(t, "TestWatcher") + w := &Watcher{} w.Start() longRunningTask() - if !w.excuting { - t.FailNow() - } + assert.Equal(true, w.excuting) w.Stop() eapsedTime := w.GetElapsedTime().Milliseconds() t.Log("Elapsed Time (milsecond)", eapsedTime) - if w.excuting { - t.FailNow() - } + assert.Equal(false, w.excuting) + + w.Reset() } func longRunningTask() {