123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 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) {
- 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) {
- 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) {
- 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) {
- 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) {
- 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) {
- 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)
- }
- })
- }
- }
- }
- }
|