Skip to content

Commit 889ba3e

Browse files
authored
fix: Uint64s and Uint64Map tests (gomodule#469)
Fix bad tests introduced by Uint64s and Uint64Map work. Also: * Fix some doc typos.
1 parent 37c69a2 commit 889ba3e

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

redis/reply.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Int(reply interface{}, err error) (int, error) {
5656
}
5757

5858
// Int64 is a helper that converts a command reply to 64 bit integer. If err is
59-
// not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
59+
// not equal to nil, then Int64 returns 0, err. Otherwise, Int64 converts the
6060
// reply to an int64 as follows:
6161
//
6262
// Reply type Result
@@ -82,14 +82,16 @@ func Int64(reply interface{}, err error) (int64, error) {
8282
return 0, fmt.Errorf("redigo: unexpected type for Int64, got type %T", reply)
8383
}
8484

85-
var errNegativeInt = errors.New("redigo: unexpected value for Uint64")
85+
func errNegativeInt(v int64) error {
86+
return fmt.Errorf("redigo: unexpected negative value %v for Uint64", v)
87+
}
8688

87-
// Uint64 is a helper that converts a command reply to 64 bit integer. If err is
88-
// not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
89-
// reply to an int64 as follows:
89+
// Uint64 is a helper that converts a command reply to 64 bit unsigned integer.
90+
// If err is not equal to nil, then Uint64 returns 0, err. Otherwise, Uint64 converts the
91+
// reply to an uint64 as follows:
9092
//
9193
// Reply type Result
92-
// integer reply, nil
94+
// +integer reply, nil
9395
// bulk string parsed reply, nil
9496
// nil 0, ErrNil
9597
// other 0, error
@@ -100,7 +102,7 @@ func Uint64(reply interface{}, err error) (uint64, error) {
100102
switch reply := reply.(type) {
101103
case int64:
102104
if reply < 0 {
103-
return 0, errNegativeInt
105+
return 0, errNegativeInt(reply)
104106
}
105107
return uint64(reply), nil
106108
case []byte:
@@ -501,7 +503,6 @@ func Uint64s(reply interface{}, err error) ([]uint64, error) {
501503
return result, err
502504
}
503505

504-
505506
// Uint64Map is a helper that converts an array of strings (alternating key, value)
506507
// into a map[string]uint64. The HGETALL commands return replies in this format.
507508
// Requires an even number of values in result.

redis/reply_test.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import (
2525
"github.com/gomodule/redigo/redis"
2626
)
2727

28+
var (
29+
maxUint64Str = strconv.FormatUint(math.MaxUint64, 10)
30+
)
31+
2832
type valueError struct {
2933
v interface{}
3034
err error
@@ -69,38 +73,18 @@ var replyTests = []struct {
6973
ve(redis.Int64s([]interface{}{int64(4), int64(5)}, nil)),
7074
ve([]int64{4, 5}, nil),
7175
},
72-
{
73-
"uint64s([uint64, uint64])",
74-
ve(redis.Uint64s([]interface{}{uint64(4), uint64(5)}, nil)),
75-
ve([]uint64{4, 5}, nil),
76-
},
77-
{
78-
"uint64s([uint64, uint64])",
79-
ve(redis.Uint64s([]interface{}{math.MaxUint64, math.MaxUint64}, nil)),
80-
ve([]uint64{math.MaxUint64, math.MaxUint64}, nil),
81-
},
8276
{
8377
"uint64s([[]byte, []byte])",
84-
ve(redis.Uint64s([]interface{}{[]byte("4"), []byte("5")}, nil)),
85-
ve([]uint64{4, 5}, nil),
78+
ve(redis.Uint64s([]interface{}{[]byte(maxUint64Str), []byte("5")}, nil)),
79+
ve([]uint64{math.MaxUint64, 5}, nil),
8680
},
8781
{
8882
"Uint64Map([[]byte, []byte])",
89-
ve(redis.Uint64Map([]interface{}{[]byte("4"), []byte("5")}, nil)),
90-
ve(map[string]uint64{"1": 4, "2": 5}, nil),
91-
},
92-
{
93-
"Uint64Map([uint64, uint64])",
94-
ve(redis.Uint64Map([]interface{}{uint64(4), uint64(5)}, nil)),
95-
ve(map[string]uint64{"1": 4, "2": 5}, nil),
96-
},
97-
{
98-
"Uint64Map([uint64, uint64])",
99-
ve(redis.Uint64Map([]interface{}{math.MaxUint64, math.MaxUint64}, nil)),
100-
ve(map[string]uint64{"1": math.MaxUint64, "2": math.MaxUint64}, nil),
83+
ve(redis.Uint64Map([]interface{}{[]byte("key1"), []byte(maxUint64Str), []byte("key2"), []byte("5")}, nil)),
84+
ve(map[string]uint64{"key1": math.MaxUint64, "key2": 5}, nil),
10185
},
10286
{
103-
"strings([[]byte, []bytev2])",
87+
"strings([[]byte, []byte])",
10488
ve(redis.Strings([]interface{}{[]byte("v1"), []byte("v2")}, nil)),
10589
ve([]string{"v1", "v2"}, nil),
10690
},
@@ -147,7 +131,7 @@ var replyTests = []struct {
147131
{
148132
"uint64(-1)",
149133
ve(redis.Uint64(int64(-1), nil)),
150-
ve(uint64(0), redis.ErrNegativeInt),
134+
ve(uint64(0), redis.ErrNegativeInt(-1)),
151135
},
152136
{
153137
"positions([[1, 2], nil, [3, 4]])",
@@ -171,7 +155,7 @@ func getSlowLog() (redis.SlowLog, error) {
171155

172156
func TestReply(t *testing.T) {
173157
for _, rt := range replyTests {
174-
if rt.actual.err != rt.expected.err {
158+
if rt.actual.err != rt.expected.err && rt.actual.err.Error() != rt.expected.err.Error() {
175159
t.Errorf("%s returned err %v, want %v", rt.name, rt.actual.err, rt.expected.err)
176160
continue
177161
}

0 commit comments

Comments
 (0)