improve validate testing with examples
also simplified structure of test cases and failure logging
This commit is contained in:
+115
-108
@@ -2,167 +2,174 @@ package validate
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func ExampleMax() {
|
||||
testMax := Max(10, true)
|
||||
fmt.Println(testMax(10))
|
||||
// Output: must be less than 10
|
||||
}
|
||||
|
||||
func ExampleMin() {
|
||||
testMin := Min(10, false)
|
||||
fmt.Println(testMin(5))
|
||||
// Output: must be greater than or equal to 10
|
||||
}
|
||||
|
||||
func TestMax(t *testing.T) {
|
||||
type TestCase struct {
|
||||
Input int
|
||||
N int
|
||||
Excl bool
|
||||
Err error
|
||||
testCases := map[int]map[bool]map[int]error{
|
||||
10: {
|
||||
true: {0: nil, 1: nil, 2: nil, 10: ErrMustBeLess.With(10), 100: ErrMustBeLess.With(10)},
|
||||
false: {0: nil, 1: nil, 2: nil, 10: nil, 100: ErrMustBeLessOrEqual.With(10)},
|
||||
},
|
||||
}
|
||||
|
||||
testCases := []TestCase{
|
||||
{Input: 10, N: 0, Err: ErrMustBeLessOrEqual},
|
||||
{Input: 10, N: 10},
|
||||
{Input: 10, N: 15},
|
||||
{Input: 10, N: 10, Excl: true, Err: ErrMustBeLess},
|
||||
}
|
||||
for setup, subSetup := range testCases {
|
||||
for excl, values := range subSetup {
|
||||
testMax := Max(setup, excl)
|
||||
|
||||
for n, tc := range testCases {
|
||||
t.Logf("(%d) Testing %d against maximum of %d", n, tc.Input, tc.N)
|
||||
for input, want := range values {
|
||||
t.Run(fmt.Sprintf("%d/%v/%d", setup, excl, input), func(t *testing.T) {
|
||||
got := testMax(input)
|
||||
|
||||
f := Max(tc.N, tc.Excl)
|
||||
err := f(tc.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 TestMaxFloat32(t *testing.T) {
|
||||
type TestCase struct {
|
||||
Input float32
|
||||
N float32
|
||||
Excl bool
|
||||
Err error
|
||||
testCases := map[float32]map[bool]map[float32]error{
|
||||
10: {
|
||||
true: {0: nil, 1: nil, 2: nil, 10: ErrMustBeLess.With(10), 100: ErrMustBeLess.With(10)},
|
||||
false: {0: nil, 1: nil, 2: nil, 10: nil, 100: ErrMustBeLessOrEqual.With(10)},
|
||||
},
|
||||
}
|
||||
|
||||
testCases := []TestCase{
|
||||
{Input: 10, N: 0, Err: ErrMustBeLessOrEqual},
|
||||
{Input: 10, N: 10},
|
||||
{Input: 10, N: 15},
|
||||
{Input: 10, N: 10, Excl: true, Err: ErrMustBeLess},
|
||||
}
|
||||
for setup, subSetup := range testCases {
|
||||
for excl, values := range subSetup {
|
||||
testMax := MaxFloat32(setup, excl)
|
||||
|
||||
for n, tc := range testCases {
|
||||
t.Logf("(%d) Testing %g against maximum of %g", n, tc.Input, tc.N)
|
||||
for input, want := range values {
|
||||
t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
|
||||
got := testMax(input)
|
||||
|
||||
f := MaxFloat32(tc.N, tc.Excl)
|
||||
err := f(tc.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 TestMaxFloat64(t *testing.T) {
|
||||
type TestCase struct {
|
||||
Input float64
|
||||
N float64
|
||||
Excl bool
|
||||
Err error
|
||||
testCases := map[float64]map[bool]map[float64]error{
|
||||
10: {
|
||||
true: {0: nil, 1: nil, 2: nil, 10: ErrMustBeLess.With(10), 100: ErrMustBeLess.With(10)},
|
||||
false: {0: nil, 1: nil, 2: nil, 10: nil, 100: ErrMustBeLessOrEqual.With(10)},
|
||||
},
|
||||
}
|
||||
|
||||
testCases := []TestCase{
|
||||
{Input: 10, N: 0, Err: ErrMustBeLessOrEqual},
|
||||
{Input: 10, N: 10},
|
||||
{Input: 10, N: 15},
|
||||
{Input: 10, N: 10, Excl: true, Err: ErrMustBeLess},
|
||||
}
|
||||
for setup, subSetup := range testCases {
|
||||
for excl, values := range subSetup {
|
||||
testMax := MaxFloat64(setup, excl)
|
||||
|
||||
for n, tc := range testCases {
|
||||
t.Logf("(%d) Testing %g against maximum of %g", n, tc.Input, tc.N)
|
||||
for input, want := range values {
|
||||
t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
|
||||
got := testMax(input)
|
||||
|
||||
f := MaxFloat64(tc.N, tc.Excl)
|
||||
err := f(tc.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 TestMin(t *testing.T) {
|
||||
type TestCase struct {
|
||||
Input int
|
||||
N int
|
||||
Excl bool
|
||||
Err error
|
||||
testCases := map[int]map[bool]map[int]error{
|
||||
10: {
|
||||
true: {0: ErrMustBeGreater.With(10), 1: ErrMustBeGreater.With(10), 2: ErrMustBeGreater.With(10), 10: ErrMustBeGreater.With(10), 100: nil},
|
||||
false: {0: ErrMustBeGreaterOrEqual.With(10), 1: ErrMustBeGreaterOrEqual.With(10), 2: ErrMustBeGreaterOrEqual.With(10), 10: nil, 100: nil},
|
||||
},
|
||||
}
|
||||
|
||||
testCases := []TestCase{
|
||||
{Input: 10, N: 0},
|
||||
{Input: 10, N: 10},
|
||||
{Input: 10, N: 15, Err: ErrMustBeGreaterOrEqual},
|
||||
{Input: 10, N: 10, Excl: true, Err: ErrMustBeGreater},
|
||||
}
|
||||
for setup, subSetup := range testCases {
|
||||
for excl, values := range subSetup {
|
||||
testMin := Min(setup, excl)
|
||||
|
||||
for n, tc := range testCases {
|
||||
t.Logf("(%d) Testing %d against minimum of %d", n, tc.Input, tc.N)
|
||||
for input, want := range values {
|
||||
t.Run(fmt.Sprintf("%d/%v/%d", setup, excl, input), func(t *testing.T) {
|
||||
got := testMin(input)
|
||||
|
||||
f := Min(tc.N, tc.Excl)
|
||||
err := f(tc.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 TestMinFloat32(t *testing.T) {
|
||||
type TestCase struct {
|
||||
Input float32
|
||||
N float32
|
||||
Excl bool
|
||||
Err error
|
||||
testCases := map[float32]map[bool]map[float32]error{
|
||||
10: {
|
||||
true: {0: ErrMustBeGreater.With(10), 1: ErrMustBeGreater.With(10), 2: ErrMustBeGreater.With(10), 10: ErrMustBeGreater.With(10), 100: nil},
|
||||
false: {0: ErrMustBeGreaterOrEqual.With(10), 1: ErrMustBeGreaterOrEqual.With(10), 2: ErrMustBeGreaterOrEqual.With(10), 10: nil, 100: nil},
|
||||
},
|
||||
}
|
||||
|
||||
testCases := []TestCase{
|
||||
{Input: 10, N: 0},
|
||||
{Input: 10, N: 10},
|
||||
{Input: 10, N: 15, Err: ErrMustBeGreaterOrEqual},
|
||||
{Input: 10, N: 10, Excl: true, Err: ErrMustBeGreater},
|
||||
}
|
||||
for setup, subSetup := range testCases {
|
||||
for excl, values := range subSetup {
|
||||
testMin := MinFloat32(setup, excl)
|
||||
|
||||
for n, tc := range testCases {
|
||||
t.Logf("(%d) Testing %g against minimum of %g", n, tc.Input, tc.N)
|
||||
for input, want := range values {
|
||||
t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
|
||||
got := testMin(input)
|
||||
|
||||
f := MinFloat32(tc.N, tc.Excl)
|
||||
err := f(tc.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 TestMinFloat64(t *testing.T) {
|
||||
type TestCase struct {
|
||||
Input float64
|
||||
N float64
|
||||
Excl bool
|
||||
Err error
|
||||
testCases := map[float64]map[bool]map[float64]error{
|
||||
10: {
|
||||
true: {0: ErrMustBeGreater.With(10), 1: ErrMustBeGreater.With(10), 2: ErrMustBeGreater.With(10), 10: ErrMustBeGreater.With(10), 100: nil},
|
||||
false: {0: ErrMustBeGreaterOrEqual.With(10), 1: ErrMustBeGreaterOrEqual.With(10), 2: ErrMustBeGreaterOrEqual.With(10), 10: nil, 100: nil},
|
||||
},
|
||||
}
|
||||
|
||||
testCases := []TestCase{
|
||||
{Input: 10, N: 0},
|
||||
{Input: 10, N: 10},
|
||||
{Input: 10, N: 15, Err: ErrMustBeGreaterOrEqual},
|
||||
{Input: 10, N: 10, Excl: true, Err: ErrMustBeGreater},
|
||||
}
|
||||
for setup, subSetup := range testCases {
|
||||
for excl, values := range subSetup {
|
||||
testMin := MinFloat64(setup, excl)
|
||||
|
||||
for n, tc := range testCases {
|
||||
t.Logf("(%d) Testing %g against minimum of %g", n, tc.Input, tc.N)
|
||||
for input, want := range values {
|
||||
t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
|
||||
got := testMin(input)
|
||||
|
||||
f := MinFloat64(tc.N, tc.Excl)
|
||||
err := f(tc.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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user