size_test.go 1.1 KB

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