test parse redis msg

This commit is contained in:
Bel LaPointe
2026-02-04 09:28:35 -07:00
parent d19d4d6b3e
commit 3382608da3
2 changed files with 44 additions and 1 deletions

View File

@@ -136,7 +136,7 @@ func _readMessageTo(w io.Writer, reader *bufio.Reader) error {
}
fmt.Fprintf(w, "%s", nextLine)
return err
case '*', '~', '%': // *=array %=map, like *-1 for nil, like *4 for [1,2,3,4]
case '*', '~', '%': // *=array ~=set %=map, like *-1 for nil, like *4 for [1,2,3,4]
n, err := strconv.Atoi(string(firstLine[1:]))
if err != nil {
return fmt.Errorf("arr not a num: %q: %w", firstLine, err)

43
src/adapt_test.go Normal file
View File

@@ -0,0 +1,43 @@
package src
import (
"bufio"
"bytes"
"strings"
"testing"
)
func TestReadMessage(t *testing.T) {
cases := map[string]string{
"ok": "+OK\r\n",
"simple string": "+my string\r\n",
"error": "-my err\r\n",
"number": ":1\r\n",
"negative number": ":-1\r\n",
"positive number": ":+1\r\n",
"nil string": "$-1\r\n",
"long string": "$100\r\n" + strings.Repeat("a", 100) + "\r\n",
"longer string": "$10000\r\n" + strings.Repeat("a", 10000) + "\r\n",
"zero array": "*0\r\n",
"array": "*3\r\n+hello\r\n:world\r\n$1\r\n!\r\n",
"set": "~3\r\n+hello\r\n:world\r\n$1\r\n!\r\n",
"map": "%1\r\n+foo\r\n+bar\r\n",
"nil": "_\r\n",
"boolean": "#t\r\n",
"double": ",1.5\r\n",
// "empty": "", (eof)
}
for name, payload := range cases {
payload := payload
t.Run(name, func(t *testing.T) {
got, err := readMessage(bufio.NewReader(strings.NewReader(payload)))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, []byte(payload)) {
t.Errorf("expected %q but got %q", payload, got)
}
})
}
}