Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dxyinme committed Jan 13, 2024
1 parent f5039fe commit ca5e0ca
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 36 deletions.
42 changes: 28 additions & 14 deletions randx/rand_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

var (
ErrTypeNotSupported = errors.New("ekit:不支持的类型")
ErrLengthLessThanZero = errors.New("ekit:长度必须大于0")
ErrLengthLessThanZero = errors.New("ekit:长度必须大于等于0")
// deprecated
ERRTYPENOTSUPPORTTED = ErrTypeNotSupported
)
Expand All @@ -34,45 +34,53 @@ const (
// 数字
TYPE_DIGIT TYPE = 1
// 小写字母
TYPE_LOWER TYPE = 1 << 1
TYPE_LETTER TYPE = TYPE_LOWER
TYPE_LOWERCASE TYPE = 1 << 1
TYPE_LETTER TYPE = TYPE_LOWERCASE
// 大写字母
TYPE_UPPER TYPE = 1 << 2
TYPE_CAPITAL TYPE = TYPE_UPPER
TYPE_UPPERCASE TYPE = 1 << 2
TYPE_CAPITAL TYPE = TYPE_UPPERCASE
// 特殊符号
TYPE_SPECIAL TYPE = 1 << 3
// 混合类型
TYPE_MIXED = (TYPE_DIGIT | TYPE_UPPER | TYPE_LOWER | TYPE_SPECIAL)
TYPE_MIXED = (TYPE_DIGIT | TYPE_UPPERCASE | TYPE_LOWERCASE | TYPE_SPECIAL)

// 数字字符组
CHARSET_DIGIT = "0123456789"
// 小写字母字符组
CHARSET_LOWER = "abcdefghijklmnopqrstuvwxyz"
CHARSET_LETTER = CHARSET_LOWER
CHARSET_LOWERCASE = "abcdefghijklmnopqrstuvwxyz"
CHARSET_LETTER = CHARSET_LOWERCASE
// 大写字母字符组
CHARSET_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
CHARSET_CAPITAL = CHARSET_UPPER
CHARSET_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
CHARSET_CAPITAL = CHARSET_UPPERCASE
// 特殊字符数组
CHARSET_SPECIAL = " ~!@#$%^&*()_+-=[]{};'\\:\"|,./<>?"
)

