length_test.go 1.0 KB

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