a9c1184d6b
also simplified structure of test cases and failure logging
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package validate
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func ExampleChars() {
|
|
testChars := Chars("0123456789abcdef")
|
|
fmt.Println(testChars("invalid input"))
|
|
// Output: contains disallowed characters
|
|
}
|
|
|
|
func ExampleExceptChars() {
|
|
testExceptChars := Chars("0123456789abcdef")
|
|
fmt.Println(testExceptChars("invalid input"))
|
|
// Output: contains disallowed characters
|
|
}
|
|
|
|
func TestChars(t *testing.T) {
|
|
testCases := map[string]map[string]error{
|
|
"0123456789abcdef": {"abcd1234": nil, "abcd 1234": ErrDisallowedChars, "ghijklmno": ErrDisallowedChars},
|
|
}
|
|
|
|
for setup, values := range testCases {
|
|
testChars := Chars(setup)
|
|
|
|
for input, want := range values {
|
|
t.Run(input, func(t *testing.T) {
|
|
got := testChars(input)
|
|
|
|
if !errors.Is(got, want) {
|
|
t.Error("got", got)
|
|
t.Error("want", want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExceptChars(t *testing.T) {
|
|
testCases := map[string]map[string]error{
|
|
"0123456789abcdef": {"abcd1234": ErrDisallowedChars, "abcd 1234": ErrDisallowedChars, "ghijklmno": nil},
|
|
}
|
|
|
|
for setup, values := range testCases {
|
|
testExceptChars := ExceptChars(setup)
|
|
|
|
for input, want := range values {
|
|
t.Run(input, func(t *testing.T) {
|
|
got := testExceptChars(input)
|
|
|
|
if !errors.Is(got, want) {
|
|
t.Error("got", got)
|
|
t.Error("want", want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|