var (
// 只限于randx包内部使用
typeCharSetPair = []pair.Pair[TYPE, string]{
typeCharsetPairs = []pair.Pair[TYPE, string]{
pair.NewPair(TYPE_DIGIT, CHARSET_DIGIT),
pair.NewPair(TYPE_LOWER, CHARSET_LOWER),
pair.NewPair(TYPE_UPPER, CHARSET_UPPER),
pair.NewPair(TYPE_LOWERCASE, CHARSET_LOWERCASE),
pair.NewPair(TYPE_UPPERCASE, CHARSET_UPPERCASE),
pair.NewPair(TYPE_SPECIAL, CHARSET_SPECIAL),
}
)

// RandCode 根据传入的长度和类型生成随机字符串
// 请保证输入的 length >= 0,否则会返回 ErrLengthLessThanZero
// 请保证输入的 typ 的取值范围在 (0, type.MIXED] 内,否则会返回 ErrTypeNotSupported
func RandCode(length int, typ TYPE) (string, error) {
if length < 0 {
return "", ErrLengthLessThanZero
}
if length == 0 {
return "", nil
}
if typ > TYPE_MIXED {
return "", ErrTypeNotSupported
}
charset := ""
for _, p := range typeCharSetPair {
for _, p := range typeCharsetPairs {
if (typ & p.Key) == p.Key {
charset += p.Value
}
Expand All @@ -81,10 +89,16 @@ func RandCode(length int, typ TYPE) (string, error) {
}

// 根据传入的长度和字符集生成随机字符串
// 请保证输入的 length >= 0,否则会返回 ErrLengthLessThanZero
// 请保证输入的字符集不为空字符串,否则会返回 ErrTypeNotSupported
// 字符集内部字符可以无序或重复
func RandStrByCharset(length int, charset string) (string, error) {
if length < 0 {
return "", ErrLengthLessThanZero
}
if length == 0 {
return "", nil
}
charsetSize := len(charset)
if charsetSize == 0 {
return "", ErrTypeNotSupported
Expand Down
81 changes: 59 additions & 22 deletions randx/rand_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func TestRandCode(t *testing.T) {
{
name: "数字+小写字母验证码",
length: 100,
typ: randx.TYPE_DIGIT | randx.TYPE_LOWER,
typ: randx.TYPE_DIGIT | randx.TYPE_LOWERCASE,
wantMatch: "^[a-z0-9]+$",
wantErr: nil,
},
{
name: "数字+大写字母验证码",
length: 100,
typ: randx.TYPE_DIGIT | randx.TYPE_UPPER,
typ: randx.TYPE_DIGIT | randx.TYPE_UPPERCASE,
wantMatch: "^[A-Z0-9]+$",
wantErr: nil,
},
Expand All @@ -66,25 +66,53 @@ func TestRandCode(t *testing.T) {
wantMatch: "^[A-Z]+$",
wantErr: nil,
},
{
name: "大写字母验证码(兼容旧版本)",
length: 100,
typ: randx.TYPE_CAPITAL,
wantMatch: "^[A-Z]+$",
wantErr: nil,
},
{
name: "大小写字母验证码",
length: 100,
typ: randx.TYPE_UPPER | randx.TYPE_LOWER,
typ: randx.TYPE_UPPERCASE | randx.TYPE_LOWERCASE,
wantMatch: "^[a-zA-Z]+$",
wantErr: nil,
},
{
name: "大小写字母验证码(兼容旧版本)",
length: 100,
typ: randx.TYPE_CAPITAL | randx.TYPE_LETTER,
wantMatch: "^[a-zA-Z]+$",
wantErr: nil,
},
{
name: "数字+大小写字母验证码",
length: 100,
typ: randx.TYPE_DIGIT | randx.TYPE_UPPER | randx.TYPE_LOWER,
typ: randx.TYPE_DIGIT | randx.TYPE_UPPERCASE | randx.TYPE_LOWERCASE,
wantMatch: "^[0-9a-zA-Z]+$",
wantErr: nil,
},
{
name: "数字+大小写字母验证码(兼容旧版本)",
length: 100,
typ: randx.TYPE_DIGIT | randx.TYPE_LETTER | randx.TYPE_CAPITAL,
wantMatch: "^[0-9a-zA-Z]+$",
wantErr: nil,
},
{
name: "所有类型验证",
length: 100,
typ: randx.TYPE_MIXED,
wantMatch: "^[\\S\\s]*$",
wantMatch: "^[\\S\\s]+$",
wantErr: nil,
},
{
name: "特殊字符类型验证",
length: 100,
typ: randx.TYPE_SPECIAL,
wantMatch: "^[^0-9a-zA-Z]+$",
wantErr: nil,
},
{
Expand All @@ -108,25 +136,29 @@ func TestRandCode(t *testing.T) {
wantMatch: "",
wantErr: randx.ErrLengthLessThanZero,
},
{
name: "长度等于0",
length: 0,
typ: randx.TYPE_MIXED,
wantMatch: "",
wantErr: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
code, err := randx.RandCode(tc.length, tc.typ)
if err != nil {
if tc.wantErr != nil {
assert.Equal(t, tc.wantErr, err)
} else {
assert.Lenf(
t,
code,
tc.length,
"expected length: %d but got length:%d",
tc.length, len(code))

return
}
assert.Len(t, code, tc.length)
if tc.length > 0 {
matched, err := regexp.MatchString(tc.wantMatch, code)
assert.Nil(t, err)
assert.Truef(t, matched, "expected %s but got %s", tc.wantMatch, code)
}

})
}
}
Expand All @@ -152,6 +184,12 @@ func TestRandStrByCharset(t *testing.T) {
charset: "123",
wantErr: randx.ErrLengthLessThanZero,
},
{
name: "长度等于0",
length: 0,
charset: "123",
wantErr: nil,
},
{
name: "随机字符串测试",
length: 100,
Expand All @@ -169,17 +207,16 @@ func TestRandStrByCharset(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
code, err := randx.RandStrByCharset(tc.length, tc.charset)
if err != nil {
if tc.wantErr != nil {
assert.Equal(t, tc.wantErr, err)
} else {
assert.Lenf(
t,
code,
tc.length,
"expected length: %d but got length:%d",
tc.length, len(code))
return
}

assert.Len(t, code, tc.length)
if tc.length > 0 {
assert.True(t, matchFunc(code, tc.charset))
}

})
}
}
Expand Down

0 comments on commit ca5e0ca

Please sign in to comment.