be_numerically_matcher.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package matchers
  2. import (
  3. "fmt"
  4. "math"
  5. "github.com/onsi/gomega/format"
  6. )
  7. type BeNumericallyMatcher struct {
  8. Comparator string
  9. CompareTo []interface{}
  10. }
  11. func (matcher *BeNumericallyMatcher) FailureMessage(actual interface{}) (message string) {
  12. return matcher.FormatFailureMessage(actual, false)
  13. }
  14. func (matcher *BeNumericallyMatcher) NegatedFailureMessage(actual interface{}) (message string) {
  15. return matcher.FormatFailureMessage(actual, true)
  16. }
  17. func (matcher *BeNumericallyMatcher) FormatFailureMessage(actual interface{}, negated bool) (message string) {
  18. if len(matcher.CompareTo) == 1 {
  19. message = fmt.Sprintf("to be %s", matcher.Comparator)
  20. } else {
  21. message = fmt.Sprintf("to be within %v of %s", matcher.CompareTo[1], matcher.Comparator)
  22. }
  23. if negated {
  24. message = "not " + message
  25. }
  26. return format.Message(actual, message, matcher.CompareTo[0])
  27. }
  28. func (matcher *BeNumericallyMatcher) Match(actual interface{}) (success bool, err error) {
  29. if len(matcher.CompareTo) == 0 || len(matcher.CompareTo) > 2 {
  30. return false, fmt.Errorf("BeNumerically requires 1 or 2 CompareTo arguments. Got:\n%s", format.Object(matcher.CompareTo, 1))
  31. }
  32. if !isNumber(actual) {
  33. return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(actual, 1))
  34. }
  35. if !isNumber(matcher.CompareTo[0]) {
  36. return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(matcher.CompareTo[0], 1))
  37. }
  38. if len(matcher.CompareTo) == 2 && !isNumber(matcher.CompareTo[1]) {
  39. return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(matcher.CompareTo[0], 1))
  40. }
  41. switch matcher.Comparator {
  42. case "==", "~", ">", ">=", "<", "<=":
  43. default:
  44. return false, fmt.Errorf("Unknown comparator: %s", matcher.Comparator)
  45. }
  46. if isFloat(actual) || isFloat(matcher.CompareTo[0]) {
  47. var secondOperand float64 = 1e-8
  48. if len(matcher.CompareTo) == 2 {
  49. secondOperand = toFloat(matcher.CompareTo[1])
  50. }
  51. success = matcher.matchFloats(toFloat(actual), toFloat(matcher.CompareTo[0]), secondOperand)
  52. } else if isInteger(actual) {
  53. var secondOperand int64 = 0
  54. if len(matcher.CompareTo) == 2 {
  55. secondOperand = toInteger(matcher.CompareTo[1])
  56. }
  57. success = matcher.matchIntegers(toInteger(actual), toInteger(matcher.CompareTo[0]), secondOperand)
  58. } else if isUnsignedInteger(actual) {
  59. var secondOperand uint64 = 0
  60. if len(matcher.CompareTo) == 2 {
  61. secondOperand = toUnsignedInteger(matcher.CompareTo[1])
  62. }
  63. success = matcher.matchUnsignedIntegers(toUnsignedInteger(actual), toUnsignedInteger(matcher.CompareTo[0]), secondOperand)
  64. } else {
  65. return false, fmt.Errorf("Failed to compare:\n%s\n%s:\n%s", format.Object(actual, 1), matcher.Comparator, format.Object(matcher.CompareTo[0], 1))
  66. }
  67. return success, nil
  68. }
  69. func (matcher *BeNumericallyMatcher) matchIntegers(actual, compareTo, threshold int64) (success bool) {
  70. switch matcher.Comparator {
  71. case "==", "~":
  72. diff := actual - compareTo
  73. return -threshold <= diff && diff <= threshold
  74. case ">":
  75. return (actual > compareTo)
  76. case ">=":
  77. return (actual >= compareTo)
  78. case "<":
  79. return (actual < compareTo)
  80. case "<=":
  81. return (actual <= compareTo)
  82. }
  83. return false
  84. }
  85. func (matcher *BeNumericallyMatcher) matchUnsignedIntegers(actual, compareTo, threshold uint64) (success bool) {
  86. switch matcher.Comparator {
  87. case "==", "~":
  88. if actual < compareTo {
  89. actual, compareTo = compareTo, actual
  90. }
  91. return actual-compareTo <= threshold
  92. case ">":
  93. return (actual > compareTo)
  94. case ">=":
  95. return (actual >= compareTo)
  96. case "<":
  97. return (actual < compareTo)
  98. case "<=":
  99. return (actual <= compareTo)
  100. }
  101. return false
  102. }
  103. func (matcher *BeNumericallyMatcher) matchFloats(actual, compareTo, threshold float64) (success bool) {
  104. switch matcher.Comparator {
  105. case "~":
  106. return math.Abs(actual-compareTo) <= threshold
  107. case "==":
  108. return (actual == compareTo)
  109. case ">":
  110. return (actual > compareTo)
  111. case ">=":
  112. return (actual >= compareTo)
  113. case "<":
  114. return (actual < compareTo)
  115. case "<=":
  116. return (actual <= compareTo)
  117. }
  118. return false
  119. }