From 57b8aa98d5cf151e1dc7aeed796b26937d571901 Mon Sep 17 00:00:00 2001 From: ryanfowler Date: Thu, 7 Apr 2016 12:39:50 -0400 Subject: [PATCH] Add the LLEN command --- wredis_lists.go | 15 ++++++++++++++- wredis_lists_test.go | 34 ++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/wredis_lists.go b/wredis_lists.go index 2efcfb3..b156560 100644 --- a/wredis_lists.go +++ b/wredis_lists.go @@ -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...)) }) } diff --git a/wredis_lists_test.go b/wredis_lists_test.go index 97cc1ff..ef21a53 100644 --- a/wredis_lists_test.go +++ b/wredis_lists_test.go @@ -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()) @@ -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()) @@ -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))) + }) + }) })