blob: dc200395ceb700c8d5c0858abfce35223c31013e [file] [log] [blame]
Holger Hildebrandtda7758b2020-03-16 11:30:03 +00001package assert
2
3import (
4 "fmt"
5 "reflect"
6)
7
mpagenkoaf801632020-07-03 10:00:42 +00008type CompareType int
9
10const (
11 compareLess CompareType = iota - 1
12 compareEqual
13 compareGreater
14)
15
16func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000017 switch kind {
18 case reflect.Int:
19 {
20 intobj1 := obj1.(int)
21 intobj2 := obj2.(int)
22 if intobj1 > intobj2 {
mpagenkoaf801632020-07-03 10:00:42 +000023 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000024 }
25 if intobj1 == intobj2 {
mpagenkoaf801632020-07-03 10:00:42 +000026 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000027 }
28 if intobj1 < intobj2 {
mpagenkoaf801632020-07-03 10:00:42 +000029 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000030 }
31 }
32 case reflect.Int8:
33 {
34 int8obj1 := obj1.(int8)
35 int8obj2 := obj2.(int8)
36 if int8obj1 > int8obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000037 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000038 }
39 if int8obj1 == int8obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000040 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000041 }
42 if int8obj1 < int8obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000043 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000044 }
45 }
46 case reflect.Int16:
47 {
48 int16obj1 := obj1.(int16)
49 int16obj2 := obj2.(int16)
50 if int16obj1 > int16obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000051 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000052 }
53 if int16obj1 == int16obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000054 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000055 }
56 if int16obj1 < int16obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000057 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000058 }
59 }
60 case reflect.Int32:
61 {
62 int32obj1 := obj1.(int32)
63 int32obj2 := obj2.(int32)
64 if int32obj1 > int32obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000065 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000066 }
67 if int32obj1 == int32obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000068 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000069 }
70 if int32obj1 < int32obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000071 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000072 }
73 }
74 case reflect.Int64:
75 {
76 int64obj1 := obj1.(int64)
77 int64obj2 := obj2.(int64)
78 if int64obj1 > int64obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000079 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000080 }
81 if int64obj1 == int64obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000082 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000083 }
84 if int64obj1 < int64obj2 {
mpagenkoaf801632020-07-03 10:00:42 +000085 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000086 }
87 }
88 case reflect.Uint:
89 {
90 uintobj1 := obj1.(uint)
91 uintobj2 := obj2.(uint)
92 if uintobj1 > uintobj2 {
mpagenkoaf801632020-07-03 10:00:42 +000093 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000094 }
95 if uintobj1 == uintobj2 {
mpagenkoaf801632020-07-03 10:00:42 +000096 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +000097 }
98 if uintobj1 < uintobj2 {
mpagenkoaf801632020-07-03 10:00:42 +000099 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000100 }
101 }
102 case reflect.Uint8:
103 {
104 uint8obj1 := obj1.(uint8)
105 uint8obj2 := obj2.(uint8)
106 if uint8obj1 > uint8obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000107 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000108 }
109 if uint8obj1 == uint8obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000110 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000111 }
112 if uint8obj1 < uint8obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000113 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000114 }
115 }
116 case reflect.Uint16:
117 {
118 uint16obj1 := obj1.(uint16)
119 uint16obj2 := obj2.(uint16)
120 if uint16obj1 > uint16obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000121 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000122 }
123 if uint16obj1 == uint16obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000124 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000125 }
126 if uint16obj1 < uint16obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000127 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000128 }
129 }
130 case reflect.Uint32:
131 {
132 uint32obj1 := obj1.(uint32)
133 uint32obj2 := obj2.(uint32)
134 if uint32obj1 > uint32obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000135 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000136 }
137 if uint32obj1 == uint32obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000138 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000139 }
140 if uint32obj1 < uint32obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000141 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000142 }
143 }
144 case reflect.Uint64:
145 {
146 uint64obj1 := obj1.(uint64)
147 uint64obj2 := obj2.(uint64)
148 if uint64obj1 > uint64obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000149 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000150 }
151 if uint64obj1 == uint64obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000152 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000153 }
154 if uint64obj1 < uint64obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000155 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000156 }
157 }
158 case reflect.Float32:
159 {
160 float32obj1 := obj1.(float32)
161 float32obj2 := obj2.(float32)
162 if float32obj1 > float32obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000163 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000164 }
165 if float32obj1 == float32obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000166 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000167 }
168 if float32obj1 < float32obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000169 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000170 }
171 }
172 case reflect.Float64:
173 {
174 float64obj1 := obj1.(float64)
175 float64obj2 := obj2.(float64)
176 if float64obj1 > float64obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000177 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000178 }
179 if float64obj1 == float64obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000180 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000181 }
182 if float64obj1 < float64obj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000183 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000184 }
185 }
186 case reflect.String:
187 {
188 stringobj1 := obj1.(string)
189 stringobj2 := obj2.(string)
190 if stringobj1 > stringobj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000191 return compareGreater, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000192 }
193 if stringobj1 == stringobj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000194 return compareEqual, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000195 }
196 if stringobj1 < stringobj2 {
mpagenkoaf801632020-07-03 10:00:42 +0000197 return compareLess, true
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000198 }
199 }
200 }
201
mpagenkoaf801632020-07-03 10:00:42 +0000202 return compareEqual, false
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000203}
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")
210func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
mpagenkoaf801632020-07-03 10:00:42 +0000211 return compareTwoValues(t, e1, e2, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs)
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000212}
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")
220func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
mpagenkoaf801632020-07-03 10:00:42 +0000221 return compareTwoValues(t, e1, e2, []CompareType{compareGreater, compareEqual}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs)
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000222}
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")
229func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
mpagenkoaf801632020-07-03 10:00:42 +0000230 return compareTwoValues(t, e1, e2, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs)
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000231}
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")
239func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
mpagenkoaf801632020-07-03 10:00:42 +0000240 return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs)
241}
242
243func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool {
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000244 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
mpagenkoaf801632020-07-03 10:00:42 +0000254 compareResult, isComparable := compare(e1, e2, e1Kind)
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000255 if !isComparable {
256 return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
257 }
258
mpagenkoaf801632020-07-03 10:00:42 +0000259 if !containsValue(allowedComparesResults, compareResult) {
260 return Fail(t, fmt.Sprintf(failMessage, e1, e2), msgAndArgs...)
Holger Hildebrandtda7758b2020-03-16 11:30:03 +0000261 }
262
263 return true
264}
mpagenkoaf801632020-07-03 10:00:42 +0000265
266func 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}