7 "github.com/onsi/gomega/format"
10 type BeNumericallyMatcher struct {
12 CompareTo []interface{}
15 func (matcher *BeNumericallyMatcher) FailureMessage(actual interface{}) (message string) {
16 return format.Message(actual, fmt.Sprintf("to be %s", matcher.Comparator), matcher.CompareTo[0])
19 func (matcher *BeNumericallyMatcher) NegatedFailureMessage(actual interface{}) (message string) {
20 return format.Message(actual, fmt.Sprintf("not to be %s", matcher.Comparator), matcher.CompareTo[0])
23 func (matcher *BeNumericallyMatcher) Match(actual interface{}) (success bool, err error) {
24 if len(matcher.CompareTo) == 0 || len(matcher.CompareTo) > 2 {
25 return false, fmt.Errorf("BeNumerically requires 1 or 2 CompareTo arguments. Got:\n%s", format.Object(matcher.CompareTo, 1))
27 if !isNumber(actual) {
28 return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(actual, 1))
30 if !isNumber(matcher.CompareTo[0]) {
31 return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(matcher.CompareTo[0], 1))
33 if len(matcher.CompareTo) == 2 && !isNumber(matcher.CompareTo[1]) {
34 return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(matcher.CompareTo[0], 1))
37 switch matcher.Comparator {
38 case "==", "~", ">", ">=", "<", "<=":
40 return false, fmt.Errorf("Unknown comparator: %s", matcher.Comparator)
43 if isFloat(actual) || isFloat(matcher.CompareTo[0]) {
44 var secondOperand float64 = 1e-8
45 if len(matcher.CompareTo) == 2 {
46 secondOperand = toFloat(matcher.CompareTo[1])
48 success = matcher.matchFloats(toFloat(actual), toFloat(matcher.CompareTo[0]), secondOperand)
49 } else if isInteger(actual) {
50 var secondOperand int64 = 0
51 if len(matcher.CompareTo) == 2 {
52 secondOperand = toInteger(matcher.CompareTo[1])
54 success = matcher.matchIntegers(toInteger(actual), toInteger(matcher.CompareTo[0]), secondOperand)
55 } else if isUnsignedInteger(actual) {
56 var secondOperand uint64 = 0
57 if len(matcher.CompareTo) == 2 {
58 secondOperand = toUnsignedInteger(matcher.CompareTo[1])
60 success = matcher.matchUnsignedIntegers(toUnsignedInteger(actual), toUnsignedInteger(matcher.CompareTo[0]), secondOperand)
62 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))
68 func (matcher *BeNumericallyMatcher) matchIntegers(actual, compareTo, threshold int64) (success bool) {
69 switch matcher.Comparator {
71 diff := actual - compareTo
72 return -threshold <= diff && diff <= threshold
74 return (actual > compareTo)
76 return (actual >= compareTo)
78 return (actual < compareTo)
80 return (actual <= compareTo)
85 func (matcher *BeNumericallyMatcher) matchUnsignedIntegers(actual, compareTo, threshold uint64) (success bool) {
86 switch matcher.Comparator {
88 if actual < compareTo {
89 actual, compareTo = compareTo, actual
91 return actual-compareTo <= threshold
93 return (actual > compareTo)
95 return (actual >= compareTo)
97 return (actual < compareTo)
99 return (actual <= compareTo)
104 func (matcher *BeNumericallyMatcher) matchFloats(actual, compareTo, threshold float64) (success bool) {
105 switch matcher.Comparator {
107 return math.Abs(actual-compareTo) <= threshold
109 return (actual == compareTo)
111 return (actual > compareTo)
113 return (actual >= compareTo)
115 return (actual < compareTo)
117 return (actual <= compareTo)