vendor/github.com/stretchr/testify/assert/assertion_compare.go
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
     1 package assert
     1 package assert
     2 
     2 
     3 import (
     3 import (
       
     4 	"bytes"
     4 	"fmt"
     5 	"fmt"
     5 	"reflect"
     6 	"reflect"
       
     7 	"time"
     6 )
     8 )
     7 
     9 
     8 type CompareType int
    10 type CompareType int
     9 
    11 
    10 const (
    12 const (
    28 
    30 
    29 	float32Type = reflect.TypeOf(float32(1))
    31 	float32Type = reflect.TypeOf(float32(1))
    30 	float64Type = reflect.TypeOf(float64(1))
    32 	float64Type = reflect.TypeOf(float64(1))
    31 
    33 
    32 	stringType = reflect.TypeOf("")
    34 	stringType = reflect.TypeOf("")
       
    35 
       
    36 	timeType  = reflect.TypeOf(time.Time{})
       
    37 	bytesType = reflect.TypeOf([]byte{})
    33 )
    38 )
    34 
    39 
    35 func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
    40 func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
    36 	obj1Value := reflect.ValueOf(obj1)
    41 	obj1Value := reflect.ValueOf(obj1)
    37 	obj2Value := reflect.ValueOf(obj2)
    42 	obj2Value := reflect.ValueOf(obj2)
   297 			}
   302 			}
   298 			if stringobj1 < stringobj2 {
   303 			if stringobj1 < stringobj2 {
   299 				return compareLess, true
   304 				return compareLess, true
   300 			}
   305 			}
   301 		}
   306 		}
       
   307 	// Check for known struct types we can check for compare results.
       
   308 	case reflect.Struct:
       
   309 		{
       
   310 			// All structs enter here. We're not interested in most types.
       
   311 			if !canConvert(obj1Value, timeType) {
       
   312 				break
       
   313 			}
       
   314 
       
   315 			// time.Time can compared!
       
   316 			timeObj1, ok := obj1.(time.Time)
       
   317 			if !ok {
       
   318 				timeObj1 = obj1Value.Convert(timeType).Interface().(time.Time)
       
   319 			}
       
   320 
       
   321 			timeObj2, ok := obj2.(time.Time)
       
   322 			if !ok {
       
   323 				timeObj2 = obj2Value.Convert(timeType).Interface().(time.Time)
       
   324 			}
       
   325 
       
   326 			return compare(timeObj1.UnixNano(), timeObj2.UnixNano(), reflect.Int64)
       
   327 		}
       
   328 	case reflect.Slice:
       
   329 		{
       
   330 			// We only care about the []byte type.
       
   331 			if !canConvert(obj1Value, bytesType) {
       
   332 				break
       
   333 			}
       
   334 
       
   335 			// []byte can be compared!
       
   336 			bytesObj1, ok := obj1.([]byte)
       
   337 			if !ok {
       
   338 				bytesObj1 = obj1Value.Convert(bytesType).Interface().([]byte)
       
   339 
       
   340 			}
       
   341 			bytesObj2, ok := obj2.([]byte)
       
   342 			if !ok {
       
   343 				bytesObj2 = obj2Value.Convert(bytesType).Interface().([]byte)
       
   344 			}
       
   345 
       
   346 			return CompareType(bytes.Compare(bytesObj1, bytesObj2)), true
       
   347 		}
   302 	}
   348 	}
   303 
   349 
   304 	return compareEqual, false
   350 	return compareEqual, false
   305 }
   351 }
   306 
   352 
   308 //
   354 //
   309 //    assert.Greater(t, 2, 1)
   355 //    assert.Greater(t, 2, 1)
   310 //    assert.Greater(t, float64(2), float64(1))
   356 //    assert.Greater(t, float64(2), float64(1))
   311 //    assert.Greater(t, "b", "a")
   357 //    assert.Greater(t, "b", "a")
   312 func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
   358 func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
   313 	return compareTwoValues(t, e1, e2, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs)
   359 	if h, ok := t.(tHelper); ok {
       
   360 		h.Helper()
       
   361 	}
       
   362 	return compareTwoValues(t, e1, e2, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs...)
   314 }
   363 }
   315 
   364 
   316 // GreaterOrEqual asserts that the first element is greater than or equal to the second
   365 // GreaterOrEqual asserts that the first element is greater than or equal to the second
   317 //
   366 //
   318 //    assert.GreaterOrEqual(t, 2, 1)
   367 //    assert.GreaterOrEqual(t, 2, 1)
   319 //    assert.GreaterOrEqual(t, 2, 2)
   368 //    assert.GreaterOrEqual(t, 2, 2)
   320 //    assert.GreaterOrEqual(t, "b", "a")
   369 //    assert.GreaterOrEqual(t, "b", "a")
   321 //    assert.GreaterOrEqual(t, "b", "b")
   370 //    assert.GreaterOrEqual(t, "b", "b")
   322 func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
   371 func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
   323 	return compareTwoValues(t, e1, e2, []CompareType{compareGreater, compareEqual}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs)
   372 	if h, ok := t.(tHelper); ok {
       
   373 		h.Helper()
       
   374 	}
       
   375 	return compareTwoValues(t, e1, e2, []CompareType{compareGreater, compareEqual}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs...)
   324 }
   376 }
   325 
   377 
   326 // Less asserts that the first element is less than the second
   378 // Less asserts that the first element is less than the second
   327 //
   379 //
   328 //    assert.Less(t, 1, 2)
   380 //    assert.Less(t, 1, 2)
   329 //    assert.Less(t, float64(1), float64(2))
   381 //    assert.Less(t, float64(1), float64(2))
   330 //    assert.Less(t, "a", "b")
   382 //    assert.Less(t, "a", "b")
   331 func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
   383 func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
   332 	return compareTwoValues(t, e1, e2, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs)
   384 	if h, ok := t.(tHelper); ok {
       
   385 		h.Helper()
       
   386 	}
       
   387 	return compareTwoValues(t, e1, e2, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs...)
   333 }
   388 }
   334 
   389 
   335 // LessOrEqual asserts that the first element is less than or equal to the second
   390 // LessOrEqual asserts that the first element is less than or equal to the second
   336 //
   391 //
   337 //    assert.LessOrEqual(t, 1, 2)
   392 //    assert.LessOrEqual(t, 1, 2)
   338 //    assert.LessOrEqual(t, 2, 2)
   393 //    assert.LessOrEqual(t, 2, 2)
   339 //    assert.LessOrEqual(t, "a", "b")
   394 //    assert.LessOrEqual(t, "a", "b")
   340 //    assert.LessOrEqual(t, "b", "b")
   395 //    assert.LessOrEqual(t, "b", "b")
   341 func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
   396 func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
   342 	return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs)
   397 	if h, ok := t.(tHelper); ok {
       
   398 		h.Helper()
       
   399 	}
       
   400 	return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs...)
   343 }
   401 }
   344 
   402 
   345 // Positive asserts that the specified element is positive
   403 // Positive asserts that the specified element is positive
   346 //
   404 //
   347 //    assert.Positive(t, 1)
   405 //    assert.Positive(t, 1)
   348 //    assert.Positive(t, 1.23)
   406 //    assert.Positive(t, 1.23)
   349 func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
   407 func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
       
   408 	if h, ok := t.(tHelper); ok {
       
   409 		h.Helper()
       
   410 	}
   350 	zero := reflect.Zero(reflect.TypeOf(e))
   411 	zero := reflect.Zero(reflect.TypeOf(e))
   351 	return compareTwoValues(t, e, zero.Interface(), []CompareType{compareGreater}, "\"%v\" is not positive", msgAndArgs)
   412 	return compareTwoValues(t, e, zero.Interface(), []CompareType{compareGreater}, "\"%v\" is not positive", msgAndArgs...)
   352 }
   413 }
   353 
   414 
   354 // Negative asserts that the specified element is negative
   415 // Negative asserts that the specified element is negative
   355 //
   416 //
   356 //    assert.Negative(t, -1)
   417 //    assert.Negative(t, -1)
   357 //    assert.Negative(t, -1.23)
   418 //    assert.Negative(t, -1.23)
   358 func Negative(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
   419 func Negative(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
       
   420 	if h, ok := t.(tHelper); ok {
       
   421 		h.Helper()
       
   422 	}
   359 	zero := reflect.Zero(reflect.TypeOf(e))
   423 	zero := reflect.Zero(reflect.TypeOf(e))
   360 	return compareTwoValues(t, e, zero.Interface(), []CompareType{compareLess}, "\"%v\" is not negative", msgAndArgs)
   424 	return compareTwoValues(t, e, zero.Interface(), []CompareType{compareLess}, "\"%v\" is not negative", msgAndArgs...)
   361 }
   425 }
   362 
   426 
   363 func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool {
   427 func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool {
   364 	if h, ok := t.(tHelper); ok {
   428 	if h, ok := t.(tHelper); ok {
   365 		h.Helper()
   429 		h.Helper()