Path: blob/main/vendor/github.com/onsi/gomega/matchers/be_numerically_matcher.go
2880 views
// untested sections: 412package matchers34import (5"fmt"6"math"78"github.com/onsi/gomega/format"9)1011type BeNumericallyMatcher struct {12Comparator string13CompareTo []any14}1516func (matcher *BeNumericallyMatcher) FailureMessage(actual any) (message string) {17return matcher.FormatFailureMessage(actual, false)18}1920func (matcher *BeNumericallyMatcher) NegatedFailureMessage(actual any) (message string) {21return matcher.FormatFailureMessage(actual, true)22}2324func (matcher *BeNumericallyMatcher) FormatFailureMessage(actual any, negated bool) (message string) {25if len(matcher.CompareTo) == 1 {26message = fmt.Sprintf("to be %s", matcher.Comparator)27} else {28message = fmt.Sprintf("to be within %v of %s", matcher.CompareTo[1], matcher.Comparator)29}30if negated {31message = "not " + message32}33return format.Message(actual, message, matcher.CompareTo[0])34}3536func (matcher *BeNumericallyMatcher) Match(actual any) (success bool, err error) {37if len(matcher.CompareTo) == 0 || len(matcher.CompareTo) > 2 {38return false, fmt.Errorf("BeNumerically requires 1 or 2 CompareTo arguments. Got:\n%s", format.Object(matcher.CompareTo, 1))39}40if !isNumber(actual) {41return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(actual, 1))42}43if !isNumber(matcher.CompareTo[0]) {44return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(matcher.CompareTo[0], 1))45}46if len(matcher.CompareTo) == 2 && !isNumber(matcher.CompareTo[1]) {47return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(matcher.CompareTo[1], 1))48}4950switch matcher.Comparator {51case "==", "~", ">", ">=", "<", "<=":52default:53return false, fmt.Errorf("Unknown comparator: %s", matcher.Comparator)54}5556if isFloat(actual) || isFloat(matcher.CompareTo[0]) {57var secondOperand float64 = 1e-858if len(matcher.CompareTo) == 2 {59secondOperand = toFloat(matcher.CompareTo[1])60}61success = matcher.matchFloats(toFloat(actual), toFloat(matcher.CompareTo[0]), secondOperand)62} else if isInteger(actual) {63var secondOperand int64 = 064if len(matcher.CompareTo) == 2 {65secondOperand = toInteger(matcher.CompareTo[1])66}67success = matcher.matchIntegers(toInteger(actual), toInteger(matcher.CompareTo[0]), secondOperand)68} else if isUnsignedInteger(actual) {69var secondOperand uint64 = 070if len(matcher.CompareTo) == 2 {71secondOperand = toUnsignedInteger(matcher.CompareTo[1])72}73success = matcher.matchUnsignedIntegers(toUnsignedInteger(actual), toUnsignedInteger(matcher.CompareTo[0]), secondOperand)74} else {75return false, fmt.Errorf("Failed to compare:\n%s\n%s:\n%s", format.Object(actual, 1), matcher.Comparator, format.Object(matcher.CompareTo[0], 1))76}7778return success, nil79}8081func (matcher *BeNumericallyMatcher) matchIntegers(actual, compareTo, threshold int64) (success bool) {82switch matcher.Comparator {83case "==", "~":84diff := actual - compareTo85return -threshold <= diff && diff <= threshold86case ">":87return (actual > compareTo)88case ">=":89return (actual >= compareTo)90case "<":91return (actual < compareTo)92case "<=":93return (actual <= compareTo)94}95return false96}9798func (matcher *BeNumericallyMatcher) matchUnsignedIntegers(actual, compareTo, threshold uint64) (success bool) {99switch matcher.Comparator {100case "==", "~":101if actual < compareTo {102actual, compareTo = compareTo, actual103}104return actual-compareTo <= threshold105case ">":106return (actual > compareTo)107case ">=":108return (actual >= compareTo)109case "<":110return (actual < compareTo)111case "<=":112return (actual <= compareTo)113}114return false115}116117func (matcher *BeNumericallyMatcher) matchFloats(actual, compareTo, threshold float64) (success bool) {118switch matcher.Comparator {119case "~":120return math.Abs(actual-compareTo) <= threshold121case "==":122return (actual == compareTo)123case ">":124return (actual > compareTo)125case ">=":126return (actual >= compareTo)127case "<":128return (actual < compareTo)129case "<=":130return (actual <= compareTo)131}132return false133}134135136