improve validate testing with examples

also simplified structure of test cases and failure logging
This commit is contained in:
Aneurin Barker Snook
2024-07-18 06:53:20 +01:00
parent 4c99b5a654
commit a9c1184d6b
14 changed files with 417 additions and 428 deletions
+53 -30
View File
@@ -2,55 +2,78 @@ package validate
import (
"errors"
"fmt"
"testing"
)
func ExampleMaxSize() {
testMaxSize := MaxSize[string](3)
fmt.Println(testMaxSize([]string{"abc", "def", "ghi", "jkl"}))
// Output: must have no more than 3 items
}
func ExampleMinSize() {
testMaxSize := MinSize[string](3)
fmt.Println(testMaxSize([]string{"abc", "def"}))
// Output: must have at least 3 items
}
func TestMaxSize(t *testing.T) {
type TestCase struct {
Input []int
L int
Err error
testCases := map[int]map[int]error{
0: {0: nil, 1: ErrMustHaveFewerItems.With(0), 2: ErrMustHaveFewerItems.With(0), 10: ErrMustHaveFewerItems.With(0)},
1: {0: nil, 1: nil, 2: ErrMustHaveFewerItems.With(1), 10: ErrMustHaveFewerItems.With(1)},
2: {0: nil, 1: nil, 2: nil, 10: ErrMustHaveFewerItems.With(2)},
10: {0: nil, 1: nil, 2: nil, 10: nil},
100: {0: nil, 1: nil, 2: nil, 10: nil},
}
testCases := []TestCase{
{Input: []int{1, 2, 3, 4}, L: 8},
{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, Err: ErrMustHaveFewerItems},
}
for max, values := range testCases {
testMaxSize := MaxSize[int](max)
for n, tc := range testCases {
t.Logf("(%d) Testing %q against maximum length of %d", n, tc.Input, tc.L)
for l, want := range values {
t.Run(fmt.Sprintf("%d/%d", max, l), func(t *testing.T) {
input := []int{}
for i := 0; i < l; i++ {
input = append(input, i)
}
f := MaxSize[int](tc.L)
err := f(tc.Input)
got := testMaxSize(input)
if !errors.Is(err, tc.Err) {
t.Errorf("Expected error %v, got %v", tc.Err, err)
if !errors.Is(got, want) {
t.Error("got", got)
t.Error("want", want)
}
})
}
}
}
func TestMinSize(t *testing.T) {
type TestCase struct {
Input []int
L int
Err error
testCases := map[int]map[int]error{
0: {0: nil, 1: nil, 2: nil, 10: nil},
1: {0: ErrMustHaveMoreItems.With(1), 1: nil, 2: nil, 10: nil},
2: {0: ErrMustHaveMoreItems.With(2), 1: ErrMustHaveMoreItems.With(2), 2: nil, 10: nil},
10: {0: ErrMustHaveMoreItems.With(10), 1: ErrMustHaveMoreItems.With(10), 2: ErrMustHaveMoreItems.With(10), 10: nil},
100: {0: ErrMustHaveMoreItems.With(100), 1: ErrMustHaveMoreItems.With(100), 2: ErrMustHaveMoreItems.With(100), 10: ErrMustHaveMoreItems.With(100)},
}
testCases := []TestCase{
{Input: []int{1, 2, 3, 4}, L: 8, Err: ErrMustHaveMoreItems},
{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 min, values := range testCases {
testMinSize := MinSize[int](min)
for n, tc := range testCases {
t.Logf("(%d) Testing %q against minimum length of %d", n, tc.Input, tc.L)
for l, want := range values {
t.Run(fmt.Sprintf("%d/%d", min, l), func(t *testing.T) {
input := []int{}
for i := 0; i < l; i++ {
input = append(input, i)
}
f := MinSize[int](tc.L)
err := f(tc.Input)
got := testMinSize(input)
if !errors.Is(err, tc.Err) {
t.Errorf("Expected error %v, got %v", tc.Err, err)
if !errors.Is(got, want) {
t.Error("got", got)
t.Error("want", want)
}
})
}
}
}