Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 1 | package assert |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | ) |
| 7 | |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 8 | type CompareType int |
| 9 | |
| 10 | const ( |
| 11 | compareLess CompareType = iota - 1 |
| 12 | compareEqual |
| 13 | compareGreater |
| 14 | ) |
| 15 | |
| 16 | func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) { |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 17 | switch kind { |
| 18 | case reflect.Int: |
| 19 | { |
| 20 | intobj1 := obj1.(int) |
| 21 | intobj2 := obj2.(int) |
| 22 | if intobj1 > intobj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 23 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 24 | } |
| 25 | if intobj1 == intobj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 26 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 27 | } |
| 28 | if intobj1 < intobj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 29 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | case reflect.Int8: |
| 33 | { |
| 34 | int8obj1 := obj1.(int8) |
| 35 | int8obj2 := obj2.(int8) |
| 36 | if int8obj1 > int8obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 37 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 38 | } |
| 39 | if int8obj1 == int8obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 40 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 41 | } |
| 42 | if int8obj1 < int8obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 43 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | case reflect.Int16: |
| 47 | { |
| 48 | int16obj1 := obj1.(int16) |
| 49 | int16obj2 := obj2.(int16) |
| 50 | if int16obj1 > int16obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 51 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 52 | } |
| 53 | if int16obj1 == int16obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 54 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 55 | } |
| 56 | if int16obj1 < int16obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 57 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | case reflect.Int32: |
| 61 | { |
| 62 | int32obj1 := obj1.(int32) |
| 63 | int32obj2 := obj2.(int32) |
| 64 | if int32obj1 > int32obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 65 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 66 | } |
| 67 | if int32obj1 == int32obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 68 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 69 | } |
| 70 | if int32obj1 < int32obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 71 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | case reflect.Int64: |
| 75 | { |
| 76 | int64obj1 := obj1.(int64) |
| 77 | int64obj2 := obj2.(int64) |
| 78 | if int64obj1 > int64obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 79 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 80 | } |
| 81 | if int64obj1 == int64obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 82 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 83 | } |
| 84 | if int64obj1 < int64obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 85 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | case reflect.Uint: |
| 89 | { |
| 90 | uintobj1 := obj1.(uint) |
| 91 | uintobj2 := obj2.(uint) |
| 92 | if uintobj1 > uintobj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 93 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 94 | } |
| 95 | if uintobj1 == uintobj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 96 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 97 | } |
| 98 | if uintobj1 < uintobj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 99 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | case reflect.Uint8: |
| 103 | { |
| 104 | uint8obj1 := obj1.(uint8) |
| 105 | uint8obj2 := obj2.(uint8) |
| 106 | if uint8obj1 > uint8obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 107 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 108 | } |
| 109 | if uint8obj1 == uint8obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 110 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 111 | } |
| 112 | if uint8obj1 < uint8obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 113 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | case reflect.Uint16: |
| 117 | { |
| 118 | uint16obj1 := obj1.(uint16) |
| 119 | uint16obj2 := obj2.(uint16) |
| 120 | if uint16obj1 > uint16obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 121 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 122 | } |
| 123 | if uint16obj1 == uint16obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 124 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 125 | } |
| 126 | if uint16obj1 < uint16obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 127 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | case reflect.Uint32: |
| 131 | { |
| 132 | uint32obj1 := obj1.(uint32) |
| 133 | uint32obj2 := obj2.(uint32) |
| 134 | if uint32obj1 > uint32obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 135 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 136 | } |
| 137 | if uint32obj1 == uint32obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 138 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 139 | } |
| 140 | if uint32obj1 < uint32obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 141 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | case reflect.Uint64: |
| 145 | { |
| 146 | uint64obj1 := obj1.(uint64) |
| 147 | uint64obj2 := obj2.(uint64) |
| 148 | if uint64obj1 > uint64obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 149 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 150 | } |
| 151 | if uint64obj1 == uint64obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 152 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 153 | } |
| 154 | if uint64obj1 < uint64obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 155 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | case reflect.Float32: |
| 159 | { |
| 160 | float32obj1 := obj1.(float32) |
| 161 | float32obj2 := obj2.(float32) |
| 162 | if float32obj1 > float32obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 163 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 164 | } |
| 165 | if float32obj1 == float32obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 166 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 167 | } |
| 168 | if float32obj1 < float32obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 169 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | case reflect.Float64: |
| 173 | { |
| 174 | float64obj1 := obj1.(float64) |
| 175 | float64obj2 := obj2.(float64) |
| 176 | if float64obj1 > float64obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 177 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 178 | } |
| 179 | if float64obj1 == float64obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 180 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 181 | } |
| 182 | if float64obj1 < float64obj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 183 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | case reflect.String: |
| 187 | { |
| 188 | stringobj1 := obj1.(string) |
| 189 | stringobj2 := obj2.(string) |
| 190 | if stringobj1 > stringobj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 191 | return compareGreater, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 192 | } |
| 193 | if stringobj1 == stringobj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 194 | return compareEqual, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 195 | } |
| 196 | if stringobj1 < stringobj2 { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 197 | return compareLess, true |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 202 | return compareEqual, false |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // Greater asserts that the first element is greater than the second |
| 206 | // |
| 207 | // assert.Greater(t, 2, 1) |
| 208 | // assert.Greater(t, float64(2), float64(1)) |
| 209 | // assert.Greater(t, "b", "a") |
| 210 | func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 211 | return compareTwoValues(t, e1, e2, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs) |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // GreaterOrEqual asserts that the first element is greater than or equal to the second |
| 215 | // |
| 216 | // assert.GreaterOrEqual(t, 2, 1) |
| 217 | // assert.GreaterOrEqual(t, 2, 2) |
| 218 | // assert.GreaterOrEqual(t, "b", "a") |
| 219 | // assert.GreaterOrEqual(t, "b", "b") |
| 220 | func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 221 | return compareTwoValues(t, e1, e2, []CompareType{compareGreater, compareEqual}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs) |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // Less asserts that the first element is less than the second |
| 225 | // |
| 226 | // assert.Less(t, 1, 2) |
| 227 | // assert.Less(t, float64(1), float64(2)) |
| 228 | // assert.Less(t, "a", "b") |
| 229 | func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 230 | return compareTwoValues(t, e1, e2, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs) |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // LessOrEqual asserts that the first element is less than or equal to the second |
| 234 | // |
| 235 | // assert.LessOrEqual(t, 1, 2) |
| 236 | // assert.LessOrEqual(t, 2, 2) |
| 237 | // assert.LessOrEqual(t, "a", "b") |
| 238 | // assert.LessOrEqual(t, "b", "b") |
| 239 | func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 240 | return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs) |
| 241 | } |
| 242 | |
| 243 | func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool { |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 244 | if h, ok := t.(tHelper); ok { |
| 245 | h.Helper() |
| 246 | } |
| 247 | |
| 248 | e1Kind := reflect.ValueOf(e1).Kind() |
| 249 | e2Kind := reflect.ValueOf(e2).Kind() |
| 250 | if e1Kind != e2Kind { |
| 251 | return Fail(t, "Elements should be the same type", msgAndArgs...) |
| 252 | } |
| 253 | |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 254 | compareResult, isComparable := compare(e1, e2, e1Kind) |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 255 | if !isComparable { |
| 256 | return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...) |
| 257 | } |
| 258 | |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 259 | if !containsValue(allowedComparesResults, compareResult) { |
| 260 | return Fail(t, fmt.Sprintf(failMessage, e1, e2), msgAndArgs...) |
Holger Hildebrandt | da7758b | 2020-03-16 11:30:03 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | return true |
| 264 | } |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 265 | |
| 266 | func containsValue(values []CompareType, value CompareType) bool { |
| 267 | for _, v := range values { |
| 268 | if v == value { |
| 269 | return true |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return false |
| 274 | } |