summaryrefslogtreecommitdiff
path: root/bufio_test.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-11-07 23:19:08 -0500
committerRyan Dahl <ry@tinyclouds.org>2018-11-07 23:19:08 -0500
commit423424f1daa3ca35f2ac63d2c0e40b99e938ec46 (patch)
tree0c7c12fb3cf1f6005827ce5f759a9efe66f74c6f /bufio_test.ts
parent8396619721bed2eaa78cab17754b63b567accfef (diff)
Add BufReader.readSlice()
Original: https://github.com/denoland/deno_std/commit/e37b949e2c4523911e071086c1cf4ea4f33dc6af
Diffstat (limited to 'bufio_test.ts')
-rw-r--r--bufio_test.ts26
1 files changed, 22 insertions, 4 deletions
diff --git a/bufio_test.ts b/bufio_test.ts
index 85be74fdc..9a3361d6f 100644
--- a/bufio_test.ts
+++ b/bufio_test.ts
@@ -5,9 +5,10 @@
import * as deno from "deno";
import { test, assertEqual } from "http://deno.land/x/testing/testing.ts";
-import { BufReader } from "./bufio.ts";
+import { BufReader, BufState } from "./bufio.ts";
import { Buffer } from "./buffer.ts";
import * as iotest from "./iotest.ts";
+import { charCode } from "./util.ts";
async function readBytes(buf: BufReader): Promise<string> {
const b = new Uint8Array(1000);
@@ -42,7 +43,7 @@ type ReadMaker = { name: string; fn: (r: deno.Reader) => deno.Reader };
const readMakers: ReadMaker[] = [
{ name: "full", fn: r => r },
{ name: "byte", fn: r => new iotest.OneByteReader(r) },
- { name: "half", fn: r => new iotest.HalfReader(r) },
+ { name: "half", fn: r => new iotest.HalfReader(r) }
// TODO { name: "data+err", r => new iotest.DataErrReader(r) },
// { name: "timeout", fn: r => new iotest.TimeoutReader(r) },
];
@@ -50,7 +51,7 @@ const readMakers: ReadMaker[] = [
function readLines(b: BufReader): string {
let s = "";
while (true) {
- let s1 = b.readString('\n');
+ let s1 = b.readString("\n");
if (s1 == null) {
break; // EOF
}
@@ -83,7 +84,7 @@ const bufreaders: NamedBufReader[] = [
{ name: "4", fn: (b: BufReader) => reads(b, 4) },
{ name: "5", fn: (b: BufReader) => reads(b, 5) },
{ name: "7", fn: (b: BufReader) => reads(b, 7) },
- { name: "bytes", fn: readBytes },
+ { name: "bytes", fn: readBytes }
// { name: "lines", fn: readLines },
];
@@ -128,3 +129,20 @@ test(async function bufioBufReader() {
}
}
});
+
+test(async function bufioBufferFull() {
+ const longString =
+ "And now, hello, world! It is the time for all good men to come to the aid of their party";
+ const buf = new BufReader(stringsReader(longString), MIN_READ_BUFFER_SIZE);
+ let [line, state] = await buf.readSlice(charCode("!"));
+
+ const decoder = new TextDecoder();
+ let actual = decoder.decode(line);
+ assertEqual(state, BufState.BufferFull);
+ assertEqual(actual, "And now, hello, ");
+
+ [line, state] = await buf.readSlice(charCode("!"));
+ actual = decoder.decode(line);
+ assertEqual(actual, "world!");
+ assertEqual(state, BufState.Ok);
+});