Skip to content

Commit

Permalink
feat: command - lolwut
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Jun 6, 2024
1 parent 1bc2e54 commit c6b4e5c
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func Parse(args [][]byte) (redis.Cmd, error) {
return key.ParseFlushDB(b)
case "info":
return server.ParseOK(b)
case "lolwut":
return server.ParseLolwut(b)

// connection
case "echo":
Expand Down
64 changes: 64 additions & 0 deletions internal/command/server/lolwut.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package server

import (
"math/rand"

"github.com/nalgeon/redka/internal/parser"
"github.com/nalgeon/redka/internal/redis"
)

var lolwutAnswers = []string{
// yes
"As I see it, yes",
"It is certain",
"It is decidedly so",
"Most likely",
"Outlook good",
"Signs point to yes",
"Without a doubt",
"Yes definitely",
"Yes",
"You may rely on it",
// maybe
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Reply hazy, try again",
// no
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
}

// Answers any question you throw at it
// with magic ⋆。𖦹°⭒˚。⋆
// LOLWUT [question...]
type Lolwut struct {
redis.BaseCmd
parts []string
}

func ParseLolwut(b redis.BaseCmd) (*Lolwut, error) {
cmd := &Lolwut{BaseCmd: b}
err := parser.New(
parser.Strings(&cmd.parts),
).Required(0).Run(cmd.Args())
if err != nil {
return cmd, err
}
return cmd, nil
}

func (c *Lolwut) Run(w redis.Writer, _ redis.Redka) (any, error) {
var answer string
if len(c.parts) != 0 {
answer = lolwutAnswers[rand.Intn(len(lolwutAnswers))]
} else {
answer = "Ask me a question (⊃。•́‿•̀。)⊃"
}
w.WriteBulkString(answer + "\n")
return answer, nil
}
60 changes: 60 additions & 0 deletions internal/command/server/lolwut_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package server

import (
"strings"
"testing"

"github.com/nalgeon/redka/internal/redis"
"github.com/nalgeon/redka/internal/testx"
)

func TestLolwutParse(t *testing.T) {
tests := []struct {
cmd string
err error
}{
{
cmd: "lolwut",
err: nil,
},
{
cmd: "lolwut you ok?",
err: nil,
},
{
cmd: "lolwut is redis cool?",
err: nil,
},
}

for _, test := range tests {
t.Run(test.cmd, func(t *testing.T) {
_, err := redis.Parse(ParseLolwut, test.cmd)
testx.AssertEqual(t, err, test.err)
})
}
}

func TestLolwutExec(t *testing.T) {
t.Run("lolwut", func(t *testing.T) {
db, red := getDB(t)
defer db.Close()

cmd := redis.MustParse(ParseLolwut, "lolwut you ok?")
conn := redis.NewFakeConn()
_, err := cmd.Run(conn, red)
testx.AssertNoErr(t, err)
testx.AssertEqual(t, len(conn.Out()) >= 3, true)
})

t.Run("empty", func(t *testing.T) {
db, red := getDB(t)
defer db.Close()

cmd := redis.MustParse(ParseLolwut, "lolwut")
conn := redis.NewFakeConn()
_, err := cmd.Run(conn, red)
testx.AssertNoErr(t, err)
testx.AssertEqual(t, strings.HasPrefix(conn.Out(), "Ask me a question"), true)
})
}

0 comments on commit c6b4e5c

Please sign in to comment.