Add handle pkg
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package handle
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var (
|
||||
c = regexp.MustCompile(`^[A-Za-z][0-9A-Za-z_\-.]*[A-Za-z0-9]$`)
|
||||
)
|
||||
|
||||
func IsValid(s string) bool {
|
||||
return s == "" || (len(s) >= 2 && c.MatchString(s))
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package handle
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsValid(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
handle string
|
||||
want bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{"empty", "", true},
|
||||
{"alphanum", "a1", true},
|
||||
{"alpha", "abc", true},
|
||||
{"num", "123", false},
|
||||
{"valid1", "Valid1Handle", true},
|
||||
{"valid2", "Valid-Handle", true},
|
||||
{"valid3", "Valid_Handle", true},
|
||||
{"weirdo", "a$&_.!", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsValid(tt.handle); got != tt.want {
|
||||
t.Errorf("IsValid() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user