Skip to content

Commit 1823b6a

Browse files
committed
feat: mixed signed and unsigned are not allowed anymore.
1 parent 8959c7c commit 1823b6a

File tree

6 files changed

+15
-55
lines changed

6 files changed

+15
-55
lines changed

builtin.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func NotDeepEqualNow(t *testing.T, actual, expect any, message ...any) error {
169169
// Equal(t, 1, 1) // success
170170
// Equal(t, "ABC", "ABC") // success
171171
// Equal(t, 1, int64(1)) // success
172-
// Equal(t, 1, uint64(1)) // success
172+
// Equal(t, 1, uint64(1)) // fail
173173
// Equal(t, 1, 0) // fail
174174
func Equal(t *testing.T, actual, expect any, message ...any) error {
175175
t.Helper()
@@ -183,7 +183,6 @@ func Equal(t *testing.T, actual, expect any, message ...any) error {
183183
// EqualNow(t, 1, 1) // success
184184
// EqualNow(t, "ABC", "ABC") // success
185185
// EqualNow(t, 1, int64(1)) // success
186-
// EqualNow(t, 1, uint64(1)) // success
187186
// EqualNow(t, 1, 0) // fail and terminate
188187
// never run
189188
func EqualNow(t *testing.T, actual, expect any, message ...any) error {
@@ -197,10 +196,10 @@ func EqualNow(t *testing.T, actual, expect any, message ...any) error {
197196
//
198197
// NotEqual(t, 1, 0) // success
199198
// NotEqual(t, "ABC", "CBA") // success
199+
// NotEqual(t, 1, uint64(1)) // success
200200
// NotEqual(t, 1, 1) // fail
201201
// NotEqual(t, "ABC", "ABC") // fail
202202
// NotEqual(t, 1, int64(1)) // fail
203-
// NotEqual(t, 1, uint64(1)) // fail
204203
func NotEqual(t *testing.T, actual, expect any, message ...any) error {
205204
t.Helper()
206205

compare.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package assert
22

33
import (
44
"fmt"
5-
"math"
65
"reflect"
76
"testing"
87
)
@@ -93,7 +92,7 @@ func tryNotDeepEqual(t *testing.T, failedNow bool, actual, expect any, message .
9392
// assertion.Equal(1, 1) // success
9493
// assertion.Equal("ABC", "ABC") // success
9594
// assertion.Equal(1, int64(1)) // success
96-
// assertion.Equal(1, uint64(1)) // success
95+
// assertion.Equal(1, uint64(1)) // fail
9796
// assertion.Equal(1, 0) // fail
9897
func (a *Assertion) Equal(actual, expect any, message ...any) error {
9998
a.Helper()
@@ -107,7 +106,6 @@ func (a *Assertion) Equal(actual, expect any, message ...any) error {
107106
// assertion.EqualNow(1, 1) // success
108107
// assertion.EqualNow("ABC", "ABC") // success
109108
// assertion.EqualNow(1, int64(1)) // success
110-
// assertion.EqualNow(1, uint64(1)) // success
111109
// assertion.EqualNow(1, 0) // fail and terminate
112110
// never run
113111
func (a *Assertion) EqualNow(actual, expect any, message ...any) error {
@@ -121,10 +119,10 @@ func (a *Assertion) EqualNow(actual, expect any, message ...any) error {
121119
//
122120
// assertion.NotEqual(1, 0) // success
123121
// assertion.NotEqual("ABC", "CBA") // success
122+
// assertion.NotEqual(1, uint64(1)) // success
124123
// assertion.NotEqual(1, 1) // fail
125124
// assertion.NotEqual("ABC", "ABC") // fail
126125
// assertion.NotEqual(1, int64(1)) // fail
127-
// assertion.NotEqual(1, uint64(1)) // fail
128126
func (a *Assertion) NotEqual(actual, expect any, message ...any) error {
129127
a.Helper()
130128

@@ -376,10 +374,7 @@ func isEqual(x, y any) bool {
376374
v2 = reflect.ValueOf(y)
377375
}
378376

379-
if isSame, isMixSign := isSameType(v1.Type(), v2.Type()); !isSame {
380-
if isMixSign {
381-
return isEqualForMixSignInt(v1, v2)
382-
}
377+
if ok := isSameType(v1.Type(), v2.Type()); !ok {
383378
return false
384379
}
385380

@@ -402,25 +397,6 @@ func isEqual(x, y any) bool {
402397
}
403398
}
404399

405-
// isEqualForMixSignInt checks the equality of two integers one of an integer is signed, but
406-
// another one is unsigned.
407-
func isEqualForMixSignInt(v1, v2 reflect.Value) bool {
408-
intVal := v1
409-
uintVal := v2
410-
if v1.Kind() >= reflect.Uint && v1.Kind() <= reflect.Uintptr {
411-
intVal = v2
412-
uintVal = v1
413-
}
414-
415-
if intVal.Int() < 0 {
416-
return false
417-
} else if uintVal.Uint() > uint64(math.MaxInt64) {
418-
return false
419-
}
420-
421-
return intVal.Int() == int64(uintVal.Uint())
422-
}
423-
424400
// isNil checks whether a value is nil or not. It'll always return false if the value is not a
425401
// channel, a function, a map, a point, an unsafe point, an interface, or a slice.
426402
func isNil(val any) bool {

compare_test.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package assert
22

33
import (
4-
"math"
54
"testing"
65
)
76

@@ -74,7 +73,7 @@ func TestEqualAndNotEqual(t *testing.T) {
7473
testEqualAndNotEqual(a, mockA, 1, 1, true)
7574
testEqualAndNotEqual(a, mockA, 1, 2, false)
7675
testEqualAndNotEqual(a, mockA, 1, int64(1), true)
77-
testEqualAndNotEqual(a, mockA, 1, uint64(1), true)
76+
testEqualAndNotEqual(a, mockA, 1, uint64(1), false)
7877
testEqualAndNotEqual(a, mockA, 1, 1.0, false)
7978
testEqualAndNotEqual(a, mockA, 1, "1", false)
8079
testEqualAndNotEqual(a, mockA, 1, '1', false)
@@ -285,18 +284,6 @@ func TestIsEqual(t *testing.T) {
285284
assert.Equal(isEqual(testStruct1{A: 0}, testStruct2{A: 0}), false)
286285
}
287286

288-
func TestIsEqualOfMixSignInt(t *testing.T) {
289-
assert := New(t)
290-
291-
assert.True(isEqual(0, uint(0)))
292-
assert.True(isEqual(1, uint(1)))
293-
assert.True(isEqual(uint(1), 1))
294-
assert.True(isEqual(math.MaxInt64, uint64(math.MaxInt64)))
295-
assert.NotTrue(isEqual(-1, uint64(math.MaxUint64)))
296-
assert.NotTrue(isEqual(uint64(math.MaxUint64), -1))
297-
assert.NotTrue(isEqual(uint64(math.MaxUint64), 0))
298-
}
299-
300287
func TestIsNil(t *testing.T) {
301288
assert := New(t)
302289

slice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func isContainsElement(source, elem any) bool {
109109
if st.Kind() != reflect.Array && st.Kind() != reflect.Slice {
110110
panic("require array or slice")
111111
}
112-
if ok, isMixed := isSameType(st.Type().Elem(), reflect.TypeOf(elem)); !ok && !isMixed {
112+
if ok := isSameType(st.Type().Elem(), reflect.TypeOf(elem)); !ok {
113113
panic("require same type")
114114
}
115115

slice_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func TestIsContainsElement(t *testing.T) {
7373
assert.NotTrueNow(isContainsElement([]int{1, 2, 3}, 4))
7474
assert.TrueNow(isContainsElement([]int64{1, 2, 3}, 3))
7575
assert.NotTrueNow(isContainsElement([]int64{1, 2, 3}, 4))
76-
assert.TrueNow(isContainsElement([]uint64{1, 2, 3}, 3))
77-
assert.NotTrueNow(isContainsElement([]uint64{1, 2, 3}, 4))
76+
assert.TrueNow(isContainsElement([]uint64{1, 2, 3}, uint(3)))
77+
assert.NotTrueNow(isContainsElement([]uint64{1, 2, 3}, uint(4)))
7878
assert.TrueNow(isContainsElement(&[]int{1, 2, 3}, 3))
7979
assert.TrueNow(isContainsElement([3]int{1, 2, 3}, 3))
8080
assert.NotTrueNow(isContainsElement([3]int{1, 2, 3}, 4))

util.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,20 @@ func failed(t *testing.T, err error, failedNow bool) {
5252
//
5353
// It will returns a bool value to tell the callee function that one of the values is an integer,
5454
// but another one is unsigned. For this case, it needs to check the value to compare them.
55-
func isSameType(t1, t2 reflect.Type) (isSame bool, isMixSign bool) {
55+
func isSameType(t1, t2 reflect.Type) bool {
5656
kind := t2.Kind()
5757

5858
switch t1.Kind() {
5959
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
60-
return kind >= reflect.Int && kind <= reflect.Int64,
61-
kind >= reflect.Uint && kind <= reflect.Uintptr
60+
return kind >= reflect.Int && kind <= reflect.Int64
6261
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
6362
reflect.Uintptr:
64-
return kind >= reflect.Uint && kind <= reflect.Uintptr,
65-
kind >= reflect.Int && kind <= reflect.Int64
63+
return kind >= reflect.Uint && kind <= reflect.Uintptr
6664
case reflect.Float32, reflect.Float64:
67-
return kind == reflect.Float32 || kind == reflect.Float64, false
65+
return kind == reflect.Float32 || kind == reflect.Float64
6866
case reflect.Complex64, reflect.Complex128:
69-
return kind == reflect.Complex64 || kind == reflect.Complex128, false
67+
return kind == reflect.Complex64 || kind == reflect.Complex128
7068
default:
71-
return t1 == t2, false
69+
return t1 == t2
7270
}
7371
}

0 commit comments

Comments
 (0)