36 lines
628 B
Go
36 lines
628 B
Go
package validate
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"regexp"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func ExampleMatch() {
|
||
|
|
slug := Match(regexp.MustCompile(`^[a-z0-9-]+$`))
|
||
|
|
fmt.Println(slug("Not A Slug"))
|
||
|
|
// Output: invalid format
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestMatch(t *testing.T) {
|
||
|
|
v := Match(regexp.MustCompile(`^[a-z0-9-]+$`))
|
||
|
|
|
||
|
|
testCases := map[string]error{
|
||
|
|
"hello-world-123": nil,
|
||
|
|
"abc": nil,
|
||
|
|
|
||
|
|
"Hello": ErrNoMatch,
|
||
|
|
"has space": ErrNoMatch,
|
||
|
|
"": ErrNoMatch,
|
||
|
|
}
|
||
|
|
|
||
|
|
for input, want := range testCases {
|
||
|
|
t.Run(input, func(t *testing.T) {
|
||
|
|
if got := v(input); !errors.Is(got, want) {
|
||
|
|
t.Error("got", got, "want", want)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|