go-examples/example-redis/main_test.go

89 lines
1.4 KiB
Go
Raw Normal View History

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
cmd string
key string
val string
}
func TestParseCommand(t *testing.T) {
tests := []ParseCmdTest{
{"test", true, "", "", ""},
{"test test", true, "", "", ""},
{"SET 1 2 3", true, "", "", ""},
{"GET 1 2", true, "", "", ""},
}
for _, test := range tests {
c, k, v, 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 != test.cmd {
t.Log("Error command", c, test.cmd)
t.Fail()
}
if k != test.key {
t.Log("Error key", k, test.key)
t.Fail()
}
if v != test.val {
t.Log("Error value", v, test.val)
t.Fail()
}
}
}
}