Add handle pkg

This commit is contained in:
Denis Arh
2019-09-30 10:20:39 +02:00
parent 838fa8302a
commit 9d5dea7551
2 changed files with 43 additions and 0 deletions
+13
View File
@@ -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))
}
+30
View File
@@ -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)
}
})
}
}