Files
validate/all_test.go
T

45 lines
807 B
Go
Raw Normal View History

2023-10-07 13:42:17 +01:00
package validate
import (
"errors"
2024-07-18 06:53:20 +01:00
"fmt"
"testing"
)
2023-10-07 13:42:17 +01:00
2024-07-18 06:53:20 +01:00
func ExampleAll() {
testAll := All(MinLength(4), Chars("0123456789abcdef"))
fmt.Println(testAll("invalid input"))
// Output: contains disallowed characters
}
2023-10-07 13:42:17 +01:00
2024-07-18 06:53:20 +01:00
func TestAll(t *testing.T) {
testAll := All(
2023-10-07 13:42:17 +01:00
MinLength(4),
MaxLength(8),
Chars("0123456789abcdef"),
In("abcd", "abcdef", "12345678"),
)
2024-07-18 06:53:20 +01:00
testCases := map[string]error{
"abcd": nil,
"abcdef": nil,
"12345678": nil,
2023-10-07 13:42:17 +01:00
2024-07-18 06:53:20 +01:00
"abc": ErrMustBeLonger.With(4),
"abcdef012": ErrMustBeShorter.With(8),
"abcdefgh": ErrDisallowedChars,
"01abcd": ErrValueNotAllowed,
}
2023-10-07 13:42:17 +01:00
2024-07-18 06:53:20 +01:00
for input, want := range testCases {
t.Run(input, func(t *testing.T) {
got := testAll(input)
2023-10-08 15:21:02 +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 13:42:17 +01:00
}
}