matterbridge/vendor/github.com/stretchr/testify/assert/assertion_compare.go

481 lines
12 KiB
Go
Raw Permalink Normal View History

2019-09-07 13:46:58 -07:00
package assert
import (
"bytes"
2019-09-07 13:46:58 -07:00
"fmt"
"reflect"
2022-04-25 14:50:10 -07:00
"time"
2019-09-07 13:46:58 -07:00
)
type CompareType int
const (
compareLess CompareType = iota - 1
compareEqual
compareGreater
)
2021-02-01 12:29:04 -08:00
var (
intType = reflect.TypeOf(int(1))
int8Type = reflect.TypeOf(int8(1))
int16Type = reflect.TypeOf(int16(1))
int32Type = reflect.TypeOf(int32(1))
int64Type = reflect.TypeOf(int64(1))
uintType = reflect.TypeOf(uint(1))
uint8Type = reflect.TypeOf(uint8(1))
uint16Type = reflect.TypeOf(uint16(1))
uint32Type = reflect.TypeOf(uint32(1))
uint64Type = reflect.TypeOf(uint64(1))
uintptrType = reflect.TypeOf(uintptr(1))
2021-02-01 12:29:04 -08:00
float32Type = reflect.TypeOf(float32(1))
float64Type = reflect.TypeOf(float64(1))
stringType = reflect.TypeOf("")
2022-04-25 14:50:10 -07:00
timeType = reflect.TypeOf(time.Time{})
bytesType = reflect.TypeOf([]byte{})
2021-02-01 12:29:04 -08:00
)
func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
2021-02-01 12:29:04 -08:00
obj1Value := reflect.ValueOf(obj1)
obj2Value := reflect.ValueOf(obj2)
// throughout this switch we try and avoid calling .Convert() if possible,
// as this has a pretty big performance impact
2019-09-07 13:46:58 -07:00
switch kind {
case reflect.Int:
{
2021-02-01 12:29:04 -08:00
intobj1, ok := obj1.(int)
if !ok {
intobj1 = obj1Value.Convert(intType).Interface().(int)
}
intobj2, ok := obj2.(int)
if !ok {
intobj2 = obj2Value.Convert(intType).Interface().(int)
}
2019-09-07 13:46:58 -07:00
if intobj1 > intobj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if intobj1 == intobj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if intobj1 < intobj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Int8:
{
2021-02-01 12:29:04 -08:00
int8obj1, ok := obj1.(int8)
if !ok {
int8obj1 = obj1Value.Convert(int8Type).Interface().(int8)
}
int8obj2, ok := obj2.(int8)
if !ok {
int8obj2 = obj2Value.Convert(int8Type).Interface().(int8)
}
2019-09-07 13:46:58 -07:00
if int8obj1 > int8obj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if int8obj1 == int8obj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if int8obj1 < int8obj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Int16:
{
2021-02-01 12:29:04 -08:00
int16obj1, ok := obj1.(int16)
if !ok {
int16obj1 = obj1Value.Convert(int16Type).Interface().(int16)
}
int16obj2, ok := obj2.(int16)
if !ok {
int16obj2 = obj2Value.Convert(int16Type).Interface().(int16)
}
2019-09-07 13:46:58 -07:00
if int16obj1 > int16obj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if int16obj1 == int16obj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if int16obj1 < int16obj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Int32:
{
2021-02-01 12:29:04 -08:00
int32obj1, ok := obj1.(int32)
if !ok {
int32obj1 = obj1Value.Convert(int32Type).Interface().(int32)
}
int32obj2, ok := obj2.(int32)
if !ok {
int32obj2 = obj2Value.Convert(int32Type).Interface().(int32)
}
2019-09-07 13:46:58 -07:00
if int32obj1 > int32obj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if int32obj1 == int32obj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if int32obj1 < int32obj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Int64:
{
2021-02-01 12:29:04 -08:00
int64obj1, ok := obj1.(int64)
if !ok {
int64obj1 = obj1Value.Convert(int64Type).Interface().(int64)
}
int64obj2, ok := obj2.(int64)
if !ok {
int64obj2 = obj2Value.Convert(int64Type).Interface().(int64)
}
2019-09-07 13:46:58 -07:00
if int64obj1 > int64obj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if int64obj1 == int64obj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if int64obj1 < int64obj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Uint:
{
2021-02-01 12:29:04 -08:00
uintobj1, ok := obj1.(uint)
if !ok {
uintobj1 = obj1Value.Convert(uintType).Interface().(uint)
}
uintobj2, ok := obj2.(uint)
if !ok {
uintobj2 = obj2Value.Convert(uintType).Interface().(uint)
}
2019-09-07 13:46:58 -07:00
if uintobj1 > uintobj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if uintobj1 == uintobj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if uintobj1 < uintobj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Uint8:
{
2021-02-01 12:29:04 -08:00
uint8obj1, ok := obj1.(uint8)
if !ok {
uint8obj1 = obj1Value.Convert(uint8Type).Interface().(uint8)
}
uint8obj2, ok := obj2.(uint8)
if !ok {
uint8obj2 = obj2Value.Convert(uint8Type).Interface().(uint8)
}
2019-09-07 13:46:58 -07:00
if uint8obj1 > uint8obj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if uint8obj1 == uint8obj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if uint8obj1 < uint8obj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Uint16:
{
2021-02-01 12:29:04 -08:00
uint16obj1, ok := obj1.(uint16)
if !ok {
uint16obj1 = obj1Value.Convert(uint16Type).Interface().(uint16)
}
uint16obj2, ok := obj2.(uint16)
if !ok {
uint16obj2 = obj2Value.Convert(uint16Type).Interface().(uint16)
}
2019-09-07 13:46:58 -07:00
if uint16obj1 > uint16obj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if uint16obj1 == uint16obj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if uint16obj1 < uint16obj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Uint32:
{
2021-02-01 12:29:04 -08:00
uint32obj1, ok := obj1.(uint32)
if !ok {
uint32obj1 = obj1Value.Convert(uint32Type).Interface().(uint32)
}
uint32obj2, ok := obj2.(uint32)
if !ok {
uint32obj2 = obj2Value.Convert(uint32Type).Interface().(uint32)
}
2019-09-07 13:46:58 -07:00
if uint32obj1 > uint32obj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if uint32obj1 == uint32obj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if uint32obj1 < uint32obj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Uint64:
{
2021-02-01 12:29:04 -08:00
uint64obj1, ok := obj1.(uint64)
if !ok {
uint64obj1 = obj1Value.Convert(uint64Type).Interface().(uint64)
}
uint64obj2, ok := obj2.(uint64)
if !ok {
uint64obj2 = obj2Value.Convert(uint64Type).Interface().(uint64)
}
2019-09-07 13:46:58 -07:00
if uint64obj1 > uint64obj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if uint64obj1 == uint64obj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if uint64obj1 < uint64obj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Float32:
{
2021-02-01 12:29:04 -08:00
float32obj1, ok := obj1.(float32)
if !ok {
float32obj1 = obj1Value.Convert(float32Type).Interface().(float32)
}
float32obj2, ok := obj2.(float32)
if !ok {
float32obj2 = obj2Value.Convert(float32Type).Interface().(float32)
}
2019-09-07 13:46:58 -07:00
if float32obj1 > float32obj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if float32obj1 == float32obj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if float32obj1 < float32obj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.Float64:
{
2021-02-01 12:29:04 -08:00
float64obj1, ok := obj1.(float64)
if !ok {
float64obj1 = obj1Value.Convert(float64Type).Interface().(float64)
}
float64obj2, ok := obj2.(float64)
if !ok {
float64obj2 = obj2Value.Convert(float64Type).Interface().(float64)
}
2019-09-07 13:46:58 -07:00
if float64obj1 > float64obj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if float64obj1 == float64obj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if float64obj1 < float64obj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
case reflect.String:
{
2021-02-01 12:29:04 -08:00
stringobj1, ok := obj1.(string)
if !ok {
stringobj1 = obj1Value.Convert(stringType).Interface().(string)
}
stringobj2, ok := obj2.(string)
if !ok {
stringobj2 = obj2Value.Convert(stringType).Interface().(string)
}
2019-09-07 13:46:58 -07:00
if stringobj1 > stringobj2 {
return compareGreater, true
2019-09-07 13:46:58 -07:00
}
if stringobj1 == stringobj2 {
return compareEqual, true
2019-09-07 13:46:58 -07:00
}
if stringobj1 < stringobj2 {
return compareLess, true
2019-09-07 13:46:58 -07:00
}
}
2022-04-25 14:50:10 -07:00
// Check for known struct types we can check for compare results.
case reflect.Struct:
{
// All structs enter here. We're not interested in most types.
if !obj1Value.CanConvert(timeType) {
2022-04-25 14:50:10 -07:00
break
}
// time.Time can be compared!
2022-04-25 14:50:10 -07:00
timeObj1, ok := obj1.(time.Time)
if !ok {
timeObj1 = obj1Value.Convert(timeType).Interface().(time.Time)
}
timeObj2, ok := obj2.(time.Time)
if !ok {
timeObj2 = obj2Value.Convert(timeType).Interface().(time.Time)
}
return compare(timeObj1.UnixNano(), timeObj2.UnixNano(), reflect.Int64)
}
case reflect.Slice:
{
// We only care about the []byte type.
if !obj1Value.CanConvert(bytesType) {
break
}
// []byte can be compared!
bytesObj1, ok := obj1.([]byte)
if !ok {
bytesObj1 = obj1Value.Convert(bytesType).Interface().([]byte)
}
bytesObj2, ok := obj2.([]byte)
if !ok {
bytesObj2 = obj2Value.Convert(bytesType).Interface().([]byte)
}
return CompareType(bytes.Compare(bytesObj1, bytesObj2)), true
}
case reflect.Uintptr:
{
uintptrObj1, ok := obj1.(uintptr)
if !ok {
uintptrObj1 = obj1Value.Convert(uintptrType).Interface().(uintptr)
}
uintptrObj2, ok := obj2.(uintptr)
if !ok {
uintptrObj2 = obj2Value.Convert(uintptrType).Interface().(uintptr)
}
if uintptrObj1 > uintptrObj2 {
return compareGreater, true
}
if uintptrObj1 == uintptrObj2 {
return compareEqual, true
}
if uintptrObj1 < uintptrObj2 {
return compareLess, true
}
}
2019-09-07 13:46:58 -07:00
}
return compareEqual, false
2019-09-07 13:46:58 -07:00
}
// Greater asserts that the first element is greater than the second
//
// assert.Greater(t, 2, 1)
// assert.Greater(t, float64(2), float64(1))
// assert.Greater(t, "b", "a")
2019-09-07 13:46:58 -07:00
func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
2022-04-25 14:50:10 -07:00
if h, ok := t.(tHelper); ok {
h.Helper()
}
return compareTwoValues(t, e1, e2, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs...)
2019-09-07 13:46:58 -07:00
}
// GreaterOrEqual asserts that the first element is greater than or equal to the second
//
// assert.GreaterOrEqual(t, 2, 1)
// assert.GreaterOrEqual(t, 2, 2)
// assert.GreaterOrEqual(t, "b", "a")
// assert.GreaterOrEqual(t, "b", "b")
2019-09-07 13:46:58 -07:00
func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
2022-04-25 14:50:10 -07:00
if h, ok := t.(tHelper); ok {
h.Helper()
}
return compareTwoValues(t, e1, e2, []CompareType{compareGreater, compareEqual}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs...)
2019-09-07 13:46:58 -07:00
}
// Less asserts that the first element is less than the second
//
// assert.Less(t, 1, 2)
// assert.Less(t, float64(1), float64(2))
// assert.Less(t, "a", "b")
2019-09-07 13:46:58 -07:00
func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
2022-04-25 14:50:10 -07:00
if h, ok := t.(tHelper); ok {
h.Helper()
}
return compareTwoValues(t, e1, e2, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs...)
2019-09-07 13:46:58 -07:00
}
// LessOrEqual asserts that the first element is less than or equal to the second
//
// assert.LessOrEqual(t, 1, 2)
// assert.LessOrEqual(t, 2, 2)
// assert.LessOrEqual(t, "a", "b")
// assert.LessOrEqual(t, "b", "b")
2019-09-07 13:46:58 -07:00
func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
2022-04-25 14:50:10 -07:00
if h, ok := t.(tHelper); ok {
h.Helper()
}
return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs...)
}
2021-02-01 12:29:04 -08:00
// Positive asserts that the specified element is positive
//
// assert.Positive(t, 1)
// assert.Positive(t, 1.23)
2021-02-01 12:29:04 -08:00
func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
2022-04-25 14:50:10 -07:00
if h, ok := t.(tHelper); ok {
h.Helper()
}
2021-02-01 12:29:04 -08:00
zero := reflect.Zero(reflect.TypeOf(e))
2022-04-25 14:50:10 -07:00
return compareTwoValues(t, e, zero.Interface(), []CompareType{compareGreater}, "\"%v\" is not positive", msgAndArgs...)
2021-02-01 12:29:04 -08:00
}
// Negative asserts that the specified element is negative
//
// assert.Negative(t, -1)
// assert.Negative(t, -1.23)
2021-02-01 12:29:04 -08:00
func Negative(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
2022-04-25 14:50:10 -07:00
if h, ok := t.(tHelper); ok {
h.Helper()
}
2021-02-01 12:29:04 -08:00
zero := reflect.Zero(reflect.TypeOf(e))
2022-04-25 14:50:10 -07:00
return compareTwoValues(t, e, zero.Interface(), []CompareType{compareLess}, "\"%v\" is not negative", msgAndArgs...)
2021-02-01 12:29:04 -08:00
}
func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool {
2019-09-07 13:46:58 -07:00
if h, ok := t.(tHelper); ok {
h.Helper()
}
e1Kind := reflect.ValueOf(e1).Kind()
e2Kind := reflect.ValueOf(e2).Kind()
if e1Kind != e2Kind {
return Fail(t, "Elements should be the same type", msgAndArgs...)
}
compareResult, isComparable := compare(e1, e2, e1Kind)
2019-09-07 13:46:58 -07:00
if !isComparable {
return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
}
if !containsValue(allowedComparesResults, compareResult) {
return Fail(t, fmt.Sprintf(failMessage, e1, e2), msgAndArgs...)
2019-09-07 13:46:58 -07:00
}
return true
}
func containsValue(values []CompareType, value CompareType) bool {
for _, v := range values {
if v == value {
return true
}
}
return false
}