-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from iuioiua/refactor-reply-tests
refactor: reply tests
- Loading branch information
Showing
4 changed files
with
73 additions
and
64 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
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,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); | ||
}); |
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