Skip to content

Commit

Permalink
DiceDB#622: Add ECHO command (DiceDB#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgupta12 authored and hash-f committed Sep 22, 2024
1 parent a317a18 commit bfea6e2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
38 changes: 38 additions & 0 deletions integration_tests/commands/echo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package commands

import (
"testing"

"gotest.tools/v3/assert"
)

func TestEcho(t *testing.T) {
conn := getLocalConnection()
defer conn.Close()

testCases := []struct {
name string
commands []string
expected []interface{}
}{
{
name: "ECHO with invalid number of arguments",
commands: []string{"ECHO"},
expected: []interface{}{"ERR wrong number of arguments for 'echo' command"},
},
{
name: "ECHO with one argument",
commands: []string{"ECHO \"hello world\""},
expected: []interface{}{"hello world"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i, cmd := range tc.commands {
result := FireCommand(conn, cmd)
assert.Equal(t, tc.expected[i], result, "Value mismatch for cmd %s", cmd)
}
})
}
}
7 changes: 7 additions & 0 deletions internal/eval/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ var (
Eval: evalPING,
Arity: -1,
}
echoCmdMeta = DiceCmdMeta{
Name: "ECHO",
Info: `ECHO returns the string given as argument.`,
Eval: evalECHO,
Arity: 1,
}
authCmdMeta = DiceCmdMeta{
Name: "AUTH",
Info: `AUTH returns with an encoded "OK" if the user is authenticated.
Expand Down Expand Up @@ -785,6 +791,7 @@ var (

func init() {
DiceCmds["PING"] = pingCmdMeta
DiceCmds["ECHO"] = echoCmdMeta
DiceCmds["AUTH"] = authCmdMeta
DiceCmds["SET"] = setCmdMeta
DiceCmds["GET"] = getCmdMeta
Expand Down
9 changes: 9 additions & 0 deletions internal/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func evalPING(args []string, store *dstore.Store) []byte {
return b
}

// evalECHO returns the argument passed by the user
func evalECHO(args []string, store *dstore.Store) []byte {
if len(args) != 1 {
return diceerrors.NewErrArity("ECHO")
}

return clientio.Encode(args[0], false)
}

// EvalAUTH returns with an encoded "OK" if the user is authenticated
// If the user is not authenticated, it returns with an encoded error message
func EvalAUTH(args []string, c *comm.Client) []byte {
Expand Down
12 changes: 12 additions & 0 deletions internal/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestEval(t *testing.T) {

testEvalMSET(t, store)
testEvalPING(t, store)
testEvalECHO(t, store)
testEvalHELLO(t, store)
testEvalSET(t, store)
testEvalGET(t, store)
Expand Down Expand Up @@ -91,6 +92,17 @@ func testEvalPING(t *testing.T, store *dstore.Store) {
runEvalTests(t, tests, evalPING, store)
}

func testEvalECHO(t *testing.T, store *dstore.Store) {
tests := map[string]evalTestCase{
"nil value": {input: nil, output: []byte("-ERR wrong number of arguments for 'echo' command\r\n")},
"empty args": {input: []string{}, output: []byte("-ERR wrong number of arguments for 'echo' command\r\n")},
"one value": {input: []string{"HEY"}, output: []byte("$3\r\nHEY\r\n")},
"more than one values": {input: []string{"HEY", "HELLO"}, output: []byte("-ERR wrong number of arguments for 'echo' command\r\n")},
}

runEvalTests(t, tests, evalECHO, store)
}

func testEvalHELLO(t *testing.T, store *dstore.Store) {
resp := []interface{}{
"proto", 2,
Expand Down

0 comments on commit bfea6e2

Please sign in to comment.