package memalpha

import (
	"bufio"
	"bytes"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestServerError(t *testing.T) {
	errorMessage := "test fake"
	response := bytes.NewReader([]byte("SERVER_ERROR " + errorMessage))
	request := bytes.NewBuffer([]byte{})

	serverReadWriter := bufio.NewReadWriter(bufio.NewReader(response), bufio.NewWriter(request))

	c := &Client{rw: serverReadWriter}

	err := c.Set("foo", []byte("bar"))
	e, ok := err.(ServerError)
	if ok && strings.Contains(e.Error(), "server error: "+errorMessage) {
		return
	}

	t.Fatalf("set(foo): Error = %v, want ServerError: test fake", err)
}

func TestClientError(t *testing.T) {
	errorMessage := "test fake"
	response := bytes.NewReader([]byte("CLIENT_ERROR " + errorMessage))
	request := bytes.NewBuffer([]byte{})

	serverReadWriter := bufio.NewReadWriter(bufio.NewReader(response), bufio.NewWriter(request))

	c := &Client{rw: serverReadWriter}

	err := c.Set("foo", []byte("bar"))
	e, ok := err.(ClientError)
	if ok && strings.Contains(e.Error(), "client error: "+errorMessage) {
		return
	}

	t.Fatalf("set(foo): Error = %v, want ClientError: test fake", err)
}

func TestReplyError(t *testing.T) {
	response := bytes.NewReader([]byte("ERROR"))
	request := bytes.NewBuffer([]byte{})

	serverReadWriter := bufio.NewReadWriter(bufio.NewReader(response), bufio.NewWriter(request))

	c := &Client{rw: serverReadWriter}

	err := c.Set("foo", []byte("bar"))
	assert.Equal(t, err, ErrReplyError)
}

package memalpha

import (
	"bufio"
	"bytes"
	"strings"
	"testing"
)

func TestHandleServerError(t *testing.T) {
	errorMessage := "test fake"
	response := bytes.NewReader([]byte("SERVER_ERROR " + errorMessage))
	request := bytes.NewBuffer([]byte{})

	serverReadWriter := bufio.NewReadWriter(bufio.NewReader(response), bufio.NewWriter(request))

	c := &Client{rw: serverReadWriter}

	err := c.Set("foo", []byte("bar"))
	e, ok := err.(ServerError)
	if ok && strings.Contains(e.Error(), "server error: "+errorMessage) {
		return
	}

	t.Fatalf("set(foo): Error = %v, want ServerError: test fake", err)
}

--- 
+++ 
@@ -5,9 +5,11 @@
 	"bytes"
 	"strings"
 	"testing"
+
+	"github.com/stretchr/testify/assert"
 )
 
-func TestHandleServerError(t *testing.T) {
+func TestServerError(t *testing.T) {
 	errorMessage := "test fake"
 	response := bytes.NewReader([]byte("SERVER_ERROR " + errorMessage))
 	request := bytes.NewBuffer([]byte{})
@@ -24,3 +26,33 @@
 
 	t.Fatalf("set(foo): Error = %v, want ServerError: test fake", err)
 }
+
+func TestClientError(t *testing.T) {
+	errorMessage := "test fake"
+	response := bytes.NewReader([]byte("CLIENT_ERROR " + errorMessage))
+	request := bytes.NewBuffer([]byte{})
+
+	serverReadWriter := bufio.NewReadWriter(bufio.NewReader(response), bufio.NewWriter(request))
+
+	c := &Client{rw: serverReadWriter}
+
+	err := c.Set("foo", []byte("bar"))
+	e, ok := err.(ClientError)
+	if ok && strings.Contains(e.Error(), "client error: "+errorMessage) {
+		return
+	}
+
+	t.Fatalf("set(foo): Error = %v, want ClientError: test fake", err)
+}
+
+func TestReplyError(t *testing.T) {
+	response := bytes.NewReader([]byte("ERROR"))
+	request := bytes.NewBuffer([]byte{})
+
+	serverReadWriter := bufio.NewReadWriter(bufio.NewReader(response), bufio.NewWriter(request))
+
+	c := &Client{rw: serverReadWriter}
+
+	err := c.Set("foo", []byte("bar"))
+	assert.Equal(t, err, ErrReplyError)
+}
Add test of ClientError and ReplyError
