From 3ffd81a98a7b2479260e6aeaea23a2437c879d2f Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sun, 9 Jan 2022 13:51:07 +0800 Subject: [PATCH] refactor: rewrite all unit test functions with assert --- random/random_test.go | 61 ++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/random/random_test.go b/random/random_test.go index 6e7d845..bd09ed3 100644 --- a/random/random_test.go +++ b/random/random_test.go @@ -9,7 +9,6 @@ package random import ( - "fmt" "reflect" "regexp" "testing" @@ -18,54 +17,40 @@ import ( ) func TestRandString(t *testing.T) { - randStr := RandString(6) - fmt.Println(randStr) pattern := `^[a-zA-Z]+$` reg := regexp.MustCompile(pattern) - if len(randStr) != 6 || !reg.MatchString(randStr) { - internal.LogFailedTestInfo(t, "RandString", "RandString(6)", "RandString(6) should be 6 letters ", randStr) - t.FailNow() - } + randStr := RandString(6) + + assert := internal.NewAssert(t, "TestRandString") + assert.Equal(6, len(randStr)) + assert.Equal(true, reg.MatchString(randStr)) } func TestRandInt(t *testing.T) { - res1 := RandInt(1, 10) - if res1 < 1 || res1 >= 10 { - internal.LogFailedTestInfo(t, "RandInt", "RandInt(1, 10)", "RandInt(1, 10) should between [1, 10) ", res1) - t.FailNow() - } + assert := internal.NewAssert(t, "TestRandInt") - res2 := RandInt(1, 1) - if res2 != 1 { - internal.LogFailedTestInfo(t, "RandInt", "RandInt(1, 1)", "RandInt(1, 1) should be 1 ", res2) - t.FailNow() - } + r1 := RandInt(1, 10) + assert.Greater(r1, 1) + assert.Less(r1, 10) - res3 := RandInt(10, 1) - if res3 < 1 || res3 >= 10 { - internal.LogFailedTestInfo(t, "RandInt", "RandInt(10, 1)", "RandInt(10, 1) should between [1, 10) ", res3) - t.FailNow() - } + r2 := RandInt(1, 1) + assert.Equal(1, r2) + + r3 := RandInt(10, 1) + assert.Greater(r1, 1) + assert.Less(r3, 10) } func TestRandBytes(t *testing.T) { - randBytes := RandBytes(4) - if len(randBytes) != 4 { - internal.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 { - internal.LogFailedTestInfo(t, "RandBytes", "RandBytes(4)", "RandBytes(4) should return 4 element of []bytes", randBytes) - t.FailNow() - } + assert := internal.NewAssert(t, "TestRandBytes") - randErr := RandBytes(0) - if randErr != nil { - internal.LogFailedTestInfo(t, "RandBytes", "RandBytes(0)", "RandBytes(0) should return nil", randErr) - t.FailNow() - } + randBytes := RandBytes(4) + assert.Equal(4, len(randBytes)) + + v := reflect.ValueOf(randBytes) + elemType := v.Type().Elem() + assert.Equal(reflect.Slice, v.Kind()) + assert.Equal(reflect.Uint8, elemType.Kind()) }