all_test.go 807 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package validate
  2. import (
  3. "errors"
  4. "fmt"
  5. "testing"
  6. )
  7. func ExampleAll() {
  8. testAll := All(MinLength(4), Chars("0123456789abcdef"))
  9. fmt.Println(testAll("invalid input"))
  10. // Output: contains disallowed characters
  11. }
  12. func TestAll(t *testing.T) {
  13. testAll := All(
  14. MinLength(4),
  15. MaxLength(8),
  16. Chars("0123456789abcdef"),
  17. In("abcd", "abcdef", "12345678"),
  18. )
  19. testCases := map[string]error{
  20. "abcd": nil,
  21. "abcdef": nil,
  22. "12345678": nil,
  23. "abc": ErrMustBeLonger.With(4),
  24. "abcdef012": ErrMustBeShorter.With(8),
  25. "abcdefgh": ErrDisallowedChars,
  26. "01abcd": ErrValueNotAllowed,
  27. }
  28. for input, want := range testCases {
  29. t.Run(input, func(t *testing.T) {
  30. got := testAll(input)
  31. if !errors.Is(got, want) {
  32. t.Error("got", got)
  33. t.Error("want", want)
  34. }
  35. })
  36. }
  37. }