summaryrefslogtreecommitdiff
path: root/cli/js/tests/files_test.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-04-22 21:30:45 +0200
committerGitHub <noreply@github.com>2020-04-22 21:30:45 +0200
commit68d287eed5dcdc7b84a317fa90c4c9131389c0b8 (patch)
tree2d6c623858ef89504f26a49ffd169e1d2b2e8453 /cli/js/tests/files_test.ts
parentda6819a14c54d1a2221b37f00b3302eb2d50660b (diff)
BREAKING CHANGE: rename Deno.toAsyncIterator() to Deno.iter() (#4848)
* rename Deno.toAsyncIterator() to Deno.iter() * adds sync version Deno.iterSync() * adds optional second argument for buffer size
Diffstat (limited to 'cli/js/tests/files_test.ts')
-rw-r--r--cli/js/tests/files_test.ts91
1 files changed, 87 insertions, 4 deletions
diff --git a/cli/js/tests/files_test.ts b/cli/js/tests/files_test.ts
index 1f7352afc..f68e5def4 100644
--- a/cli/js/tests/files_test.ts
+++ b/cli/js/tests/files_test.ts
@@ -25,23 +25,71 @@ unitTest({ perms: { read: true } }, async function filesCopyToStdout(): Promise<
file.close();
});
+unitTest({ perms: { read: true } }, async function filesIter(): Promise<void> {
+ const filename = "cli/tests/hello.txt";
+ const file = await Deno.open(filename);
+
+ let totalSize = 0;
+ for await (const buf of Deno.iter(file)) {
+ totalSize += buf.byteLength;
+ }
+
+ assertEquals(totalSize, 12);
+ file.close();
+});
+
unitTest(
{ perms: { read: true } },
- async function filesToAsyncIterator(): Promise<void> {
+ async function filesIterCustomBufSize(): Promise<void> {
const filename = "cli/tests/hello.txt";
const file = await Deno.open(filename);
let totalSize = 0;
- for await (const buf of Deno.toAsyncIterator(file)) {
+ let iterations = 0;
+ for await (const buf of Deno.iter(file, 6)) {
+ totalSize += buf.byteLength;
+ iterations += 1;
+ }
+
+ assertEquals(totalSize, 12);
+ assertEquals(iterations, 2);
+ file.close();
+ }
+);
+
+unitTest({ perms: { read: true } }, function filesIterSync(): void {
+ const filename = "cli/tests/hello.txt";
+ const file = Deno.openSync(filename);
+
+ let totalSize = 0;
+ for (const buf of Deno.iterSync(file)) {
+ totalSize += buf.byteLength;
+ }
+
+ assertEquals(totalSize, 12);
+ file.close();
+});
+
+unitTest(
+ { perms: { read: true } },
+ function filesIterSyncCustomBufSize(): void {
+ const filename = "cli/tests/hello.txt";
+ const file = Deno.openSync(filename);
+
+ let totalSize = 0;
+ let iterations = 0;
+ for (const buf of Deno.iterSync(file, 6)) {
totalSize += buf.byteLength;
+ iterations += 1;
}
assertEquals(totalSize, 12);
+ assertEquals(iterations, 2);
file.close();
}
);
-unitTest(async function readerToAsyncIterator(): Promise<void> {
+unitTest(async function readerIter(): Promise<void> {
// ref: https://github.com/denoland/deno/issues/2330
const encoder = new TextEncoder();
@@ -69,7 +117,42 @@ unitTest(async function readerToAsyncIterator(): Promise<void> {
const reader = new TestReader("hello world!");
let totalSize = 0;
- for await (const buf of Deno.toAsyncIterator(reader)) {
+ for await (const buf of Deno.iter(reader)) {
+ totalSize += buf.byteLength;
+ }
+
+ assertEquals(totalSize, 12);
+});
+
+unitTest(async function readerIterSync(): Promise<void> {
+ // ref: https://github.com/denoland/deno/issues/2330
+ const encoder = new TextEncoder();
+
+ class TestReader implements Deno.SyncReader {
+ #offset = 0;
+ #buf: Uint8Array;
+
+ constructor(s: string) {
+ this.#buf = new Uint8Array(encoder.encode(s));
+ }
+
+ readSync(p: Uint8Array): number | Deno.EOF {
+ const n = Math.min(p.byteLength, this.#buf.byteLength - this.#offset);
+ p.set(this.#buf.slice(this.#offset, this.#offset + n));
+ this.#offset += n;
+
+ if (n === 0) {
+ return Deno.EOF;
+ }
+
+ return n;
+ }
+ }
+
+ const reader = new TestReader("hello world!");
+
+ let totalSize = 0;
+ for await (const buf of Deno.iterSync(reader)) {
totalSize += buf.byteLength;
}