2023-10-16 17:43:05 +01:00
|
|
|
package validate
|
|
|
|
|
|
2023-11-19 20:27:33 +00:00
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
2023-10-16 17:43:05 +01:00
|
|
|
|
|
|
|
|
func TestMaxSize(t *testing.T) {
|
|
|
|
|
type TestCase struct {
|
|
|
|
|
Input []int
|
|
|
|
|
L int
|
|
|
|
|
Err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testCases := []TestCase{
|
|
|
|
|
{Input: []int{1, 2, 3, 4}, L: 8},
|
|
|
|
|
{Input: []int{1, 2, 3, 4, 5, 6, 7, 8}, L: 8},
|
2023-11-19 20:42:48 +00:00
|
|
|
{Input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, L: 8, Err: ErrMustHaveFewerItems},
|
2023-10-16 17:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for n, tc := range testCases {
|
|
|
|
|
t.Logf("(%d) Testing %q against maximum length of %d", n, tc.Input, tc.L)
|
|
|
|
|
|
|
|
|
|
f := MaxSize[int](tc.L)
|
|
|
|
|
err := f(tc.Input)
|
|
|
|
|
|
2023-11-19 20:27:33 +00:00
|
|
|
if !errors.Is(err, tc.Err) {
|
2023-10-16 17:43:05 +01:00
|
|
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMinSize(t *testing.T) {
|
|
|
|
|
type TestCase struct {
|
|
|
|
|
Input []int
|
|
|
|
|
L int
|
|
|
|
|
Err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testCases := []TestCase{
|
2023-11-19 20:42:48 +00:00
|
|
|
{Input: []int{1, 2, 3, 4}, L: 8, Err: ErrMustHaveMoreItems},
|
2023-10-16 17:43:05 +01:00
|
|
|
{Input: []int{1, 2, 3, 4, 5, 6, 7, 8}, L: 8},
|
|
|
|
|
{Input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, L: 8},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for n, tc := range testCases {
|
|
|
|
|
t.Logf("(%d) Testing %q against minimum length of %d", n, tc.Input, tc.L)
|
|
|
|
|
|
|
|
|
|
f := MinSize[int](tc.L)
|
|
|
|
|
err := f(tc.Input)
|
|
|
|
|
|
2023-11-19 20:27:33 +00:00
|
|
|
if !errors.Is(err, tc.Err) {
|
2023-10-16 17:43:05 +01:00
|
|
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|