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

refactor: rewrite all unit test functions with assert

This commit is contained in:
dudaodong
2022-01-09 13:51:07 +08:00
parent f490ef2404
commit 3ffd81a98a

View File

@@ -9,7 +9,6 @@
package random package random
import ( import (
"fmt"
"reflect" "reflect"
"regexp" "regexp"
"testing" "testing"
@@ -18,54 +17,40 @@ import (
) )
func TestRandString(t *testing.T) { func TestRandString(t *testing.T) {
randStr := RandString(6)
fmt.Println(randStr)
pattern := `^[a-zA-Z]+$` pattern := `^[a-zA-Z]+$`
reg := regexp.MustCompile(pattern) reg := regexp.MustCompile(pattern)
if len(randStr) != 6 || !reg.MatchString(randStr) { randStr := RandString(6)
internal.LogFailedTestInfo(t, "RandString", "RandString(6)", "RandString(6) should be 6 letters ", randStr)
t.FailNow() assert := internal.NewAssert(t, "TestRandString")
} assert.Equal(6, len(randStr))
assert.Equal(true, reg.MatchString(randStr))
} }
func TestRandInt(t *testing.T) { func TestRandInt(t *testing.T) {
res1 := RandInt(1, 10) assert := internal.NewAssert(t, "TestRandInt")
if res1 < 1 || res1 >= 10 {
internal.LogFailedTestInfo(t, "RandInt", "RandInt(1, 10)", "RandInt(1, 10) should between [1, 10) ", res1)
t.FailNow()
}
res2 := RandInt(1, 1) r1 := RandInt(1, 10)
if res2 != 1 { assert.Greater(r1, 1)
internal.LogFailedTestInfo(t, "RandInt", "RandInt(1, 1)", "RandInt(1, 1) should be 1 ", res2) assert.Less(r1, 10)
t.FailNow()
}
res3 := RandInt(10, 1) r2 := RandInt(1, 1)
if res3 < 1 || res3 >= 10 { assert.Equal(1, r2)
internal.LogFailedTestInfo(t, "RandInt", "RandInt(10, 1)", "RandInt(10, 1) should between [1, 10) ", res3)
t.FailNow() r3 := RandInt(10, 1)
} assert.Greater(r1, 1)
assert.Less(r3, 10)
} }
func TestRandBytes(t *testing.T) { func TestRandBytes(t *testing.T) {
randBytes := RandBytes(4) assert := internal.NewAssert(t, "TestRandBytes")
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()
}
randErr := RandBytes(0) randBytes := RandBytes(4)
if randErr != nil { assert.Equal(4, len(randBytes))
internal.LogFailedTestInfo(t, "RandBytes", "RandBytes(0)", "RandBytes(0) should return nil", randErr)
t.FailNow() v := reflect.ValueOf(randBytes)
} elemType := v.Type().Elem()
assert.Equal(reflect.Slice, v.Kind())
assert.Equal(reflect.Uint8, elemType.Kind())
} }