2017-07-09 14:50:55 +00:00
|
|
|
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
|
2017-07-09 16:46:32 +00:00
|
|
|
RedisCmd
|
2017-07-09 14:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseCommand(t *testing.T) {
|
|
|
|
tests := []ParseCmdTest{
|
2017-07-09 16:46:32 +00:00
|
|
|
{"test", true, RedisCmd{"", "", ""}},
|
|
|
|
{"test test", true, RedisCmd{"", "", ""}},
|
|
|
|
{"SET 1 2 3", true, RedisCmd{"", "", ""}},
|
|
|
|
{"GET 1 2", true, RedisCmd{"", "", ""}},
|
2017-07-09 14:50:55 +00:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2017-07-09 16:46:32 +00:00
|
|
|
c, e := parseCmd(test.line)
|
2017-07-09 14:50:55 +00:00
|
|
|
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()
|
|
|
|
}
|
2017-07-09 16:46:32 +00:00
|
|
|
if c.cmd != test.cmd {
|
|
|
|
t.Log("Error command", c.cmd, test.cmd)
|
2017-07-09 14:50:55 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2017-07-09 16:46:32 +00:00
|
|
|
if c.key != test.key {
|
|
|
|
t.Log("Error key", c.key, test.key)
|
2017-07-09 14:50:55 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
2017-07-09 16:46:32 +00:00
|
|
|
if c.val != test.val {
|
|
|
|
t.Log("Error value", c.val, test.val)
|
2017-07-09 14:50:55 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|