Passed
Pull Request — master (#23)
by Frank
06:05 queued 03:00
created

assert.compare   F

Complexity

Conditions 53

Size

Total Lines 187
Code Lines 133

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 53
eloc 133
nop 3
dl 0
loc 187
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like assert.compare often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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