diff --git a/assert/assertion_order.go b/assert/assertion_order.go index 15a486c..3c73da5 100644 --- a/assert/assertion_order.go +++ b/assert/assertion_order.go @@ -5,6 +5,12 @@ import ( "reflect" ) +const( + greater = 1 + equal = 0 + less = -1 +) + func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { switch kind { case reflect.Int: @@ -12,13 +18,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { intobj1 := obj1.(int) intobj2 := obj2.(int) if intobj1 > intobj2 { - return -1, true + return greater, true } if intobj1 == intobj2 { - return 0, true + return equal, true } if intobj1 < intobj2 { - return 1, true + return less, true } } case reflect.Int8: @@ -26,13 +32,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { int8obj1 := obj1.(int8) int8obj2 := obj2.(int8) if int8obj1 > int8obj2 { - return -1, true + return greater, true } if int8obj1 == int8obj2 { - return 0, true + return equal, true } if int8obj1 < int8obj2 { - return 1, true + return less, true } } case reflect.Int16: @@ -40,13 +46,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { int16obj1 := obj1.(int16) int16obj2 := obj2.(int16) if int16obj1 > int16obj2 { - return -1, true + return greater, true } if int16obj1 == int16obj2 { - return 0, true + return equal, true } if int16obj1 < int16obj2 { - return 1, true + return less, true } } case reflect.Int32: @@ -54,13 +60,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { int32obj1 := obj1.(int32) int32obj2 := obj2.(int32) if int32obj1 > int32obj2 { - return -1, true + return greater, true } if int32obj1 == int32obj2 { - return 0, true + return equal, true } if int32obj1 < int32obj2 { - return 1, true + return less, true } } case reflect.Int64: @@ -68,13 +74,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { int64obj1 := obj1.(int64) int64obj2 := obj2.(int64) if int64obj1 > int64obj2 { - return -1, true + return greater, true } if int64obj1 == int64obj2 { - return 0, true + return equal, true } if int64obj1 < int64obj2 { - return 1, true + return less, true } } case reflect.Uint: @@ -82,13 +88,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { uintobj1 := obj1.(uint) uintobj2 := obj2.(uint) if uintobj1 > uintobj2 { - return -1, true + return greater, true } if uintobj1 == uintobj2 { - return 0, true + return equal, true } if uintobj1 < uintobj2 { - return 1, true + return less, true } } case reflect.Uint8: @@ -96,13 +102,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { uint8obj1 := obj1.(uint8) uint8obj2 := obj2.(uint8) if uint8obj1 > uint8obj2 { - return -1, true + return greater, true } if uint8obj1 == uint8obj2 { - return 0, true + return equal, true } if uint8obj1 < uint8obj2 { - return 1, true + return less, true } } case reflect.Uint16: @@ -110,13 +116,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { uint16obj1 := obj1.(uint16) uint16obj2 := obj2.(uint16) if uint16obj1 > uint16obj2 { - return -1, true + return greater, true } if uint16obj1 == uint16obj2 { - return 0, true + return equal, true } if uint16obj1 < uint16obj2 { - return 1, true + return less, true } } case reflect.Uint32: @@ -124,13 +130,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { uint32obj1 := obj1.(uint32) uint32obj2 := obj2.(uint32) if uint32obj1 > uint32obj2 { - return -1, true + return greater, true } if uint32obj1 == uint32obj2 { - return 0, true + return equal, true } if uint32obj1 < uint32obj2 { - return 1, true + return less, true } } case reflect.Uint64: @@ -138,13 +144,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { uint64obj1 := obj1.(uint64) uint64obj2 := obj2.(uint64) if uint64obj1 > uint64obj2 { - return -1, true + return greater, true } if uint64obj1 == uint64obj2 { - return 0, true + return equal, true } if uint64obj1 < uint64obj2 { - return 1, true + return less, true } } case reflect.Float32: @@ -152,13 +158,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { float32obj1 := obj1.(float32) float32obj2 := obj2.(float32) if float32obj1 > float32obj2 { - return -1, true + return greater, true } if float32obj1 == float32obj2 { - return 0, true + return equal, true } if float32obj1 < float32obj2 { - return 1, true + return less, true } } case reflect.Float64: @@ -166,13 +172,13 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { float64obj1 := obj1.(float64) float64obj2 := obj2.(float64) if float64obj1 > float64obj2 { - return -1, true + return greater, true } if float64obj1 == float64obj2 { - return 0, true + return equal, true } if float64obj1 < float64obj2 { - return 1, true + return less, true } } case reflect.String: @@ -180,18 +186,18 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { stringobj1 := obj1.(string) stringobj2 := obj2.(string) if stringobj1 > stringobj2 { - return -1, true + return greater, true } if stringobj1 == stringobj2 { - return 0, true + return equal, true } if stringobj1 < stringobj2 { - return 1, true + return less, true } } } - return 0, false + return equal, false } // Greater asserts that the first element is greater than the second @@ -200,26 +206,7 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) { // assert.Greater(t, float64(2), float64(1)) // assert.Greater(t, "b", "a") func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { - 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...) - } - - res, isComparable := compare(e1, e2, e1Kind) - if !isComparable { - return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...) - } - - if res != -1 { - return Fail(t, fmt.Sprintf("\"%v\" is not greater than \"%v\"", e1, e2), msgAndArgs...) - } - - return true + return compareTwoValues(t, e1, e2, []int {greater}, "\"%s\" is not greater than \"%s\"", msgAndArgs); } // GreaterOrEqual asserts that the first element is greater than or equal to the second @@ -229,26 +216,7 @@ func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface // assert.GreaterOrEqual(t, "b", "a") // assert.GreaterOrEqual(t, "b", "b") func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { - 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...) - } - - res, isComparable := compare(e1, e2, e1Kind) - if !isComparable { - return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...) - } - - if res != -1 && res != 0 { - return Fail(t, fmt.Sprintf("\"%v\" is not greater than or equal to \"%v\"", e1, e2), msgAndArgs...) - } - - return true + return compareTwoValues(t, e1, e2, []int {greater, equal}, "\"%s\" is not greater or equal than \"%s\"", msgAndArgs); } // Less asserts that the first element is less than the second @@ -257,26 +225,7 @@ func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...in // assert.Less(t, float64(1), float64(2)) // assert.Less(t, "a", "b") func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { - 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...) - } - - res, isComparable := compare(e1, e2, e1Kind) - if !isComparable { - return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...) - } - - if res != 1 { - return Fail(t, fmt.Sprintf("\"%v\" is not less than \"%v\"", e1, e2), msgAndArgs...) - } - - return true + return compareTwoValues(t, e1, e2, []int {less}, "\"%s\" is not less than \"%s\"", msgAndArgs); } // LessOrEqual asserts that the first element is less than or equal to the second @@ -286,6 +235,11 @@ func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) // assert.LessOrEqual(t, "a", "b") // assert.LessOrEqual(t, "b", "b") 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); +} + + +func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []int, failMessage string, msgAndArgs ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() } @@ -296,14 +250,25 @@ func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...inter return Fail(t, "Elements should be the same type", msgAndArgs...) } - res, isComparable := compare(e1, e2, e1Kind) + compareResult, isComparable := compare(e1, e2, e1Kind) if !isComparable { return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...) } - if res != 1 && res != 0 { - return Fail(t, fmt.Sprintf("\"%v\" is not less than or equal to \"%v\"", e1, e2), msgAndArgs...) + if !containsValue(allowedComparesResults, compareResult) { + return Fail(t, fmt.Sprintf(failMessage, e1, e2), msgAndArgs...) } return true } + + +func containsValue(values []int, value int) bool{ + for _, v := range values { + if (v == value) { + return true + } + } + + return false +} \ No newline at end of file diff --git a/assert/assertion_order_test.go b/assert/assertion_order_test.go index 4dd897a..3ee352a 100644 --- a/assert/assertion_order_test.go +++ b/assert/assertion_order_test.go @@ -29,7 +29,7 @@ func TestCompare(t *testing.T) { t.Error("object should be comparable for type " + currCase.cType) } - if resLess != 1 { + if resLess != -1 { t.Errorf("object less should be less than greater for type " + currCase.cType) } @@ -38,7 +38,7 @@ func TestCompare(t *testing.T) { t.Error("object are comparable for type " + currCase.cType) } - if resGreater != -1 { + if resGreater != 1 { t.Errorf("object greater should be greater than less for type " + currCase.cType) }