Created writer mock.

Created mock server.
Added test for ping command plugin.
This commit is contained in:
Kelvin Clement Mwinuka
2023-07-13 18:37:53 +08:00
parent 91e1ba5555
commit b5cadf43a7
3 changed files with 86 additions and 2 deletions

View File

@@ -0,0 +1,33 @@
package main
import (
"bufio"
"strings"
"testing"
"github.com/kelvinmwinuka/memstore/utils"
)
func TestHandleCommand(t *testing.T) {
server := &utils.MockServer{}
cw := &utils.CustomWriter{}
writer := bufio.NewWriter(cw)
tests := []struct {
cmd []string
expected string
}{
{[]string{"ping"}, "+PONG\r\n\n"},
{[]string{"ping", "Ping Test"}, "+Ping Test\r\n\n"},
{[]string{"ping", "Ping Test", "Error"}, "-Error wrong number of arguments for PING command\r\n\n"},
}
for _, tt := range tests {
cw.Buf.Reset()
Plugin.HandleCommand(tt.cmd, server, writer)
if tt.expected != cw.Buf.String() {
t.Errorf("Expected %s, Got %s", strings.TrimSpace(tt.expected), strings.TrimSpace(cw.Buf.String()))
}
}
}