blob: 1c3b47182a726afbfb1890c5119144bad1bcf8c9 [file] [log] [blame]
Scott Baker8461e152019-10-01 14:44:30 -07001package assert
2
3import (
4 "fmt"
5 "reflect"
6)
7
khenaidood948f772021-08-11 17:49:24 -04008// isOrdered checks that collection contains orderable elements.
9func isOrdered(t TestingT, object interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool {
10 objKind := reflect.TypeOf(object).Kind()
11 if objKind != reflect.Slice && objKind != reflect.Array {
12 return false
Scott Baker8461e152019-10-01 14:44:30 -070013 }
14
khenaidood948f772021-08-11 17:49:24 -040015 objValue := reflect.ValueOf(object)
16 objLen := objValue.Len()
Scott Baker8461e152019-10-01 14:44:30 -070017
khenaidood948f772021-08-11 17:49:24 -040018 if objLen <= 1 {
19 return true
Scott Baker8461e152019-10-01 14:44:30 -070020 }
21
khenaidood948f772021-08-11 17:49:24 -040022 value := objValue.Index(0)
23 valueInterface := value.Interface()
24 firstValueKind := value.Kind()
Scott Baker8461e152019-10-01 14:44:30 -070025
khenaidood948f772021-08-11 17:49:24 -040026 for i := 1; i < objLen; i++ {
27 prevValue := value
28 prevValueInterface := valueInterface
Scott Baker8461e152019-10-01 14:44:30 -070029
khenaidood948f772021-08-11 17:49:24 -040030 value = objValue.Index(i)
31 valueInterface = value.Interface()
32
33 compareResult, isComparable := compare(prevValueInterface, valueInterface, firstValueKind)
34
35 if !isComparable {
36 return Fail(t, fmt.Sprintf("Can not compare type \"%s\" and \"%s\"", reflect.TypeOf(value), reflect.TypeOf(prevValue)), msgAndArgs...)
37 }
38
39 if !containsValue(allowedComparesResults, compareResult) {
40 return Fail(t, fmt.Sprintf(failMessage, prevValue, value), msgAndArgs...)
41 }
Scott Baker8461e152019-10-01 14:44:30 -070042 }
43
44 return true
45}
46
khenaidood948f772021-08-11 17:49:24 -040047// IsIncreasing asserts that the collection is increasing
Scott Baker8461e152019-10-01 14:44:30 -070048//
khenaidood948f772021-08-11 17:49:24 -040049// assert.IsIncreasing(t, []int{1, 2, 3})
50// assert.IsIncreasing(t, []float{1, 2})
51// assert.IsIncreasing(t, []string{"a", "b"})
52func IsIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
53 return isOrdered(t, object, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs)
Scott Baker8461e152019-10-01 14:44:30 -070054}
55
khenaidood948f772021-08-11 17:49:24 -040056// IsNonIncreasing asserts that the collection is not increasing
Scott Baker8461e152019-10-01 14:44:30 -070057//
khenaidood948f772021-08-11 17:49:24 -040058// assert.IsNonIncreasing(t, []int{2, 1, 1})
59// assert.IsNonIncreasing(t, []float{2, 1})
60// assert.IsNonIncreasing(t, []string{"b", "a"})
61func IsNonIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
62 return isOrdered(t, object, []CompareType{compareEqual, compareGreater}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs)
Scott Baker8461e152019-10-01 14:44:30 -070063}
64
khenaidood948f772021-08-11 17:49:24 -040065// IsDecreasing asserts that the collection is decreasing
Scott Baker8461e152019-10-01 14:44:30 -070066//
khenaidood948f772021-08-11 17:49:24 -040067// assert.IsDecreasing(t, []int{2, 1, 0})
68// assert.IsDecreasing(t, []float{2, 1})
69// assert.IsDecreasing(t, []string{"b", "a"})
70func IsDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
71 return isOrdered(t, object, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs)
72}
Scott Baker8461e152019-10-01 14:44:30 -070073
khenaidood948f772021-08-11 17:49:24 -040074// IsNonDecreasing asserts that the collection is not decreasing
75//
76// assert.IsNonDecreasing(t, []int{1, 1, 2})
77// assert.IsNonDecreasing(t, []float{1, 2})
78// assert.IsNonDecreasing(t, []string{"a", "b"})
79func IsNonDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
80 return isOrdered(t, object, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs)
Scott Baker8461e152019-10-01 14:44:30 -070081}