sms_tool
This commit is contained in:
78
common_fun/string_func_test.go
Normal file
78
common_fun/string_func_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package cf
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Test IsMobile
|
||||
func TestIsMobile(t *testing.T) {
|
||||
valid := []string{
|
||||
"13800138000",
|
||||
"19999999999",
|
||||
"18888888888",
|
||||
}
|
||||
invalid := []string{
|
||||
"23800138000",
|
||||
"10000000000",
|
||||
"12345",
|
||||
"abc13800138000",
|
||||
"",
|
||||
}
|
||||
|
||||
for _, v := range valid {
|
||||
if !IsMobile(v) {
|
||||
t.Errorf("IsMobile(%q) = false, want true", v)
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range invalid {
|
||||
if IsMobile(v) {
|
||||
t.Errorf("IsMobile(%q) = true, want false", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test StrRand
|
||||
func TestStrRand(t *testing.T) {
|
||||
length := 16
|
||||
s := StrRand(length)
|
||||
if len(s) != length {
|
||||
t.Errorf("StrRand length = %d, want %d", len(s), length)
|
||||
}
|
||||
|
||||
// check character set
|
||||
matched, _ := regexp.MatchString(`^[0-9a-zA-Z]+$`, s)
|
||||
if !matched {
|
||||
t.Errorf("StrRand returned invalid characters: %s", s)
|
||||
}
|
||||
}
|
||||
|
||||
// Test StrCharRand
|
||||
func TestStrCharRand(t *testing.T) {
|
||||
length := 10
|
||||
s := StrCharRand(length)
|
||||
if len(s) != length {
|
||||
t.Errorf("StrCharRand length = %d, want %d", len(s), length)
|
||||
}
|
||||
matched, _ := regexp.MatchString(`^[a-zA-Z]+$`, s)
|
||||
if !matched {
|
||||
t.Errorf("StrCharRand returned invalid characters: %s", s)
|
||||
}
|
||||
}
|
||||
|
||||
// Test StrNumberRand
|
||||
func TestStrNumberRand(t *testing.T) {
|
||||
length := 8
|
||||
s := StrNumberRand(length)
|
||||
if len(s) != length {
|
||||
t.Errorf("StrNumberRand length = %d, want %d", len(s), length)
|
||||
}
|
||||
matched, _ := regexp.MatchString(`^[0-9]+$`, s)
|
||||
if !matched {
|
||||
t.Errorf("StrNumberRand returned invalid characters: %s", s)
|
||||
}
|
||||
if s[0] == '0' {
|
||||
t.Errorf("StrNumberRand starts with 0, which is not allowed: %s", s)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user