Files
validate/chars_test.go
T

62 lines
1.3 KiB
Go
Raw Normal View History

2023-10-07 10:52:11 +01:00
package validate
import (
"errors"
2024-07-18 06:53:20 +01:00
"fmt"
"testing"
)
2023-10-07 10:52:11 +01:00
2024-07-18 06:53:20 +01:00
func ExampleChars() {
testChars := Chars("0123456789abcdef")
fmt.Println(testChars("invalid input"))
// Output: contains disallowed characters
}
2023-10-07 10:52:11 +01:00
2024-07-18 06:53:20 +01:00
func ExampleExceptChars() {
testExceptChars := Chars("0123456789abcdef")
fmt.Println(testExceptChars("invalid input"))
// Output: contains disallowed characters
}
2023-10-07 10:52:11 +01:00
2024-07-18 06:53:20 +01:00
func TestChars(t *testing.T) {
testCases := map[string]map[string]error{
"0123456789abcdef": {"abcd1234": nil, "abcd 1234": ErrDisallowedChars, "ghijklmno": ErrDisallowedChars},
2023-10-07 10:52:11 +01:00
}
2024-07-18 06:53:20 +01:00
for setup, values := range testCases {
testChars := Chars(setup)
2023-10-07 10:52:11 +01:00
2024-07-18 06:53:20 +01:00
for input, want := range values {
t.Run(input, func(t *testing.T) {
got := testChars(input)
2023-10-07 10:52:11 +01:00
2024-07-18 06:53:20 +01:00
if !errors.Is(got, want) {
t.Error("got", got)
t.Error("want", want)
}
})
2023-10-07 10:52:11 +01:00
}
}
}
2023-11-20 17:59:05 +00:00
func TestExceptChars(t *testing.T) {
2024-07-18 06:53:20 +01:00
testCases := map[string]map[string]error{
"0123456789abcdef": {"abcd1234": ErrDisallowedChars, "abcd 1234": ErrDisallowedChars, "ghijklmno": nil},
2023-11-20 17:59:05 +00:00
}
2024-07-18 06:53:20 +01:00
for setup, values := range testCases {
testExceptChars := ExceptChars(setup)
2023-11-20 17:59:05 +00:00
2024-07-18 06:53:20 +01:00
for input, want := range values {
t.Run(input, func(t *testing.T) {
got := testExceptChars(input)
2023-11-20 17:59:05 +00:00
2024-07-18 06:53:20 +01:00
if !errors.Is(got, want) {
t.Error("got", got)
t.Error("want", want)
}
})
2023-11-20 17:59:05 +00:00
}
}
}