mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
test: add some unit test function for convertor, random, slice, and formatter
This commit is contained in:
@@ -2,10 +2,11 @@ package random
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/utils"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/utils"
|
||||
)
|
||||
|
||||
func TestRandString(t *testing.T) {
|
||||
@@ -21,26 +22,44 @@ func TestRandString(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRandInt(t *testing.T) {
|
||||
randInt := RandInt(1, 10)
|
||||
res1 := RandInt(1, 10)
|
||||
if res1 < 1 || res1 >= 10 {
|
||||
utils.LogFailedTestInfo(t, "RandInt", "RandInt(1, 10)", "RandInt(1, 10) should between [1, 10) ", res1)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if randInt < 1 || randInt >= 10 {
|
||||
utils.LogFailedTestInfo(t, "RandInt", "RandInt(1, 10)", "RandInt(1, 10) should between [1, 10) ", randInt)
|
||||
res2 := RandInt(1, 1)
|
||||
if res2 != 1 {
|
||||
utils.LogFailedTestInfo(t, "RandInt", "RandInt(1, 1)", "RandInt(1, 1) should be 1 ", res2)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
res3 := RandInt(10, 1)
|
||||
if res3 < 1 || res3 >= 10 {
|
||||
utils.LogFailedTestInfo(t, "RandInt", "RandInt(10, 1)", "RandInt(10, 1) should between [1, 10) ", res3)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandBytes(t *testing.T) {
|
||||
randBytes := RandBytes(4)
|
||||
|
||||
if len(randBytes) != 4 {
|
||||
utils.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 {
|
||||
utils.LogFailedTestInfo(t, "RandBytes", "RandBytes(4)", "RandBytes(4) should return 4 element of []bytes", randBytes)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
randErr := RandBytes(0)
|
||||
if randErr != nil {
|
||||
utils.LogFailedTestInfo(t, "RandBytes", "RandBytes(0)", "RandBytes(0) should return nil", randErr)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user