test and fix parsehashkey

This commit is contained in:
Bel LaPointe
2026-02-04 09:39:49 -07:00
parent 930ff1a093
commit 262e432344
2 changed files with 22 additions and 0 deletions

View File

@@ -141,6 +141,7 @@ func parseHashKey(raw []byte) string {
} }
return string(matches[j][1]) return string(matches[j][1])
} }
return string(matches[i][1])
} }
} }
return "" return ""

View File

@@ -3,10 +3,31 @@ package src
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"strings" "strings"
"testing" "testing"
) )
func TestParseHashKey(t *testing.T) {
cases := map[string]string{
"+OK\r\n": "+OK",
"+string\r\n": "+string",
"*1\r\n$4\r\nping\r\n": "ping",
"*2\r\n$3\r\nget\r\n$1\r\nx\r\n": "x",
"*2\r\n$3\r\nset\r\n$1\r\nx\r\n$1\r\ny\r\n": "x",
}
for given, expect := range cases {
given, expect := given, expect
t.Run(fmt.Sprintf("%q", given), func(t *testing.T) {
got := parseHashKey([]byte(given))
if string(got) != expect {
t.Errorf("expected %q=>%q but got =>%q", given, expect, got)
}
})
}
}
func TestReadMessage(t *testing.T) { func TestReadMessage(t *testing.T) {
cases := map[string]string{ cases := map[string]string{
"ok": "+OK\r\n", "ok": "+OK\r\n",