87 lines
1.5 KiB
Go
87 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetCommand(t *testing.T) {
|
|
r := strings.NewReader("test\n")
|
|
line, err := getCommand(r)
|
|
if err != nil {
|
|
t.Log("Should not err")
|
|
t.Fail()
|
|
}
|
|
if line != "test" {
|
|
t.Log("Invalid command")
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestIsCommand(t *testing.T) {
|
|
tests := map[string]bool{
|
|
"GET": true,
|
|
"SET": true,
|
|
"DEL": true,
|
|
"XXX": false,
|
|
}
|
|
for k, v := range tests {
|
|
if isCommand(k) != v {
|
|
t.Log("Error " + k)
|
|
t.Fail()
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRespond(t *testing.T) {
|
|
var bs []byte
|
|
buf := bytes.NewBuffer(bs)
|
|
respond(buf, "test")
|
|
if buf.String() != "test\n" {
|
|
t.Log("Invalid response (" + buf.String() + ")")
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
type ParseCmdTest struct {
|
|
line string
|
|
fails bool
|
|
RedisCmd
|
|
}
|
|
|
|
func TestParseCommand(t *testing.T) {
|
|
tests := []ParseCmdTest{
|
|
{"test", true, RedisCmd{"", "", ""}},
|
|
{"test test", true, RedisCmd{"", "", ""}},
|
|
{"SET 1 2 3", true, RedisCmd{"", "", ""}},
|
|
{"GET 1 2", true, RedisCmd{"", "", ""}},
|
|
}
|
|
for _, test := range tests {
|
|
c, e := parseCmd(test.line)
|
|
if test.fails {
|
|
if e == nil {
|
|
t.Log("should fail (" + test.line + ")")
|
|
t.Fail()
|
|
}
|
|
} else {
|
|
if e != nil {
|
|
t.Log("should no fail", e)
|
|
t.Fail()
|
|
}
|
|
if c.cmd != test.cmd {
|
|
t.Log("Error command", c.cmd, test.cmd)
|
|
t.Fail()
|
|
}
|
|
if c.key != test.key {
|
|
t.Log("Error key", c.key, test.key)
|
|
t.Fail()
|
|
}
|
|
if c.val != test.val {
|
|
t.Log("Error value", c.val, test.val)
|
|
t.Fail()
|
|
}
|
|
}
|
|
}
|
|
}
|