mirror of
https://github.com/k8snetworkplumbingwg/whereabouts.git
synced 2025-06-03 06:42:26 +00:00
![dependabot[bot]](/assets/img/avatar_default.png)
Bumps the other-go-modules group with 8 updates: | Package | From | To | | --- | --- | --- | | [github.com/containernetworking/cni](https://github.com/containernetworking/cni) | `1.2.3` | `1.3.0` | | [github.com/containernetworking/plugins](https://github.com/containernetworking/plugins) | `1.6.1` | `1.6.2` | | [github.com/k8snetworkplumbingwg/network-attachment-definition-client](https://github.com/k8snetworkplumbingwg/network-attachment-definition-client) | `1.7.0` | `1.7.6` | | [github.com/onsi/gomega](https://github.com/onsi/gomega) | `1.36.2` | `1.37.0` | | [gomodules.xyz/jsonpatch/v2](https://github.com/gomodules/jsonpatch) | `2.4.0` | `2.5.0` | | [github.com/go-co-op/gocron/v2](https://github.com/go-co-op/gocron) | `2.12.4` | `2.16.1` | | [github.com/fsnotify/fsnotify](https://github.com/fsnotify/fsnotify) | `1.8.0` | `1.9.0` | | [golang.org/x/time](https://github.com/golang/time) | `0.8.0` | `0.11.0` | Updates `github.com/containernetworking/cni` from 1.2.3 to 1.3.0 - [Release notes](https://github.com/containernetworking/cni/releases) - [Commits](https://github.com/containernetworking/cni/compare/v1.2.3...v1.3.0) Updates `github.com/containernetworking/plugins` from 1.6.1 to 1.6.2 - [Release notes](https://github.com/containernetworking/plugins/releases) - [Commits](https://github.com/containernetworking/plugins/compare/v1.6.1...v1.6.2) Updates `github.com/k8snetworkplumbingwg/network-attachment-definition-client` from 1.7.0 to 1.7.6 - [Release notes](https://github.com/k8snetworkplumbingwg/network-attachment-definition-client/releases) - [Commits](https://github.com/k8snetworkplumbingwg/network-attachment-definition-client/compare/v1.7.0...v1.7.6) Updates `github.com/onsi/gomega` from 1.36.2 to 1.37.0 - [Release notes](https://github.com/onsi/gomega/releases) - [Changelog](https://github.com/onsi/gomega/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/gomega/compare/v1.36.2...v1.37.0) Updates `gomodules.xyz/jsonpatch/v2` from 2.4.0 to 2.5.0 - [Release notes](https://github.com/gomodules/jsonpatch/releases) - [Changelog](https://github.com/gomodules/jsonpatch/blob/release-2.0/CHANGELOG.md) - [Commits](https://github.com/gomodules/jsonpatch/compare/v2.4.0...v2.5.0) Updates `github.com/go-co-op/gocron/v2` from 2.12.4 to 2.16.1 - [Release notes](https://github.com/go-co-op/gocron/releases) - [Commits](https://github.com/go-co-op/gocron/compare/v2.12.4...v2.16.1) Updates `github.com/fsnotify/fsnotify` from 1.8.0 to 1.9.0 - [Release notes](https://github.com/fsnotify/fsnotify/releases) - [Changelog](https://github.com/fsnotify/fsnotify/blob/main/CHANGELOG.md) - [Commits](https://github.com/fsnotify/fsnotify/compare/v1.8.0...v1.9.0) Updates `golang.org/x/time` from 0.8.0 to 0.11.0 - [Commits](https://github.com/golang/time/compare/v0.8.0...v0.11.0) --- updated-dependencies: - dependency-name: github.com/containernetworking/cni dependency-version: 1.3.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: other-go-modules - dependency-name: github.com/containernetworking/plugins dependency-version: 1.6.2 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: other-go-modules - dependency-name: github.com/k8snetworkplumbingwg/network-attachment-definition-client dependency-version: 1.7.6 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: other-go-modules - dependency-name: github.com/onsi/gomega dependency-version: 1.37.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: other-go-modules - dependency-name: gomodules.xyz/jsonpatch/v2 dependency-version: 2.5.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: other-go-modules - dependency-name: github.com/go-co-op/gocron/v2 dependency-version: 2.16.1 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: other-go-modules - dependency-name: github.com/fsnotify/fsnotify dependency-version: 1.9.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: other-go-modules - dependency-name: golang.org/x/time dependency-version: 0.11.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: other-go-modules ... Signed-off-by: dependabot[bot] <support@github.com>
135 lines
3.9 KiB
Go
135 lines
3.9 KiB
Go
// untested sections: 4
|
|
|
|
package matchers
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
|
|
"github.com/onsi/gomega/format"
|
|
)
|
|
|
|
type BeNumericallyMatcher struct {
|
|
Comparator string
|
|
CompareTo []any
|
|
}
|
|
|
|
func (matcher *BeNumericallyMatcher) FailureMessage(actual any) (message string) {
|
|
return matcher.FormatFailureMessage(actual, false)
|
|
}
|
|
|
|
func (matcher *BeNumericallyMatcher) NegatedFailureMessage(actual any) (message string) {
|
|
return matcher.FormatFailureMessage(actual, true)
|
|
}
|
|
|
|
func (matcher *BeNumericallyMatcher) FormatFailureMessage(actual any, negated bool) (message string) {
|
|
if len(matcher.CompareTo) == 1 {
|
|
message = fmt.Sprintf("to be %s", matcher.Comparator)
|
|
} else {
|
|
message = fmt.Sprintf("to be within %v of %s", matcher.CompareTo[1], matcher.Comparator)
|
|
}
|
|
if negated {
|
|
message = "not " + message
|
|
}
|
|
return format.Message(actual, message, matcher.CompareTo[0])
|
|
}
|
|
|
|
func (matcher *BeNumericallyMatcher) Match(actual any) (success bool, err error) {
|
|
if len(matcher.CompareTo) == 0 || len(matcher.CompareTo) > 2 {
|
|
return false, fmt.Errorf("BeNumerically requires 1 or 2 CompareTo arguments. Got:\n%s", format.Object(matcher.CompareTo, 1))
|
|
}
|
|
if !isNumber(actual) {
|
|
return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(actual, 1))
|
|
}
|
|
if !isNumber(matcher.CompareTo[0]) {
|
|
return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(matcher.CompareTo[0], 1))
|
|
}
|
|
if len(matcher.CompareTo) == 2 && !isNumber(matcher.CompareTo[1]) {
|
|
return false, fmt.Errorf("Expected a number. Got:\n%s", format.Object(matcher.CompareTo[1], 1))
|
|
}
|
|
|
|
switch matcher.Comparator {
|
|
case "==", "~", ">", ">=", "<", "<=":
|
|
default:
|
|
return false, fmt.Errorf("Unknown comparator: %s", matcher.Comparator)
|
|
}
|
|
|
|
if isFloat(actual) || isFloat(matcher.CompareTo[0]) {
|
|
var secondOperand float64 = 1e-8
|
|
if len(matcher.CompareTo) == 2 {
|
|
secondOperand = toFloat(matcher.CompareTo[1])
|
|
}
|
|
success = matcher.matchFloats(toFloat(actual), toFloat(matcher.CompareTo[0]), secondOperand)
|
|
} else if isInteger(actual) {
|
|
var secondOperand int64 = 0
|
|
if len(matcher.CompareTo) == 2 {
|
|
secondOperand = toInteger(matcher.CompareTo[1])
|
|
}
|
|
success = matcher.matchIntegers(toInteger(actual), toInteger(matcher.CompareTo[0]), secondOperand)
|
|
} else if isUnsignedInteger(actual) {
|
|
var secondOperand uint64 = 0
|
|
if len(matcher.CompareTo) == 2 {
|
|
secondOperand = toUnsignedInteger(matcher.CompareTo[1])
|
|
}
|
|
success = matcher.matchUnsignedIntegers(toUnsignedInteger(actual), toUnsignedInteger(matcher.CompareTo[0]), secondOperand)
|
|
} else {
|
|
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))
|
|
}
|
|
|
|
return success, nil
|
|
}
|
|
|
|
func (matcher *BeNumericallyMatcher) matchIntegers(actual, compareTo, threshold int64) (success bool) {
|
|
switch matcher.Comparator {
|
|
case "==", "~":
|
|
diff := actual - compareTo
|
|
return -threshold <= diff && diff <= threshold
|
|
case ">":
|
|
return (actual > compareTo)
|
|
case ">=":
|
|
return (actual >= compareTo)
|
|
case "<":
|
|
return (actual < compareTo)
|
|
case "<=":
|
|
return (actual <= compareTo)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (matcher *BeNumericallyMatcher) matchUnsignedIntegers(actual, compareTo, threshold uint64) (success bool) {
|
|
switch matcher.Comparator {
|
|
case "==", "~":
|
|
if actual < compareTo {
|
|
actual, compareTo = compareTo, actual
|
|
}
|
|
return actual-compareTo <= threshold
|
|
case ">":
|
|
return (actual > compareTo)
|
|
case ">=":
|
|
return (actual >= compareTo)
|
|
case "<":
|
|
return (actual < compareTo)
|
|
case "<=":
|
|
return (actual <= compareTo)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (matcher *BeNumericallyMatcher) matchFloats(actual, compareTo, threshold float64) (success bool) {
|
|
switch matcher.Comparator {
|
|
case "~":
|
|
return math.Abs(actual-compareTo) <= threshold
|
|
case "==":
|
|
return (actual == compareTo)
|
|
case ">":
|
|
return (actual > compareTo)
|
|
case ">=":
|
|
return (actual >= compareTo)
|
|
case "<":
|
|
return (actual < compareTo)
|
|
case "<=":
|
|
return (actual <= compareTo)
|
|
}
|
|
return false
|
|
}
|