number_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package validate
  2. import (
  3. "errors"
  4. "fmt"
  5. "testing"
  6. )
  7. func ExampleMax() {
  8. testMax := Max(10, true)
  9. fmt.Println(testMax(10))
  10. // Output: must be less than 10
  11. }
  12. func ExampleMin() {
  13. testMin := Min(10, false)
  14. fmt.Println(testMin(5))
  15. // Output: must be greater than or equal to 10
  16. }
  17. func TestMax(t *testing.T) {
  18. testCases := map[int]map[bool]map[int]error{
  19. 10: {
  20. true: {0: nil, 1: nil, 2: nil, 10: ErrMustBeLess.With(10), 100: ErrMustBeLess.With(10)},
  21. false: {0: nil, 1: nil, 2: nil, 10: nil, 100: ErrMustBeLessOrEqual.With(10)},
  22. },
  23. }
  24. for setup, subSetup := range testCases {
  25. for excl, values := range subSetup {
  26. testMax := Max(setup, excl)
  27. for input, want := range values {
  28. t.Run(fmt.Sprintf("%d/%v/%d", setup, excl, input), func(t *testing.T) {
  29. got := testMax(input)
  30. if !errors.Is(got, want) {
  31. t.Error("got", got)
  32. t.Error("want", want)
  33. }
  34. })
  35. }
  36. }
  37. }
  38. }
  39. func TestMaxFloat32(t *testing.T) {
  40. testCases := map[float32]map[bool]map[float32]error{
  41. 10: {
  42. true: {0: nil, 1: nil, 2: nil, 10: ErrMustBeLess.With(10), 100: ErrMustBeLess.With(10)},
  43. false: {0: nil, 1: nil, 2: nil, 10: nil, 100: ErrMustBeLessOrEqual.With(10)},
  44. },
  45. }
  46. for setup, subSetup := range testCases {
  47. for excl, values := range subSetup {
  48. testMax := MaxFloat32(setup, excl)
  49. for input, want := range values {
  50. t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
  51. got := testMax(input)
  52. if !errors.Is(got, want) {
  53. t.Error("got", got)
  54. t.Error("want", want)
  55. }
  56. })
  57. }
  58. }
  59. }
  60. }
  61. func TestMaxFloat64(t *testing.T) {
  62. testCases := map[float64]map[bool]map[float64]error{
  63. 10: {
  64. true: {0: nil, 1: nil, 2: nil, 10: ErrMustBeLess.With(10), 100: ErrMustBeLess.With(10)},
  65. false: {0: nil, 1: nil, 2: nil, 10: nil, 100: ErrMustBeLessOrEqual.With(10)},
  66. },
  67. }
  68. for setup, subSetup := range testCases {
  69. for excl, values := range subSetup {
  70. testMax := MaxFloat64(setup, excl)
  71. for input, want := range values {
  72. t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
  73. got := testMax(input)
  74. if !errors.Is(got, want) {
  75. t.Error("got", got)
  76. t.Error("want", want)
  77. }
  78. })
  79. }
  80. }
  81. }
  82. }
  83. func TestMin(t *testing.T) {
  84. testCases := map[int]map[bool]map[int]error{
  85. 10: {
  86. true: {0: ErrMustBeGreater.With(10), 1: ErrMustBeGreater.With(10), 2: ErrMustBeGreater.With(10), 10: ErrMustBeGreater.With(10), 100: nil},
  87. false: {0: ErrMustBeGreaterOrEqual.With(10), 1: ErrMustBeGreaterOrEqual.With(10), 2: ErrMustBeGreaterOrEqual.With(10), 10: nil, 100: nil},
  88. },
  89. }
  90. for setup, subSetup := range testCases {
  91. for excl, values := range subSetup {
  92. testMin := Min(setup, excl)
  93. for input, want := range values {
  94. t.Run(fmt.Sprintf("%d/%v/%d", setup, excl, input), func(t *testing.T) {
  95. got := testMin(input)
  96. if !errors.Is(got, want) {
  97. t.Error("got", got)
  98. t.Error("want", want)
  99. }
  100. })
  101. }
  102. }
  103. }
  104. }
  105. func TestMinFloat32(t *testing.T) {
  106. testCases := map[float32]map[bool]map[float32]error{
  107. 10: {
  108. true: {0: ErrMustBeGreater.With(10), 1: ErrMustBeGreater.With(10), 2: ErrMustBeGreater.With(10), 10: ErrMustBeGreater.With(10), 100: nil},
  109. false: {0: ErrMustBeGreaterOrEqual.With(10), 1: ErrMustBeGreaterOrEqual.With(10), 2: ErrMustBeGreaterOrEqual.With(10), 10: nil, 100: nil},
  110. },
  111. }
  112. for setup, subSetup := range testCases {
  113. for excl, values := range subSetup {
  114. testMin := MinFloat32(setup, excl)
  115. for input, want := range values {
  116. t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
  117. got := testMin(input)
  118. if !errors.Is(got, want) {
  119. t.Error("got", got)
  120. t.Error("want", want)
  121. }
  122. })
  123. }
  124. }
  125. }
  126. }
  127. func TestMinFloat64(t *testing.T) {
  128. testCases := map[float64]map[bool]map[float64]error{
  129. 10: {
  130. true: {0: ErrMustBeGreater.With(10), 1: ErrMustBeGreater.With(10), 2: ErrMustBeGreater.With(10), 10: ErrMustBeGreater.With(10), 100: nil},
  131. false: {0: ErrMustBeGreaterOrEqual.With(10), 1: ErrMustBeGreaterOrEqual.With(10), 2: ErrMustBeGreaterOrEqual.With(10), 10: nil, 100: nil},
  132. },
  133. }
  134. for setup, subSetup := range testCases {
  135. for excl, values := range subSetup {
  136. testMin := MinFloat64(setup, excl)
  137. for input, want := range values {
  138. t.Run(fmt.Sprintf("%f/%v/%f", setup, excl, input), func(t *testing.T) {
  139. got := testMin(input)
  140. if !errors.Is(got, want) {
  141. t.Error("got", got)
  142. t.Error("want", want)
  143. }
  144. })
  145. }
  146. }
  147. }
  148. }