length_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package validate
  2. import "testing"
  3. func TestMaxLength(t *testing.T) {
  4. type TestCase struct {
  5. L int
  6. Input string
  7. Err bool
  8. }
  9. testCases := []TestCase{
  10. {L: 8, Input: "abcd"},
  11. {L: 8, Input: "abcdefgh"},
  12. {L: 8, Input: "abcd efg"},
  13. {L: 8, Input: "abcdefghi", Err: true},
  14. }
  15. for _, tc := range testCases {
  16. t.Logf("Max length %d for %q", tc.L, tc.Input)
  17. f := MaxLength(tc.L)
  18. err := f(tc.Input)
  19. if tc.Err {
  20. if err == nil {
  21. t.Error("Expected an error; got nil")
  22. }
  23. } else {
  24. if err != nil {
  25. t.Errorf("Expected nil; got %s", err)
  26. }
  27. }
  28. }
  29. }
  30. func TestMinLength(t *testing.T) {
  31. type TestCase struct {
  32. L int
  33. Input string
  34. Err bool
  35. }
  36. testCases := []TestCase{
  37. {L: 8, Input: "abcd", Err: true},
  38. {L: 8, Input: "abcdefgh"},
  39. {L: 8, Input: "abcd efg"},
  40. {L: 8, Input: "abcdefghi"},
  41. }
  42. for _, tc := range testCases {
  43. t.Logf("Min length %d for %q", tc.L, tc.Input)
  44. f := MinLength(tc.L)
  45. err := f(tc.Input)
  46. if tc.Err {
  47. if err == nil {
  48. t.Error("Expected an error; got nil")
  49. }
  50. } else {
  51. if err != nil {
  52. t.Errorf("Expected nil; got %s", err)
  53. }
  54. }
  55. }
  56. }