feat(stringx): 自 go-hua/common 迁入字符串/切片/拼音工具并独立成包
- common 的 string.go/array.go/pinyin.go 拆为 stringx 包(含随机串、GUID、 通配符匹配、校验规则、链式缓冲、驼峰转下划线、切片删元素、拼音首字母) - 附单元测试与 examples/stringx 可运行例程、包 README - go.mod 新增直接依赖 mozillazg/go-pinyin(go-hua 同版本锁 v0.20.0→实际解析 v0.21.0)
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
package stringx
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateRandomString(t *testing.T) {
|
||||
s := CreateRandomString(16)
|
||||
if len(s) != 16 {
|
||||
t.Errorf("CreateRandomString(16) 长度 = %d, want 16", len(s))
|
||||
}
|
||||
for _, c := range s {
|
||||
if !strings.ContainsRune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", c) {
|
||||
t.Errorf("字符 %q 不在允许字符集内", c)
|
||||
}
|
||||
}
|
||||
if s2 := CreateRandomString(16); s2 == s {
|
||||
t.Error("两次生成结果不应相同")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRandomStringInt(t *testing.T) {
|
||||
s := CreateRandomStringInt(8)
|
||||
if len(s) != 8 {
|
||||
t.Errorf("长度 = %d, want 8", len(s))
|
||||
}
|
||||
for _, c := range s {
|
||||
if c < '0' || c > '9' {
|
||||
t.Errorf("字符 %q 应为数字", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRandomStringHex(t *testing.T) {
|
||||
s := CreateRandomStringHex(8)
|
||||
if len(s) != 8 {
|
||||
t.Errorf("长度 = %d, want 8", len(s))
|
||||
}
|
||||
for _, c := range s {
|
||||
if !strings.ContainsRune("ABCDEF1234567890", c) {
|
||||
t.Errorf("字符 %q 不在十六进制字符集内", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetBetweenStr(t *testing.T) {
|
||||
if got := GetBetweenStr("key=value&x=1", "key=", "&"); got != "value" {
|
||||
t.Errorf("GetBetweenStr = %q, want value", got)
|
||||
}
|
||||
if got := GetBetweenStr("abc", "x", "y"); got != "" {
|
||||
t.Errorf("起始不存在应返回空: %q", got)
|
||||
}
|
||||
if got := GetBetweenStr("a<..>b", "<..>", ""); got != "b" {
|
||||
t.Errorf("结束为空应返回到结尾: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetGUIDFormat(t *testing.T) {
|
||||
guid := GetGUID()
|
||||
if strings.Count(guid, "-") != 4 {
|
||||
t.Errorf("GetGUID 应为 8-4-4-4-12 格式: %q", guid)
|
||||
}
|
||||
if len(strings.ReplaceAll(guid, "-", "")) != 32 {
|
||||
t.Errorf("GetGUID 总长应为 32 位十六进制: %q", guid)
|
||||
}
|
||||
uuid := GetUUID()
|
||||
if strings.Contains(uuid, "-") {
|
||||
t.Errorf("GetUUID 不应含连字符: %q", uuid)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckMobile(t *testing.T) {
|
||||
if !CheckMobile("13812345678") {
|
||||
t.Error("合法手机号应通过")
|
||||
}
|
||||
if CheckMobile("12345678901") {
|
||||
t.Error("第二位为 2 应不通过")
|
||||
}
|
||||
if CheckMobile("1381234567") {
|
||||
t.Error("10 位应不通过")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckIdCard(t *testing.T) {
|
||||
if !CheckIdCard("110101199001011234") {
|
||||
t.Error("18 位数字应通过")
|
||||
}
|
||||
if !CheckIdCard("11010119900101123X") {
|
||||
t.Error("17 位数字 + X 应通过")
|
||||
}
|
||||
if CheckIdCard("abc") {
|
||||
t.Error("非法字符应不通过")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsMatch(t *testing.T) {
|
||||
if !IsMatchGreed("hello.txt", "*.txt") {
|
||||
t.Error("IsMatchGreed(hello.txt, *.txt) 应为 true")
|
||||
}
|
||||
if IsMatchGreed("hello.txt", "*.jpg") {
|
||||
t.Error("IsMatchGreed(hello.txt, *.jpg) 应为 false")
|
||||
}
|
||||
if !IsMatchDynamic("hello.txt", "*.txt") {
|
||||
t.Error("IsMatchDynamic(hello.txt, *.txt) 应为 true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCamel2Case(t *testing.T) {
|
||||
if got := Camel2Case("GetUserName"); got != "get_user_name" {
|
||||
t.Errorf("Camel2Case = %q, want get_user_name", got)
|
||||
}
|
||||
if got := Camel2Case("id"); got != "id" {
|
||||
t.Errorf("Camel2Case = %q, want id", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteElement(t *testing.T) {
|
||||
s := []int{1, 2, 3, 4, 5}
|
||||
got := DeleteElement(s, 2)
|
||||
if len(got) != 4 || got[0] != 1 || got[1] != 2 || got[2] != 4 || got[3] != 5 {
|
||||
t.Errorf("DeleteElement = %v, want [1 2 4 5]", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuffer(t *testing.T) {
|
||||
b := NewBuffer()
|
||||
b.Append("a").Append(1).Append(int64(2)).Append([]byte("b")).Append(uint64(3))
|
||||
if b.String() != "a12b3" {
|
||||
t.Errorf("Buffer = %q, want a12b3", b.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFirstLetter(t *testing.T) {
|
||||
if got := GetFirstLetter("中国"); got != "zg" {
|
||||
t.Errorf("GetFirstLetter(中国) = %q, want zg", got)
|
||||
}
|
||||
if got := GetFirstLetter("abc123"); got != "abc123" {
|
||||
t.Errorf("非中文字符应原样返回: %q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user