length_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package validate
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. func TestMaxLength(t *testing.T) {
  7. type TestCase struct {
  8. Input string
  9. L int
  10. Err error
  11. }
  12. testCases := []TestCase{
  13. {Input: "abcd", L: 8},
  14. {Input: "abcdefgh", L: 8},
  15. {Input: "abcd efg", L: 8},
  16. {Input: "abcdefghi", L: 8, Err: ErrTooManyChars},
  17. }
  18. for n, tc := range testCases {
  19. t.Logf("(%d) Testing %q against maximum length of %d", n, tc.Input, tc.L)
  20. f := MaxLength(tc.L)
  21. err := f(tc.Input)
  22. if !errors.Is(err, tc.Err) {
  23. t.Errorf("Expected error %v, got %v", tc.Err, err)
  24. }
  25. }
  26. }
  27. func TestMinLength(t *testing.T) {
  28. type TestCase struct {
  29. Input string
  30. L int
  31. Err error
  32. }
  33. testCases := []TestCase{
  34. {Input: "abcd", L: 8, Err: ErrTooFewChars},
  35. {Input: "abcdefgh", L: 8},
  36. {Input: "abcd efg", L: 8},
  37. {Input: "abcdefghi", L: 8},
  38. }
  39. for n, tc := range testCases {
  40. t.Logf("(%d) Testing %q against minimum length of %d", n, tc.Input, tc.L)
  41. f := MinLength(tc.L)
  42. err := f(tc.Input)
  43. if !errors.Is(err, tc.Err) {
  44. t.Errorf("Expected error %v, got %v", tc.Err, err)
  45. }
  46. }
  47. }