Skip to content

Commit

Permalink
Merge pull request #10 from iuioiua/refactor-reply-tests
Browse files Browse the repository at this point in the history
refactor: reply tests
  • Loading branch information
iuioiua authored Oct 13, 2022
2 parents 6203609 + 08bcfe0 commit 21be5ce
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 64 deletions.
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export {
assertEquals,
assertRejects,
} from "https://deno.land/std@0.158.0/testing/asserts.ts";
export { StringReader } from "https://deno.land/std@0.158.0/io/readers.ts";

export { connect } from "https://deno.land/x/redis@v0.27.0/redis.ts";
2 changes: 1 addition & 1 deletion src/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function readSimpleString(line: string): string {
}

async function readError(line: string): Promise<never> {
return await Promise.reject(removePrefix(line));
return await Promise.reject(removePrefix(line).slice(4));
}

function readInteger(line: string): number {
Expand Down
71 changes: 71 additions & 0 deletions src/request_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { assertRejects } from "https://deno.land/std@0.158.0/testing/asserts.ts";
import { assertEquals, BufReader, StringReader } from "../deps.ts";
import { readReply, type Reply } from "./reply.ts";

async function readReplyTest(output: string, expected: Reply) {
assertEquals(
await readReply(new BufReader(new StringReader(output))),
expected,
);
}

async function readReplyRejectTest(output: string, expected: string) {
await assertRejects(
async () => await readReply(new BufReader(new StringReader(output))),
expected,
);
}

/** RESP v2 */

Deno.test("simple string", async () => {
await readReplyTest("+OK\r\n", "OK");
});

Deno.test("integer", async () => {
await readReplyTest(":42\r\n", 42);
});

Deno.test("bulk string", async () => {
await readReplyTest("$5\r\nhello\r\n", "hello");
/** Empty bulk string */
await readReplyTest("$0\r\n\r\n", "");
/** Null bulk string */
await readReplyTest("$-1\r\n", null);
});

Deno.test("array", async () => {
await readReplyTest("*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", [
"hello",
"world",
]);
await readReplyTest("*3\r\n:1\r\n:2\r\n:3\r\n", [1, 2, 3]);
/** Empty array */
await readReplyTest("*0\r\n", []);
/** Null array */
await readReplyTest("*-1\r\n", null);
/** Null elements in array */
await readReplyTest("*3\r\n$5\r\nhello\r\n$-1\r\n$5\r\nworld\r\n", [
"hello",
null,
"world",
]);
});

Deno.test("simple error", async () => {
await readReplyRejectTest(
"-ERR this is the error description\r\n",
"this is the error description",
);
});

/** RESP3 */

Deno.test("null", async () => {
await readReplyTest("_\r\n", null);
});

Deno.test("boolean", async () => {
await readReplyTest("#t\r\n", true);
await readReplyTest("#f\r\n", false);
});
63 changes: 0 additions & 63 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,6 @@ async function sendCommandTest(
assertEquals(await sendCommand(redisConn, command), expected);
}

Deno.test("RESP v2", async (t) => {
await t.step(
"simple string",
async () => await sendCommandTest(["PING"], "PONG"),
);

await t.step("error", async () => {
await assertRejects(async () =>
await sendCommand(redisConn, ["helloworld"])
);
});

await t.step("integer", async () => {
await sendCommandTest(["INCR", "integer"], 1);
await sendCommandTest(["INCR", "integer"], 2);
});

await t.step("bulk string", async () => {
await sendCommand(redisConn, ["SET", "big ups", "west side massive"]);
await sendCommandTest(["GET", "big ups"], "west side massive");
});

await t.step("null", async () => {
await sendCommandTest(["GET", "nonexistant"], null);
});

await t.step("array", async () => {
await sendCommand(redisConn, [
"HSET",
"hash",
"hello",
"world",
"integer",
13,
]);
await sendCommandTest([
"HMGET",
"hash",
"hello",
"integer",
"nonexistant",
], ["world", "13", null]);
/** Empty array */
await sendCommandTest(["HGETALL", "nonexistant"], []);
/** Null array */
await sendCommandTest(["BLPOP", "list", 1], null);
});
});

Deno.test("methods", async (t) => {
await t.step("transactions", async () => {
await sendCommandTest(["MULTI"], "OK");
Expand Down Expand Up @@ -106,20 +57,6 @@ Deno.test("methods", async (t) => {
});
});

Deno.test("RESP3", async (t) => {
await sendCommand(redisConn, ["HELLO", 3]);

await t.step("boolean", async () => {
await sendCommandTest(["EVAL", "redis.setresp(3); return true", 0], true);
await sendCommandTest(["EVAL", "redis.setresp(3); return false", 0], false);
});

await t.step(
"null",
async () => await sendCommandTest(["GET", "null"], null),
);
});

/** This test must be last */
Deno.test(
"cleanup",
Expand Down

0 comments on commit 21be5ce

Please sign in to comment.