Repository: stretchr/testify
Pull request #1339 (2023-02-04 02:21:29)

Add support to compare uintptr


Patch:
diff --git a/assert/assertion_compare.go b/assert/assertion_compare.go
index 34b9c13..4d4b4aa 100644
--- a/assert/assertion_compare.go
+++ b/assert/assertion_compare.go
@@ -28,6 +28,8 @@ var (
 	uint32Type = reflect.TypeOf(uint32(1))
 	uint64Type = reflect.TypeOf(uint64(1))
 
+	uintptrType = reflect.TypeOf(uintptr(1))
+
 	float32Type = reflect.TypeOf(float32(1))
 	float64Type = reflect.TypeOf(float64(1))
 
@@ -345,6 +347,26 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
 
 			return CompareType(bytes.Compare(bytesObj1, bytesObj2)), true
 		}
+	case reflect.Uintptr:
+		{
+			uintptrObj1, ok := obj1.(uintptr)
+			if !ok {
+				uintptrObj1 = obj1Value.Convert(uintptrType).Interface().(uintptr)
+			}
+			uintptrObj2, ok := obj2.(uintptr)
+			if !ok {
+				uintptrObj2 = obj2Value.Convert(uintptrType).Interface().(uintptr)
+			}
+			if uintptrObj1 > uintptrObj2 {
+				return compareGreater, true
+			}
+			if uintptrObj1 == uintptrObj2 {
+				return compareEqual, true
+			}
+			if uintptrObj1 < uintptrObj2 {
+				return compareLess, true
+			}
+		}
 	}
 
 	return compareEqual, false


Test patch:
diff --git a/assert/assertion_compare_test.go b/assert/assertion_compare_test.go
index e155313..95a7a7b 100644
--- a/assert/assertion_compare_test.go
+++ b/assert/assertion_compare_test.go
@@ -10,6 +10,7 @@ import (
 )
 
 func TestCompare(t *testing.T) {
+	type customString string
 	type customInt int
 	type customInt8 int8
 	type customInt16 int16
@@ -22,7 +23,7 @@ func TestCompare(t *testing.T) {
 	type customUInt64 uint64
 	type customFloat32 float32
 	type customFloat64 float64
-	type customString string
+	type customUintptr uintptr
 	type customTime time.Time
 	type customBytes []byte
 	for _, currCase := range []struct {
@@ -55,6 +56,8 @@ func TestCompare(t *testing.T) {
 		{less: customFloat32(1.23), greater: customFloat32(2.23), cType: "float32"},
 		{less: float64(1.23), greater: float64(2.34), cType: "float64"},
 		{less: customFloat64(1.23), greater: customFloat64(2.34), cType: "float64"},
+		{less: uintptr(1), greater: uintptr(2), cType: "uintptr"},
+		{less: customUintptr(1), greater: customUintptr(2), cType: "uint64"},
 		{less: time.Now(), greater: time.Now().Add(time.Hour), cType: "time.Time"},
 		{less: customTime(time.Now()), greater: customTime(time.Now().Add(time.Hour)), cType: "time.Time"},
 		{less: []byte{1, 1}, greater: []byte{1, 2}, cType: "[]byte"},
@@ -155,8 +158,9 @@ func TestGreater(t *testing.T) {
 		{less: uint64(1), greater: uint64(2), msg: `"1" is not greater than "2"`},
 		{less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than "2.34"`},
 		{less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than "2.34"`},
-		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than "[1 2]"`},
+		{less: uintptr(1), greater: uintptr(2), msg: `"1" is not greater than "2"`},
 		{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 00:00:00 +0000 UTC" is not greater than "0001-01-01 01:00:00 +0000 UTC"`},
+		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than "[1 2]"`},
 	} {
 		out := &outputT{buf: bytes.NewBuffer(nil)}
 		False(t, Greater(out, currCase.less, currCase.greater))
@@ -198,8 +202,9 @@ func TestGreaterOrEqual(t *testing.T) {
 		{less: uint64(1), greater: uint64(2), msg: `"1" is not greater than or equal to "2"`},
 		{less: float32(1.23), greater: float32(2.34), msg: `"1.23" is not greater than or equal to "2.34"`},
 		{less: float64(1.23), greater: float64(2.34), msg: `"1.23" is not greater than or equal to "2.34"`},
-		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than or equal to "[1 2]"`},
+		{less: uintptr(1), greater: uintptr(2), msg: `"1" is not greater than or equal to "2"`},
 		{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 00:00:00 +0000 UTC" is not greater than or equal to "0001-01-01 01:00:00 +0000 UTC"`},
+		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than or equal to "[1 2]"`},
 	} {
 		out := &outputT{buf: bytes.NewBuffer(nil)}
 		False(t, GreaterOrEqual(out, currCase.less, currCase.greater))
@@ -241,8 +246,9 @@ func TestLess(t *testing.T) {
 		{less: uint64(1), greater: uint64(2), msg: `"2" is not less than "1"`},
 		{less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than "1.23"`},
 		{less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than "1.23"`},
-		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than "[1 1]"`},
+		{less: uintptr(1), greater: uintptr(2), msg: `"2" is not less than "1"`},
 		{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 01:00:00 +0000 UTC" is not less than "0001-01-01 00:00:00 +0000 UTC"`},
+		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than "[1 1]"`},
 	} {
 		out := &outputT{buf: bytes.NewBuffer(nil)}
 		False(t, Less(out, currCase.greater, currCase.less))
@@ -284,8 +290,9 @@ func TestLessOrEqual(t *testing.T) {
 		{less: uint64(1), greater: uint64(2), msg: `"2" is not less than or equal to "1"`},
 		{less: float32(1.23), greater: float32(2.34), msg: `"2.34" is not less than or equal to "1.23"`},
 		{less: float64(1.23), greater: float64(2.34), msg: `"2.34" is not less than or equal to "1.23"`},
-		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than or equal to "[1 1]"`},
+		{less: uintptr(1), greater: uintptr(2), msg: `"2" is not less than or equal to "1"`},
 		{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 01:00:00 +0000 UTC" is not less than or equal to "0001-01-01 00:00:00 +0000 UTC"`},
+		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than or equal to "[1 1]"`},
 	} {
 		out := &outputT{buf: bytes.NewBuffer(nil)}
 		False(t, LessOrEqual(out, currCase.greater, currCase.less))