all_test.go 914 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package validate
  2. import "testing"
  3. func TestAll(t *testing.T) {
  4. type TestCase[T any] struct {
  5. Input T
  6. F func(T) error
  7. Err error
  8. }
  9. f := All(
  10. MinLength(4),
  11. MaxLength(8),
  12. Chars("0123456789abcdef"),
  13. In("abcd", "abcdef", "12345678"),
  14. )
  15. testCases := []TestCase[string]{
  16. {Input: "abcd", F: f},
  17. {Input: "abcdef", F: f},
  18. {Input: "12345678", F: f},
  19. {Input: "abc", F: f, Err: ErrTooFewChars},
  20. {Input: "abcdef012", F: f, Err: ErrTooManyChars},
  21. {Input: "abcdefgh", F: f, Err: ErrDisallowedChars},
  22. {Input: "01abcd", F: f, Err: ErrValueNotAllowed},
  23. }
  24. for _, tc := range testCases {
  25. t.Logf("%q", tc.Input)
  26. err := tc.F(tc.Input)
  27. if tc.Err != nil {
  28. if err == nil {
  29. t.Errorf("Expected %s; got nil", tc.Err)
  30. }
  31. if err != tc.Err {
  32. t.Errorf("Expected %s; got %s", tc.Err, err)
  33. }
  34. } else {
  35. if err != nil {
  36. t.Errorf("Expected nil; got %s", err)
  37. }
  38. }
  39. }
  40. }