diff --git a/encoding/ascii.go b/encoding/ascii.go index 2557e43..7f4b083 100644 --- a/encoding/ascii.go +++ b/encoding/ascii.go @@ -4,10 +4,11 @@ import "bytes" type ASCIICoder struct{} -func (c *ASCIICoder) Encode(s string, buf *bytes.Buffer) { +func (c *ASCIICoder) Encode(s string, buf *bytes.Buffer) error { buf.WriteString(s) + return nil } -func (c *ASCIICoder) Decode(buf *bytes.Buffer) string { - return buf.String() +func (c *ASCIICoder) Decode(buf *bytes.Buffer) (string, error) { + return buf.String(), nil } diff --git a/encoding/ascii_test.go b/encoding/ascii_test.go index d28f93b..bea47c9 100644 --- a/encoding/ascii_test.go +++ b/encoding/ascii_test.go @@ -11,7 +11,11 @@ func TestASCIIEncodeSimpleASCIIString(t *testing.T) { input := "Hello, World!" expected := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33} - coder.Encode(input, &buf) + err := coder.Encode(input, &buf) + + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } if !bytes.Equal(buf.Bytes(), expected) { t.Errorf("Expected %v, but got %v", expected, buf.Bytes()) @@ -25,7 +29,11 @@ func TestASCIIDecodeSimpleASCIIString(t *testing.T) { expected := "Hello, World!" buf.Write(input) - output := coder.Decode(&buf) + output, err := coder.Decode(&buf) + + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } if output != expected { t.Errorf("Expected %v, but got %v", expected, output) @@ -38,7 +46,11 @@ func TestASCIIEncodeEmptyString(t *testing.T) { input := "" expected := []byte{} - coder.Encode(input, &buf) + err := coder.Encode(input, &buf) + + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } if !bytes.Equal(buf.Bytes(), expected) { t.Errorf("Expected %v, but got %v", expected, buf.Bytes()) @@ -50,7 +62,11 @@ func TestASCIIDecodeEmptyString(t *testing.T) { buf := bytes.NewBuffer([]byte{}) expected := "" - output := coder.Decode(buf) + output, err := coder.Decode(buf) + + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } if output != expected { t.Errorf("Expected %v, but got %v", expected, output) diff --git a/encoding/coder.go b/encoding/coder.go index 7f2c39b..3a1ac50 100644 --- a/encoding/coder.go +++ b/encoding/coder.go @@ -3,6 +3,6 @@ package encoding import "bytes" type Coder interface { - Encode(s string, buf *bytes.Buffer) - Decode(buf *bytes.Buffer) string + Encode(s string, buf *bytes.Buffer) error + Decode(buf *bytes.Buffer) (string, error) } \ No newline at end of file