assertion_order.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package assert
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. // isOrdered checks that collection contains orderable elements.
  7. func isOrdered(t TestingT, object interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool {
  8. objKind := reflect.TypeOf(object).Kind()
  9. if objKind != reflect.Slice && objKind != reflect.Array {
  10. return false
  11. }
  12. objValue := reflect.ValueOf(object)
  13. objLen := objValue.Len()
  14. if objLen <= 1 {
  15. return true
  16. }
  17. value := objValue.Index(0)
  18. valueInterface := value.Interface()
  19. firstValueKind := value.Kind()
  20. for i := 1; i < objLen; i++ {
  21. prevValue := value
  22. prevValueInterface := valueInterface
  23. value = objValue.Index(i)
  24. valueInterface = value.Interface()
  25. compareResult, isComparable := compare(prevValueInterface, valueInterface, firstValueKind)
  26. if !isComparable {
  27. return Fail(t, fmt.Sprintf("Can not compare type \"%s\" and \"%s\"", reflect.TypeOf(value), reflect.TypeOf(prevValue)), msgAndArgs...)
  28. }
  29. if !containsValue(allowedComparesResults, compareResult) {
  30. return Fail(t, fmt.Sprintf(failMessage, prevValue, value), msgAndArgs...)
  31. }
  32. }
  33. return true
  34. }
  35. // IsIncreasing asserts that the collection is increasing
  36. //
  37. // assert.IsIncreasing(t, []int{1, 2, 3})
  38. // assert.IsIncreasing(t, []float{1, 2})
  39. // assert.IsIncreasing(t, []string{"a", "b"})
  40. func IsIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
  41. return isOrdered(t, object, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs...)
  42. }
  43. // IsNonIncreasing asserts that the collection is not increasing
  44. //
  45. // assert.IsNonIncreasing(t, []int{2, 1, 1})
  46. // assert.IsNonIncreasing(t, []float{2, 1})
  47. // assert.IsNonIncreasing(t, []string{"b", "a"})
  48. func IsNonIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
  49. return isOrdered(t, object, []CompareType{compareEqual, compareGreater}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs...)
  50. }
  51. // IsDecreasing asserts that the collection is decreasing
  52. //
  53. // assert.IsDecreasing(t, []int{2, 1, 0})
  54. // assert.IsDecreasing(t, []float{2, 1})
  55. // assert.IsDecreasing(t, []string{"b", "a"})
  56. func IsDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
  57. return isOrdered(t, object, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs...)
  58. }
  59. // IsNonDecreasing asserts that the collection is not decreasing
  60. //
  61. // assert.IsNonDecreasing(t, []int{1, 1, 2})
  62. // assert.IsNonDecreasing(t, []float{1, 2})
  63. // assert.IsNonDecreasing(t, []string{"a", "b"})
  64. func IsNonDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
  65. return isOrdered(t, object, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs...)
  66. }