Skip to content

Commit

Permalink
Add the LLEN command
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfowler committed Apr 7, 2016
1 parent 3b4e4d4 commit 57b8aa9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
15 changes: 14 additions & 1 deletion wredis_lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ func (w *Wredis) RPop(key string) (string, error) {
return stringError("key cannot be empty")
}
return w.ExecString(func(conn redis.Conn) (string, error) {
return redis.String(conn.Do("RPOP", key))
args := redis.Args{}.Add(key)
return redis.String(conn.Do("RPOP", args...))
})
}

// LLen returns the length of the list stored at key. For more information, see
// http://redis.io/commands/llen.
func (w *Wredis) LLen(key string) (int64, error) {
if key == "" {
return int64Error("key cannot be empty")
}
return w.ExecInt64(func(conn redis.Conn) (int64, error) {
args := redis.Args{}.Add(key)
return redis.Int64(conn.Do("LLEN", args...))
})
}
34 changes: 26 additions & 8 deletions wredis_lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ var _ = Describe("WredisLists", func() {

var testList = "wredis::test::list"

Context("LPush", func() {
BeforeEach(func() {
unsafe.Del(testList)
})
BeforeEach(func() {
unsafe.Del(testList)
})

Context("LPush", func() {
It("should return an error when no key provided", func() {
_, err := safe.LPush("")
Ω(err).Should(HaveOccurred())
Expand Down Expand Up @@ -50,10 +50,6 @@ var _ = Describe("WredisLists", func() {
})

Context("RPop", func() {
BeforeEach(func() {
unsafe.Del(testList)
})

It("should return an error when no key provided", func() {
_, err := safe.RPop("")
Ω(err).Should(HaveOccurred())
Expand All @@ -76,4 +72,26 @@ var _ = Describe("WredisLists", func() {
Ω(i).Should(Equal("1"))
})
})

Context("LLen", func() {
It("should return an error when no key provided", func() {
_, err := safe.LLen("")
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("key cannot be empty"))
})

It("should return 0 when the list doesn't exist", func() {
i, err := safe.LLen(testList)
Ω(err).ShouldNot(HaveOccurred())
Ω(i).Should(Equal(int64(0)))
})

It("should return the length of the list", func() {
_, err := safe.LPush(testList, "1", "2", "3")
Ω(err).ShouldNot(HaveOccurred())
i, err := safe.LLen(testList)
Ω(err).ShouldNot(HaveOccurred())
Ω(i).Should(Equal(int64(3)))
})
})
})

0 comments on commit 57b8aa9

Please sign in to comment.