|
@@ -2,167 +2,174 @@ package validate
|
|
|
|
|
|
import (
|
|
import (
|
|
"errors"
|
|
"errors"
|
|
|
|
+ "fmt"
|
|
"testing"
|
|
"testing"
|
|
)
|
|
)
|
|
|
|
|
|
-func TestMax(t *testing.T) {
|
|
|
|
- type TestCase struct {
|
|
|
|
- Input int
|
|
|
|
- N int
|
|
|
|
- Excl bool
|
|
|
|
- Err error
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- 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 n, tc := range testCases {
|
|
|
|
- t.Logf("(%d) Testing %d against maximum of %d", n, tc.Input, tc.N)
|
|
|
|
|
|
+func ExampleMax() {
|
|
|
|
+ testMax := Max(10, true)
|
|
|
|
+ fmt.Println(testMax(10))
|
|
|
|
+ // Output: must be less than 10
|
|
|
|
+}
|
|
|
|
|
|
- f := Max(tc.N, tc.Excl)
|
|
|
|
- err := f(tc.Input)
|
|
|
|
|
|
+func ExampleMin() {
|
|
|
|
+ testMin := Min(10, false)
|
|
|
|
+ fmt.Println(testMin(5))
|
|
|
|
+ // Output: must be greater than or equal to 10
|
|
|
|
+}
|
|
|
|
|
|
- if !errors.Is(err, tc.Err) {
|
|
|
|
- t.Errorf("Expected error %v, got %v", tc.Err, err)
|
|
|
|
|
|
+func TestMax(t *testing.T) {
|
|
|
|
+ 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)},
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for setup, subSetup := range testCases {
|
|
|
|
+ for excl, values := range subSetup {
|
|
|
|
+ testMax := Max(setup, excl)
|
|
|
|
+
|
|
|
|
+ for input, want := range values {
|
|
|
|
+ t.Run(fmt.Sprintf("%d/%v/%d", setup, excl, input), func(t *testing.T) {
|
|
|
|
+ got := testMax(input)
|
|
|
|
+
|
|
|
|
+ if !errors.Is(got, want) {
|
|
|
|
+ t.Error("got", got)
|
|
|
|
+ t.Error("want", want)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestMaxFloat32(t *testing.T) {
|
|
func TestMaxFloat32(t *testing.T) {
|
|
- type TestCase struct {
|
|
|
|
- Input float32
|
|
|
|
- N float32
|
|
|
|
- Excl bool
|
|
|
|
- Err error
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- 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 n, tc := range testCases {
|
|
|
|
- t.Logf("(%d) Testing %g against maximum of %g", n, tc.Input, tc.N)
|
|
|
|
-
|
|
|
|
- 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)
|
|
|
|
|
|
+ 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)},
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for setup, subSetup := range testCases {
|
|
|
|
+ for excl, values := range subSetup {
|
|
|
|
+ testMax := MaxFloat32(setup, excl)
|
|
|
|
+
|
|
|
|
+ for input, want := range values {
|
|
|
|
+ t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
|
|
|
|
+ got := testMax(input)
|
|
|
|
+
|
|
|
|
+ if !errors.Is(got, want) {
|
|
|
|
+ t.Error("got", got)
|
|
|
|
+ t.Error("want", want)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestMaxFloat64(t *testing.T) {
|
|
func TestMaxFloat64(t *testing.T) {
|
|
- type TestCase struct {
|
|
|
|
- Input float64
|
|
|
|
- N float64
|
|
|
|
- Excl bool
|
|
|
|
- Err error
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- 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 n, tc := range testCases {
|
|
|
|
- t.Logf("(%d) Testing %g against maximum of %g", n, tc.Input, tc.N)
|
|
|
|
-
|
|
|
|
- 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)
|
|
|
|
|
|
+ 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)},
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for setup, subSetup := range testCases {
|
|
|
|
+ for excl, values := range subSetup {
|
|
|
|
+ testMax := MaxFloat64(setup, excl)
|
|
|
|
+
|
|
|
|
+ for input, want := range values {
|
|
|
|
+ t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
|
|
|
|
+ got := testMax(input)
|
|
|
|
+
|
|
|
|
+ if !errors.Is(got, want) {
|
|
|
|
+ t.Error("got", got)
|
|
|
|
+ t.Error("want", want)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestMin(t *testing.T) {
|
|
func TestMin(t *testing.T) {
|
|
- type TestCase struct {
|
|
|
|
- Input int
|
|
|
|
- N int
|
|
|
|
- Excl bool
|
|
|
|
- Err error
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- 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 n, tc := range testCases {
|
|
|
|
- t.Logf("(%d) Testing %d against minimum of %d", n, tc.Input, tc.N)
|
|
|
|
-
|
|
|
|
- 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)
|
|
|
|
|
|
+ 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},
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for setup, subSetup := range testCases {
|
|
|
|
+ for excl, values := range subSetup {
|
|
|
|
+ testMin := Min(setup, excl)
|
|
|
|
+
|
|
|
|
+ for input, want := range values {
|
|
|
|
+ t.Run(fmt.Sprintf("%d/%v/%d", setup, excl, input), func(t *testing.T) {
|
|
|
|
+ got := testMin(input)
|
|
|
|
+
|
|
|
|
+ if !errors.Is(got, want) {
|
|
|
|
+ t.Error("got", got)
|
|
|
|
+ t.Error("want", want)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestMinFloat32(t *testing.T) {
|
|
func TestMinFloat32(t *testing.T) {
|
|
- type TestCase struct {
|
|
|
|
- Input float32
|
|
|
|
- N float32
|
|
|
|
- Excl bool
|
|
|
|
- Err error
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- 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 n, tc := range testCases {
|
|
|
|
- t.Logf("(%d) Testing %g against minimum of %g", n, tc.Input, tc.N)
|
|
|
|
-
|
|
|
|
- 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)
|
|
|
|
|
|
+ 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},
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for setup, subSetup := range testCases {
|
|
|
|
+ for excl, values := range subSetup {
|
|
|
|
+ testMin := MinFloat32(setup, excl)
|
|
|
|
+
|
|
|
|
+ for input, want := range values {
|
|
|
|
+ t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
|
|
|
|
+ got := testMin(input)
|
|
|
|
+
|
|
|
|
+ if !errors.Is(got, want) {
|
|
|
|
+ t.Error("got", got)
|
|
|
|
+ t.Error("want", want)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestMinFloat64(t *testing.T) {
|
|
func TestMinFloat64(t *testing.T) {
|
|
- type TestCase struct {
|
|
|
|
- Input float64
|
|
|
|
- N float64
|
|
|
|
- Excl bool
|
|
|
|
- Err error
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- 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 n, tc := range testCases {
|
|
|
|
- t.Logf("(%d) Testing %g against minimum of %g", n, tc.Input, tc.N)
|
|
|
|
-
|
|
|
|
- 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)
|
|
|
|
|
|
+ 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},
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for setup, subSetup := range testCases {
|
|
|
|
+ for excl, values := range subSetup {
|
|
|
|
+ testMin := MinFloat64(setup, excl)
|
|
|
|
+
|
|
|
|
+ for input, want := range values {
|
|
|
|
+ t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
|
|
|
|
+ got := testMin(input)
|
|
|
|
+
|
|
|
|
+ if !errors.Is(got, want) {
|
|
|
|
+ t.Error("got", got)
|
|
|
|
+ t.Error("want", want)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|