diff options
| author | Ryan Dahl <ry@tinyclouds.org> | 2018-11-07 13:16:07 -0500 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2018-11-07 13:16:07 -0500 |
| commit | 8610e3578c923be2b7d758e75ea370801abf8574 (patch) | |
| tree | 605c8b5f400c6a0daf29282250573816341e2cef /bufio_test.ts | |
| parent | abe47d10c97f5cff671d3565a5a985c2ef203d4d (diff) | |
First pass at bufio.
Original: https://github.com/denoland/deno_std/commit/c5cc6959705c310f4f7a864d77aae54171707c04
Diffstat (limited to 'bufio_test.ts')
| -rw-r--r-- | bufio_test.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/bufio_test.ts b/bufio_test.ts new file mode 100644 index 000000000..a66052ba1 --- /dev/null +++ b/bufio_test.ts @@ -0,0 +1,32 @@ +import * as deno from "deno"; +import { test, assertEqual } from "http://deno.land/x/testing/testing.ts"; +import * as bufio from "./bufio.ts"; +import { Buffer } from "./buffer.ts"; + +async function readBytes(buf: bufio.Reader): Promise<string> { + const b = new Uint8Array(1000); + let nb = 0; + while (true) { + let c = await buf.readByte(); + if (c < 0) { + break; // EOF + } + b[nb] = c; + nb++; + } + const decoder = new TextDecoder(); + return decoder.decode(b.subarray(0, nb)); +} + +function stringsReader(s: string): deno.Reader { + const encoder = new TextEncoder(); + const ui8 = encoder.encode(s); + return new Buffer(ui8.buffer as ArrayBuffer); +} + +test(async function bufioReaderSimple() { + const data = "hello world"; + const b = new bufio.Reader(stringsReader(data)); + const s = await readBytes(b); + assertEqual(s, data); +}); |
