-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |