Skip to content

Commit

Permalink
Add the RPop command
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfowler committed Apr 6, 2016
1 parent 2617219 commit 1e15938
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions wredis_lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ func (w *Wredis) LPush(key string, items ...string) (int64, error) {
return redis.Int64(conn.Do("LPUSH", args...))
})
}

// RPop removes and returns the last element of the list stored at key. For more
// information, see http://redis.io/commands/rpop.
func (w *Wredis) RPop(key string) (string, error) {
if key == "" {
return stringError("key cannot be empty")
}
return w.ExecString(func(conn redis.Conn) (string, error) {
return redis.String(conn.Do("RPOP", key))
})
}
28 changes: 28 additions & 0 deletions wredis_lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,32 @@ var _ = Describe("WredisLists", func() {
Ω(n).Should(Equal(int64(4)))
})
})

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

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

It("should return an error when popping from an empty list", func() {
_, err := safe.RPop(testList)
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("redigo: nil returned"))
})

It("should return the last item in a list", func() {
n, err := safe.LPush(testList, "1", "2", "3")
Ω(err).ShouldNot(HaveOccurred())
Ω(n).Should(Equal(int64(n)))

i, err := safe.RPop(testList)
Ω(err).ShouldNot(HaveOccurred())
Ω(i).Should(Equal("1"))
})
})
})

0 comments on commit 1e15938

Please sign in to comment.