Skip to content

Commit a803268

Browse files
committed
Add support to compare uintptr
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 0ab3ce1 commit a803268

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

assert/assertion_compare.go

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ var (
3333

3434
stringType = reflect.TypeOf("")
3535

36+
uintptrType = reflect.TypeOf(uintptr(1))
37+
3638
timeType = reflect.TypeOf(time.Time{})
3739
bytesType = reflect.TypeOf([]byte{})
3840
)
@@ -345,16 +347,37 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
345347

346348
return CompareType(bytes.Compare(bytesObj1, bytesObj2)), true
347349
}
350+
351+
case reflect.Uintptr:
352+
{
353+
uintptrObj1, ok := obj1.(uintptr)
354+
if !ok {
355+
uintptrObj1 = obj1Value.Convert(uintptrType).Interface().(uintptr)
356+
}
357+
uintptrObj2, ok := obj2.(uintptr)
358+
if !ok {
359+
uintptrObj2 = obj2Value.Convert(uintptrType).Interface().(uintptr)
360+
}
361+
if uintptrObj1 > uintptrObj2 {
362+
return compareGreater, true
363+
}
364+
if uintptrObj1 == uintptrObj2 {
365+
return compareEqual, true
366+
}
367+
if uintptrObj1 < uintptrObj2 {
368+
return compareLess, true
369+
}
370+
}
348371
}
349372

350373
return compareEqual, false
351374
}
352375

