mirror of
https://github.com/stretchr/testify.git
synced 2025-04-27 13:13:19 +00:00
update assertion_order.go according review notes
This commit is contained in:
parent
ad53dbbf0a
commit
0b4ff03cda
@ -5,26 +5,28 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type CompareType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
greater = 1
|
compareLess CompareType = iota - 1
|
||||||
equal = 0
|
compareEqual
|
||||||
less = -1
|
compareGreater
|
||||||
)
|
)
|
||||||
|
|
||||||
func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
|
||||||
switch kind {
|
switch kind {
|
||||||
case reflect.Int:
|
case reflect.Int:
|
||||||
{
|
{
|
||||||
intobj1 := obj1.(int)
|
intobj1 := obj1.(int)
|
||||||
intobj2 := obj2.(int)
|
intobj2 := obj2.(int)
|
||||||
if intobj1 > intobj2 {
|
if intobj1 > intobj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if intobj1 == intobj2 {
|
if intobj1 == intobj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if intobj1 < intobj2 {
|
if intobj1 < intobj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Int8:
|
case reflect.Int8:
|
||||||
@ -32,13 +34,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
int8obj1 := obj1.(int8)
|
int8obj1 := obj1.(int8)
|
||||||
int8obj2 := obj2.(int8)
|
int8obj2 := obj2.(int8)
|
||||||
if int8obj1 > int8obj2 {
|
if int8obj1 > int8obj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if int8obj1 == int8obj2 {
|
if int8obj1 == int8obj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if int8obj1 < int8obj2 {
|
if int8obj1 < int8obj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Int16:
|
case reflect.Int16:
|
||||||
@ -46,13 +48,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
int16obj1 := obj1.(int16)
|
int16obj1 := obj1.(int16)
|
||||||
int16obj2 := obj2.(int16)
|
int16obj2 := obj2.(int16)
|
||||||
if int16obj1 > int16obj2 {
|
if int16obj1 > int16obj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if int16obj1 == int16obj2 {
|
if int16obj1 == int16obj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if int16obj1 < int16obj2 {
|
if int16obj1 < int16obj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Int32:
|
case reflect.Int32:
|
||||||
@ -60,13 +62,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
int32obj1 := obj1.(int32)
|
int32obj1 := obj1.(int32)
|
||||||
int32obj2 := obj2.(int32)
|
int32obj2 := obj2.(int32)
|
||||||
if int32obj1 > int32obj2 {
|
if int32obj1 > int32obj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if int32obj1 == int32obj2 {
|
if int32obj1 == int32obj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if int32obj1 < int32obj2 {
|
if int32obj1 < int32obj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Int64:
|
case reflect.Int64:
|
||||||
@ -74,13 +76,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
int64obj1 := obj1.(int64)
|
int64obj1 := obj1.(int64)
|
||||||
int64obj2 := obj2.(int64)
|
int64obj2 := obj2.(int64)
|
||||||
if int64obj1 > int64obj2 {
|
if int64obj1 > int64obj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if int64obj1 == int64obj2 {
|
if int64obj1 == int64obj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if int64obj1 < int64obj2 {
|
if int64obj1 < int64obj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Uint:
|
case reflect.Uint:
|
||||||
@ -88,13 +90,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
uintobj1 := obj1.(uint)
|
uintobj1 := obj1.(uint)
|
||||||
uintobj2 := obj2.(uint)
|
uintobj2 := obj2.(uint)
|
||||||
if uintobj1 > uintobj2 {
|
if uintobj1 > uintobj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if uintobj1 == uintobj2 {
|
if uintobj1 == uintobj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if uintobj1 < uintobj2 {
|
if uintobj1 < uintobj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Uint8:
|
case reflect.Uint8:
|
||||||
@ -102,13 +104,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
uint8obj1 := obj1.(uint8)
|
uint8obj1 := obj1.(uint8)
|
||||||
uint8obj2 := obj2.(uint8)
|
uint8obj2 := obj2.(uint8)
|
||||||
if uint8obj1 > uint8obj2 {
|
if uint8obj1 > uint8obj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if uint8obj1 == uint8obj2 {
|
if uint8obj1 == uint8obj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if uint8obj1 < uint8obj2 {
|
if uint8obj1 < uint8obj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Uint16:
|
case reflect.Uint16:
|
||||||
@ -116,13 +118,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
uint16obj1 := obj1.(uint16)
|
uint16obj1 := obj1.(uint16)
|
||||||
uint16obj2 := obj2.(uint16)
|
uint16obj2 := obj2.(uint16)
|
||||||
if uint16obj1 > uint16obj2 {
|
if uint16obj1 > uint16obj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if uint16obj1 == uint16obj2 {
|
if uint16obj1 == uint16obj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if uint16obj1 < uint16obj2 {
|
if uint16obj1 < uint16obj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Uint32:
|
case reflect.Uint32:
|
||||||
@ -130,13 +132,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
uint32obj1 := obj1.(uint32)
|
uint32obj1 := obj1.(uint32)
|
||||||
uint32obj2 := obj2.(uint32)
|
uint32obj2 := obj2.(uint32)
|
||||||
if uint32obj1 > uint32obj2 {
|
if uint32obj1 > uint32obj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if uint32obj1 == uint32obj2 {
|
if uint32obj1 == uint32obj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if uint32obj1 < uint32obj2 {
|
if uint32obj1 < uint32obj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Uint64:
|
case reflect.Uint64:
|
||||||
@ -144,13 +146,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
uint64obj1 := obj1.(uint64)
|
uint64obj1 := obj1.(uint64)
|
||||||
uint64obj2 := obj2.(uint64)
|
uint64obj2 := obj2.(uint64)
|
||||||
if uint64obj1 > uint64obj2 {
|
if uint64obj1 > uint64obj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if uint64obj1 == uint64obj2 {
|
if uint64obj1 == uint64obj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if uint64obj1 < uint64obj2 {
|
if uint64obj1 < uint64obj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Float32:
|
case reflect.Float32:
|
||||||
@ -158,13 +160,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
float32obj1 := obj1.(float32)
|
float32obj1 := obj1.(float32)
|
||||||
float32obj2 := obj2.(float32)
|
float32obj2 := obj2.(float32)
|
||||||
if float32obj1 > float32obj2 {
|
if float32obj1 > float32obj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if float32obj1 == float32obj2 {
|
if float32obj1 == float32obj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if float32obj1 < float32obj2 {
|
if float32obj1 < float32obj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Float64:
|
case reflect.Float64:
|
||||||
@ -172,13 +174,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
float64obj1 := obj1.(float64)
|
float64obj1 := obj1.(float64)
|
||||||
float64obj2 := obj2.(float64)
|
float64obj2 := obj2.(float64)
|
||||||
if float64obj1 > float64obj2 {
|
if float64obj1 > float64obj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if float64obj1 == float64obj2 {
|
if float64obj1 == float64obj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if float64obj1 < float64obj2 {
|
if float64obj1 < float64obj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
@ -186,18 +188,18 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
stringobj1 := obj1.(string)
|
stringobj1 := obj1.(string)
|
||||||
stringobj2 := obj2.(string)
|
stringobj2 := obj2.(string)
|
||||||
if stringobj1 > stringobj2 {
|
if stringobj1 > stringobj2 {
|
||||||
return greater, true
|
return compareGreater, true
|
||||||
}
|
}
|
||||||
if stringobj1 == stringobj2 {
|
if stringobj1 == stringobj2 {
|
||||||
return equal, true
|
return compareEqual, true
|
||||||
}
|
}
|
||||||
if stringobj1 < stringobj2 {
|
if stringobj1 < stringobj2 {
|
||||||
return less, true
|
return compareLess, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return equal, false
|
return compareEqual, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Greater asserts that the first element is greater than the second
|
// Greater asserts that the first element is greater than the second
|
||||||
@ -206,7 +208,7 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
|||||||
// assert.Greater(t, float64(2), float64(1))
|
// assert.Greater(t, float64(2), float64(1))
|
||||||
// assert.Greater(t, "b", "a")
|
// assert.Greater(t, "b", "a")
|
||||||
func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
||||||
return compareTwoValues(t, e1, e2, []int{greater}, "\"%s\" is not greater than \"%s\"", msgAndArgs)
|
return compareTwoValues(t, e1, e2, []CompareType{compareGreater}, "\"%s\" is not greater than \"%s\"", msgAndArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GreaterOrEqual asserts that the first element is greater than or equal to the second
|
// GreaterOrEqual asserts that the first element is greater than or equal to the second
|
||||||
@ -216,7 +218,7 @@ func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface
|
|||||||
// assert.GreaterOrEqual(t, "b", "a")
|
// assert.GreaterOrEqual(t, "b", "a")
|
||||||
// assert.GreaterOrEqual(t, "b", "b")
|
// assert.GreaterOrEqual(t, "b", "b")
|
||||||
func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
||||||
return compareTwoValues(t, e1, e2, []int{greater, equal}, "\"%s\" is not greater or equal than \"%s\"", msgAndArgs)
|
return compareTwoValues(t, e1, e2, []CompareType{compareGreater, compareEqual}, "\"%s\" is not greater or equal than \"%s\"", msgAndArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Less asserts that the first element is less than the second
|
// Less asserts that the first element is less than the second
|
||||||
@ -225,7 +227,7 @@ func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...in
|
|||||||
// assert.Less(t, float64(1), float64(2))
|
// assert.Less(t, float64(1), float64(2))
|
||||||
// assert.Less(t, "a", "b")
|
// assert.Less(t, "a", "b")
|
||||||
func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
||||||
return compareTwoValues(t, e1, e2, []int{less}, "\"%s\" is not less than \"%s\"", msgAndArgs)
|
return compareTwoValues(t, e1, e2, []CompareType{compareLess}, "\"%s\" is not less than \"%s\"", msgAndArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LessOrEqual asserts that the first element is less than or equal to the second
|
// LessOrEqual asserts that the first element is less than or equal to the second
|
||||||
@ -235,10 +237,10 @@ func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{})
|
|||||||
// assert.LessOrEqual(t, "a", "b")
|
// assert.LessOrEqual(t, "a", "b")
|
||||||
// assert.LessOrEqual(t, "b", "b")
|
// assert.LessOrEqual(t, "b", "b")
|
||||||
func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
||||||
return compareTwoValues(t, e1, e2, []int{less, equal}, "\"%s\" is not less or equal than \"%s\"", msgAndArgs)
|
return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%s\" is not less or equal than \"%s\"", msgAndArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []int, failMessage string, msgAndArgs ...interface{}) bool {
|
func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool {
|
||||||
if h, ok := t.(tHelper); ok {
|
if h, ok := t.(tHelper); ok {
|
||||||
h.Helper()
|
h.Helper()
|
||||||
}
|
}
|
||||||
@ -261,7 +263,7 @@ func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedCompare
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func containsValue(values []int, value int) bool {
|
func containsValue(values []CompareType, value CompareType) bool {
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
if v == value {
|
if v == value {
|
||||||
return true
|
return true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user