Skip to content

Commit

Permalink
Better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dim committed Dec 1, 2017
1 parent de1fd29 commit f56fa48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
8 changes: 4 additions & 4 deletions redeo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ = Describe("Ping", func() {

w = redeotest.NewRecorder()
subject.ServeRedeo(w, resp.NewCommand("PING", resp.CommandArgument("bad"), resp.CommandArgument("args")))
Expect(w.Response()).To(Equal("ERR wrong number of arguments for 'PING' command"))
Expect(w.Response()).To(MatchError("ERR wrong number of arguments for 'PING' command"))
})

})
Expand Down Expand Up @@ -61,19 +61,19 @@ var _ = Describe("SubCommands", func() {
It("should fail on calls without a sub", func() {
w := redeotest.NewRecorder()
subject.ServeRedeo(w, resp.NewCommand("CUSTOM"))
Expect(w.Response()).To(Equal("ERR wrong number of arguments for 'CUSTOM' command"))
Expect(w.Response()).To(MatchError("ERR wrong number of arguments for 'CUSTOM' command"))
})

It("should fail on calls with an unknown sub", func() {
w := redeotest.NewRecorder()
subject.ServeRedeo(w, resp.NewCommand("CUSTOM", resp.CommandArgument("missing")))
Expect(w.Response()).To(Equal("ERR Unknown custom subcommand 'missing'"))
Expect(w.Response()).To(MatchError("ERR Unknown custom subcommand 'missing'"))
})

It("should fail on calls with invalid args", func() {
w := redeotest.NewRecorder()
subject.ServeRedeo(w, resp.NewCommand("CUSTOM", resp.CommandArgument("echo")))
Expect(w.Response()).To(Equal("ERR wrong number of arguments for 'CUSTOM echo' command"))
Expect(w.Response()).To(MatchError("ERR wrong number of arguments for 'CUSTOM echo' command"))
})

It("should succeed", func() {
Expand Down
21 changes: 16 additions & 5 deletions redeotest/redeotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/bsm/redeo/resp"
)

// ErrorResponse is used to wrap error strings
type ErrorResponse string

// Error implements error interface
func (e ErrorResponse) Error() string { return string(e) }

// ResponseRecorder is an implementation of resp.ResponseWriter that
// is helpful in tests.
type ResponseRecorder struct {
Expand Down Expand Up @@ -44,10 +50,11 @@ func (r *ResponseRecorder) Quoted() string {

// Response returns the first response
func (r *ResponseRecorder) Response() (interface{}, error) {
_ = r.ResponseWriter.Flush()

rr := resp.NewResponseReader(bytes.NewReader(r.b.Bytes()))
return parseResult(rr)
vv, err := r.Responses()
if err != nil || len(vv) == 0 {
return nil, err
}
return vv[len(vv)-1], nil
}

// Responses returns all responses
Expand Down Expand Up @@ -81,7 +88,11 @@ func parseResult(rr resp.ResponseReader) (interface{}, error) {
case resp.TypeInt:
return rr.ReadInt()
case resp.TypeError:
return rr.ReadError()
s, err := rr.ReadError()
if err != nil {
return nil, err
}
return ErrorResponse(s), nil
case resp.TypeNil:
return nil, rr.ReadNil()
case resp.TypeArray:
Expand Down

0 comments on commit f56fa48

Please sign in to comment.