David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 1 | package assert |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | ) |
| 7 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 8 | // isOrdered checks that collection contains orderable elements. |
| 9 | func 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 |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 13 | } |
| 14 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 15 | objValue := reflect.ValueOf(object) |
| 16 | objLen := objValue.Len() |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 17 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 18 | if objLen <= 1 { |
| 19 | return true |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 20 | } |
| 21 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 22 | value := objValue.Index(0) |
| 23 | valueInterface := value.Interface() |
| 24 | firstValueKind := value.Kind() |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 25 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 26 | for i := 1; i < objLen; i++ { |
| 27 | prevValue := value |
| 28 | prevValueInterface := valueInterface |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 29 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 30 | 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 | } |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | return true |
| 45 | } |
| 46 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 47 | // IsIncreasing asserts that the collection is increasing |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 48 | // |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 49 | // assert.IsIncreasing(t, []int{1, 2, 3}) |
| 50 | // assert.IsIncreasing(t, []float{1, 2}) |
| 51 | // assert.IsIncreasing(t, []string{"a", "b"}) |
| 52 | func IsIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { |
| 53 | return isOrdered(t, object, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs) |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 54 | } |
| 55 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 56 | // IsNonIncreasing asserts that the collection is not increasing |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 57 | // |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 58 | // assert.IsNonIncreasing(t, []int{2, 1, 1}) |
| 59 | // assert.IsNonIncreasing(t, []float{2, 1}) |
| 60 | // assert.IsNonIncreasing(t, []string{"b", "a"}) |
| 61 | func 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) |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 63 | } |
| 64 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 65 | // IsDecreasing asserts that the collection is decreasing |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 66 | // |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 67 | // assert.IsDecreasing(t, []int{2, 1, 0}) |
| 68 | // assert.IsDecreasing(t, []float{2, 1}) |
| 69 | // assert.IsDecreasing(t, []string{"b", "a"}) |
| 70 | func IsDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { |
| 71 | return isOrdered(t, object, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs) |
| 72 | } |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 73 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 74 | // 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"}) |
| 79 | func 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) |
David Bainbridge | c4029aa | 2019-09-26 18:56:39 +0000 | [diff] [blame] | 81 | } |