mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-06 05:42:25 +08:00
refactor: rewrite all unit test functions with assert
This commit is contained in:
@@ -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())
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user