Skip to content

Commit 186ae4b

Browse files
committed
feat: add lcs cmd and BitPosSpan API
Signed-off-by: monkey92t <golang@88.com>
1 parent 871636f commit 186ae4b

File tree

6 files changed

+117
-3
lines changed

6 files changed

+117
-3
lines changed

commands_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,14 @@ var _ = Describe("Commands", func() {
759759
})
760760
})
761761

762+
It("BitPosSpan", func() {
763+
operationIntCmd(clientMock, func() *ExpectedInt {
764+
return clientMock.ExpectBitPosSpan("key", 1, 1, 3, "bit")
765+
}, func() *redis.IntCmd {
766+
return client.BitPosSpan(ctx, "key", 1, 1, 3, "bit")
767+
})
768+
})
769+
762770
It("BitField", func() {
763771
operationIntSliceCmd(clientMock, func() *ExpectedIntSlice {
764772
return clientMock.ExpectBitField("key", "INCRBY", "i5", 100, 1, "GET", "u4", 0)
@@ -983,6 +991,26 @@ var _ = Describe("Commands", func() {
983991
})
984992
})
985993

994+
It("LIndex", func() {
995+
operationLCSCmd(clientMock, func() *ExpectedLCS {
996+
return clientMock.ExpectLCS(&redis.LCSQuery{
997+
Key1: "key1",
998+
Key2: "key2",
999+
Idx: true,
1000+
MinMatchLen: 3,
1001+
WithMatchLen: true,
1002+
})
1003+
}, func() *redis.LCSCmd {
1004+
return client.LCS(ctx, &redis.LCSQuery{
1005+
Key1: "key1",
1006+
Key2: "key2",
1007+
Idx: true,
1008+
MinMatchLen: 3,
1009+
WithMatchLen: true,
1010+
})
1011+
})
1012+
})
1013+
9861014
It("LIndex", func() {
9871015
operationStringCmd(clientMock, func() *ExpectedString {
9881016
return clientMock.ExpectLIndex("key", 1)

expect.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type baseMock interface {
9999
ExpectBitOpXor(destKey string, keys ...string) *ExpectedInt
100100
ExpectBitOpNot(destKey string, key string) *ExpectedInt
101101
ExpectBitPos(key string, bit int64, pos ...int64) *ExpectedInt
102+
ExpectBitPosSpan(key string, bit int8, start, end int64, span string) *ExpectedInt
102103
ExpectBitField(key string, args ...interface{}) *ExpectedIntSlice
103104

104105
ExpectScan(cursor uint64, match string, count int64) *ExpectedScan
@@ -127,6 +128,7 @@ type baseMock interface {
127128
ExpectBLMPop(timeout time.Duration, direction string, count int64, keys ...string) *ExpectedKeyValues
128129
ExpectBRPop(timeout time.Duration, keys ...string) *ExpectedStringSlice
129130
ExpectBRPopLPush(source, destination string, timeout time.Duration) *ExpectedString
131+
ExpectLCS(q *redis.LCSQuery) *ExpectedLCS
130132
ExpectLIndex(key string, index int64) *ExpectedString
131133
ExpectLInsert(key, op string, pivot, value interface{}) *ExpectedInt
132134
ExpectLInsertBefore(key string, pivot, value interface{}) *ExpectedInt
@@ -1203,6 +1205,24 @@ func (cmd *ExpectedFunctionList) inflow(c redis.Cmder) {
12031205

12041206
// ------------------------------------------------------------
12051207

1208+
type ExpectedLCS struct {
1209+
expectedBase
1210+
1211+
val *redis.LCSMatch
1212+
}
1213+
1214+
func (cmd *ExpectedLCS) SetVal(val *redis.LCSMatch) {
1215+
cmd.setVal = true
1216+
v := *val
1217+
cmd.val = &v
1218+
}
1219+
1220+
func (cmd *ExpectedLCS) inflow(c redis.Cmder) {
1221+
inflow(c, "val", cmd.val)
1222+
}
1223+
1224+
// ------------------------------------------------------------
1225+
12061226
type ExpectedError struct {
12071227
expectedBase
12081228
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
github.com/onsi/ginkgo v1.16.5
77
github.com/onsi/gomega v1.25.0
8-
github.com/redis/go-redis/v9 v9.0.3-0.20230320053549-153a9efb96ab
8+
github.com/redis/go-redis/v9 v9.0.3-0.20230324073811-6790337e5ddf
99
)
1010

1111
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
3838
github.com/onsi/gomega v1.25.0 h1:Vw7br2PCDYijJHSfBOWhov+8cAnUf8MfMaIOV323l6Y=
3939
github.com/onsi/gomega v1.25.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM=
4040
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
41-
github.com/redis/go-redis/v9 v9.0.3-0.20230320053549-153a9efb96ab h1:9vEuFJboLo5/XLjeDN+8emgtKhVCr96KQGXx2y091O8=
42-
github.com/redis/go-redis/v9 v9.0.3-0.20230320053549-153a9efb96ab/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
41+
github.com/redis/go-redis/v9 v9.0.3-0.20230324073811-6790337e5ddf h1:QHQ3H0F7NrEOmIl6rMJjZemysCDIAbE8Tg7+yOmrGM8=
42+
github.com/redis/go-redis/v9 v9.0.3-0.20230324073811-6790337e5ddf/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
4343
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4444
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
4545
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

mock.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,13 @@ func (m *mock) ExpectBitPos(key string, bit int64, pos ...int64) *ExpectedInt {
942942
return e
943943
}
944944

945+
func (m *mock) ExpectBitPosSpan(key string, bit int8, start, end int64, span string) *ExpectedInt {
946+
e := &ExpectedInt{}
947+
e.cmd = m.factory.BitPosSpan(m.ctx, key, bit, start, end, span)
948+
m.pushExpect(e)
949+
return e
950+
}
951+
945952
func (m *mock) ExpectBitField(key string, args ...interface{}) *ExpectedIntSlice {
946953
e := &ExpectedIntSlice{}
947954
e.cmd = m.factory.BitField(m.ctx, key, args...)
@@ -1117,6 +1124,13 @@ func (m *mock) ExpectBRPopLPush(source, destination string, timeout time.Duratio
11171124
return e
11181125
}
11191126

1127+
func (m *mock) ExpectLCS(q *redis.LCSQuery) *ExpectedLCS {
1128+
e := &ExpectedLCS{}
1129+
e.cmd = m.factory.LCS(m.ctx, q)
1130+
m.pushExpect(e)
1131+
return e
1132+
}
1133+
11201134
func (m *mock) ExpectLIndex(key string, index int64) *ExpectedString {
11211135
e := &ExpectedString{}
11221136
e.cmd = m.factory.LIndex(m.ctx, key, index)

mock_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,3 +1460,55 @@ func operationFunctionListCmd(base baseMock, expected func() *ExpectedFunctionLi
14601460
Expect(err).NotTo(HaveOccurred())
14611461
Expect(val).To(Equal(libs))
14621462
}
1463+
1464+
func operationLCSCmd(base baseMock, expected func() *ExpectedLCS, actual func() *redis.LCSCmd) {
1465+
var (
1466+
setErr = errors.New("lcs cmd error")
1467+
val *redis.LCSMatch
1468+
err error
1469+
)
1470+
1471+
base.ClearExpect()
1472+
expected().SetErr(setErr)
1473+
val, err = actual().Result()
1474+
Expect(err).To(Equal(setErr))
1475+
Expect(val).To(BeNil())
1476+
1477+
base.ClearExpect()
1478+
expected()
1479+
val, err = actual().Result()
1480+
Expect(err).To(HaveOccurred())
1481+
Expect(val).To(BeNil())
1482+
1483+
lcs := &redis.LCSMatch{
1484+
MatchString: "match string",
1485+
}
1486+
1487+
base.ClearExpect()
1488+
expected().SetVal(lcs)
1489+
val, err = actual().Result()
1490+
Expect(err).NotTo(HaveOccurred())
1491+
Expect(val).To(Equal(lcs))
1492+
1493+
lcs = &redis.LCSMatch{
1494+
MatchString: "",
1495+
Matches: []redis.LCSMatchedPosition{
1496+
{
1497+
Key1: redis.LCSPosition{Start: 3, End: 5},
1498+
Key2: redis.LCSPosition{Start: 1, End: 3},
1499+
MatchLen: 2,
1500+
},
1501+
{
1502+
Key1: redis.LCSPosition{Start: 5, End: 8},
1503+
Key2: redis.LCSPosition{Start: 2, End: 5},
1504+
MatchLen: 3,
1505+
},
1506+
},
1507+
Len: 3,
1508+
}
1509+
base.ClearExpect()
1510+
expected().SetVal(lcs)
1511+
val, err = actual().Result()
1512+
Expect(err).NotTo(HaveOccurred())
1513+
Expect(val).To(Equal(lcs))
1514+
}

0 commit comments

Comments
 (0)