353376
// Greater asserts that the first element is greater than the second
354377
//
355-
// assert.Greater(t, 2, 1)
356-
// assert.Greater(t, float64(2), float64(1))
357-
// assert.Greater(t, "b", "a")
378+
// assert.Greater(t, 2, 1)
379+
// assert.Greater(t, float64(2), float64(1))
380+
// assert.Greater(t, "b", "a")
358381
func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
359382
if h, ok := t.(tHelper); ok {
360383
h.Helper()
@@ -364,10 +387,10 @@ func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface
364387

365388
// GreaterOrEqual asserts that the first element is greater than or equal to the second
366389
//
367-
// assert.GreaterOrEqual(t, 2, 1)
368-
// assert.GreaterOrEqual(t, 2, 2)
369-
// assert.GreaterOrEqual(t, "b", "a")
370-
// assert.GreaterOrEqual(t, "b", "b")
390+
// assert.GreaterOrEqual(t, 2, 1)
391+
// assert.GreaterOrEqual(t, 2, 2)
392+
// assert.GreaterOrEqual(t, "b", "a")
393+
// assert.GreaterOrEqual(t, "b", "b")
371394
func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
372395
if h, ok := t.(tHelper); ok {
373396
h.Helper()
@@ -377,9 +400,9 @@ func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...in
377400

378401
// Less asserts that the first element is less than the second
379402
//
380-
// assert.Less(t, 1, 2)
381-
// assert.Less(t, float64(1), float64(2))
382-
// assert.Less(t, "a", "b")
403+
// assert.Less(t, 1, 2)
404+
// assert.Less(t, float64(1), float64(2))
405+
// assert.Less(t, "a", "b")
383406
func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
384407
if h, ok := t.(tHelper); ok {
385408
h.Helper()
@@ -389,10 +412,10 @@ func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{})
389412

390413
// LessOrEqual asserts that the first element is less than or equal to the second
391414
//
392-
// assert.LessOrEqual(t, 1, 2)
393-
// assert.LessOrEqual(t, 2, 2)
394-
// assert.LessOrEqual(t, "a", "b")
395-
// assert.LessOrEqual(t, "b", "b")
415+
// assert.LessOrEqual(t, 1, 2)
416+
// assert.LessOrEqual(t, 2, 2)
417+
// assert.LessOrEqual(t, "a", "b")
418+
// assert.LessOrEqual(t, "b", "b")
396419
func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
397420
if h, ok := t.(tHelper); ok {
398421
h.Helper()
@@ -402,8 +425,8 @@ func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...inter
402425

403426
// Positive asserts that the specified element is positive
404427
//
405-
// assert.Positive(t, 1)
406-
// assert.Positive(t, 1.23)
428+
// assert.Positive(t, 1)
429+
// assert.Positive(t, 1.23)
407430
func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
408431
if h, ok := t.(tHelper); ok {
409432
h.Helper()
@@ -414,8 +437,8 @@ func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
414437

415438
// Negative asserts that the specified element is negative
416439
//
417-
// assert.Negative(t, -1)
418-
// assert.Negative(t, -1.23)
440+
// assert.Negative(t, -1)
441+
// assert.Negative(t, -1.23)
419442
func Negative(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
420443
if h, ok := t.(tHelper); ok {
421444
h.Helper()

assert/assertion_compare_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestCompare(t *testing.T) {
2222
type customFloat32 float32
2323
type customFloat64 float64
2424
type customString string
25+
type customUintptr uintptr
2526
for _, currCase := range []struct {
2627
less interface{}
2728
greater interface{}
@@ -52,6 +53,8 @@ func TestCompare(t *testing.T) {
5253
{less: customFloat32(1.23), greater: customFloat32(2.23), cType: "float32"},
5354
{less: float64(1.23), greater: float64(2.34), cType: "float64"},
5455
{less: customFloat64(1.23), greater: customFloat64(2.34), cType: "float64"},
56+
{less: uintptr(1), greater: uintptr(2), cType: "uintptr"},
57+
{less: customUintptr(1), greater: customUintptr(2), cType: "uint64"},
5558
} {
5659
resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind())
5760
if !isComparable {
@@ -148,6 +151,7 @@ func TestGreater(t *testing.T) {
148151
{less: uint64(1), greater: uint64(2), msg: `"1" is not greater than "2"`},
149152
{less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than "2.34"`},
150153
{less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than "2.34"`},
154+
{less: uintptr(1), greater: uintptr(2), msg: `"1" is not greater than "2"`},
151155
} {
152156
out := &outputT{buf: bytes.NewBuffer(nil)}
153157
False(t, Greater(out, currCase.less, currCase.greater))
@@ -189,6 +193,7 @@ func TestGreaterOrEqual(t *testing.T) {
189193
{less: uint64(1), greater: uint64(2), msg: `"1" is not greater than or equal to "2"`},
190194
{less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than or equal to "2.34"`},
191195
{less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than or equal to "2.34"`},
196+
{less: uintptr(1), greater: uintptr(2), msg: `"1" is not greater than or equal to "2"`},
192197
} {
193198
out := &outputT{buf: bytes.NewBuffer(nil)}
194199
False(t, GreaterOrEqual(out, currCase.less, currCase.greater))
@@ -230,6 +235,7 @@ func TestLess(t *testing.T) {
230235
{less: uint64(1), greater: uint64(2), msg: `"2" is not less than "1"`},
231236
{less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than "1.23"`},
232237
{less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than "1.23"`},
238+
{less: uintptr(1), greater: uintptr(2), msg: `"2" is not less than "1"`},
233239
} {
234240
out := &outputT{buf: bytes.NewBuffer(nil)}
235241
False(t, Less(out, currCase.greater, currCase.less))
@@ -271,6 +277,7 @@ func TestLessOrEqual(t *testing.T) {
271277
{less: uint64(1), greater: uint64(2), msg: `"2" is not less than or equal to "1"`},
272278
{less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than or equal to "1.23"`},
273279
{less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than or equal to "1.23"`},
280+
{less: uintptr(1), greater: uintptr(2), msg: `"2" is not less than or equal to "1"`},
274281
} {
275282
out := &outputT{buf: bytes.NewBuffer(nil)}
276283
False(t, LessOrEqual(out, currCase.greater, currCase.less))

0 commit comments

Comments
 (0)