quantity.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package assertions
  2. import (
  3. "fmt"
  4. "github.com/smartystreets/assertions/internal/oglematchers"
  5. )
  6. // ShouldBeGreaterThan receives exactly two parameters and ensures that the first is greater than the second.
  7. func ShouldBeGreaterThan(actual interface{}, expected ...interface{}) string {
  8. if fail := need(1, expected); fail != success {
  9. return fail
  10. }
  11. if matchError := oglematchers.GreaterThan(expected[0]).Matches(actual); matchError != nil {
  12. return fmt.Sprintf(shouldHaveBeenGreater, actual, expected[0])
  13. }
  14. return success
  15. }
  16. // ShouldBeGreaterThanOrEqualTo receives exactly two parameters and ensures that the first is greater than or equal to the second.
  17. func ShouldBeGreaterThanOrEqualTo(actual interface{}, expected ...interface{}) string {
  18. if fail := need(1, expected); fail != success {
  19. return fail
  20. } else if matchError := oglematchers.GreaterOrEqual(expected[0]).Matches(actual); matchError != nil {
  21. return fmt.Sprintf(shouldHaveBeenGreaterOrEqual, actual, expected[0])
  22. }
  23. return success
  24. }
  25. // ShouldBeLessThan receives exactly two parameters and ensures that the first is less than the second.
  26. func ShouldBeLessThan(actual interface{}, expected ...interface{}) string {
  27. if fail := need(1, expected); fail != success {
  28. return fail
  29. } else if matchError := oglematchers.LessThan(expected[0]).Matches(actual); matchError != nil {
  30. return fmt.Sprintf(shouldHaveBeenLess, actual, expected[0])
  31. }
  32. return success
  33. }
  34. // ShouldBeLessThan receives exactly two parameters and ensures that the first is less than or equal to the second.
  35. func ShouldBeLessThanOrEqualTo(actual interface{}, expected ...interface{}) string {
  36. if fail := need(1, expected); fail != success {
  37. return fail
  38. } else if matchError := oglematchers.LessOrEqual(expected[0]).Matches(actual); matchError != nil {
  39. return fmt.Sprintf(shouldHaveBeenLess, actual, expected[0])
  40. }
  41. return success
  42. }
  43. // ShouldBeBetween receives exactly three parameters: an actual value, a lower bound, and an upper bound.
  44. // It ensures that the actual value is between both bounds (but not equal to either of them).
  45. func ShouldBeBetween(actual interface{}, expected ...interface{}) string {
  46. if fail := need(2, expected); fail != success {
  47. return fail
  48. }
  49. lower, upper, fail := deriveBounds(expected)
  50. if fail != success {
  51. return fail
  52. } else if !isBetween(actual, lower, upper) {
  53. return fmt.Sprintf(shouldHaveBeenBetween, actual, lower, upper)
  54. }
  55. return success
  56. }
  57. // ShouldNotBeBetween receives exactly three parameters: an actual value, a lower bound, and an upper bound.
  58. // It ensures that the actual value is NOT between both bounds.
  59. func ShouldNotBeBetween(actual interface{}, expected ...interface{}) string {
  60. if fail := need(2, expected); fail != success {
  61. return fail
  62. }
  63. lower, upper, fail := deriveBounds(expected)
  64. if fail != success {
  65. return fail
  66. } else if isBetween(actual, lower, upper) {
  67. return fmt.Sprintf(shouldNotHaveBeenBetween, actual, lower, upper)
  68. }
  69. return success
  70. }
  71. func deriveBounds(values []interface{}) (lower interface{}, upper interface{}, fail string) {
  72. lower = values[0]
  73. upper = values[1]
  74. if ShouldNotEqual(lower, upper) != success {
  75. return nil, nil, fmt.Sprintf(shouldHaveDifferentUpperAndLower, lower)
  76. } else if ShouldBeLessThan(lower, upper) != success {
  77. lower, upper = upper, lower
  78. }
  79. return lower, upper, success
  80. }
  81. func isBetween(value, lower, upper interface{}) bool {
  82. if ShouldBeGreaterThan(value, lower) != success {
  83. return false
  84. } else if ShouldBeLessThan(value, upper) != success {
  85. return false
  86. }
  87. return true
  88. }
  89. // ShouldBeBetweenOrEqual receives exactly three parameters: an actual value, a lower bound, and an upper bound.
  90. // It ensures that the actual value is between both bounds or equal to one of them.
  91. func ShouldBeBetweenOrEqual(actual interface{}, expected ...interface{}) string {
  92. if fail := need(2, expected); fail != success {
  93. return fail
  94. }
  95. lower, upper, fail := deriveBounds(expected)
  96. if fail != success {
  97. return fail
  98. } else if !isBetweenOrEqual(actual, lower, upper) {
  99. return fmt.Sprintf(shouldHaveBeenBetweenOrEqual, actual, lower, upper)
  100. }
  101. return success
  102. }
  103. // ShouldNotBeBetweenOrEqual receives exactly three parameters: an actual value, a lower bound, and an upper bound.
  104. // It ensures that the actual value is nopt between the bounds nor equal to either of them.
  105. func ShouldNotBeBetweenOrEqual(actual interface{}, expected ...interface{}) string {
  106. if fail := need(2, expected); fail != success {
  107. return fail
  108. }
  109. lower, upper, fail := deriveBounds(expected)
  110. if fail != success {
  111. return fail
  112. } else if isBetweenOrEqual(actual, lower, upper) {
  113. return fmt.Sprintf(shouldNotHaveBeenBetweenOrEqual, actual, lower, upper)
  114. }
  115. return success
  116. }
  117. func isBetweenOrEqual(value, lower, upper interface{}) bool {
  118. if ShouldBeGreaterThanOrEqualTo(value, lower) != success {
  119. return false
  120. } else if ShouldBeLessThanOrEqualTo(value, upper) != success {
  121. return false
  122. }
  123. return true
  124. }