70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package validate
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func ExamplePrefix() {
|
||
|
|
fmt.Println(Prefix("https://")("http://example.com"))
|
||
|
|
// Output: must start with "https://"
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPrefix(t *testing.T) {
|
||
|
|
testCases := map[string]error{
|
||
|
|
"https://example.com": nil,
|
||
|
|
"https://": nil,
|
||
|
|
|
||
|
|
"http://example.com": ErrMissingPrefix.With("https://"),
|
||
|
|
"": ErrMissingPrefix.With("https://"),
|
||
|
|
}
|
||
|
|
|
||
|
|
v := Prefix("https://")
|
||
|
|
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)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSuffix(t *testing.T) {
|
||
|
|
testCases := map[string]error{
|
||
|
|
"report.pdf": nil,
|
||
|
|
".pdf": nil,
|
||
|
|
|
||
|
|
"report.txt": ErrMissingSuffix.With(".pdf"),
|
||
|
|
"pdf": ErrMissingSuffix.With(".pdf"),
|
||
|
|
}
|
||
|
|
|
||
|
|
v := Suffix(".pdf")
|
||
|
|
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)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestContains(t *testing.T) {
|
||
|
|
testCases := map[string]error{
|
||
|
|
"a b c": nil,
|
||
|
|
" ": nil,
|
||
|
|
|
||
|
|
"abc": ErrMissingSubstring.With(" "),
|
||
|
|
"": ErrMissingSubstring.With(" "),
|
||
|
|
}
|
||
|
|
|
||
|
|
v := Contains(" ")
|
||
|
|
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)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|