From 151ce0266eb4de2c8fc600c81c192a5f791b6169 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 9 Oct 2019 17:10:09 -0400 Subject: Move everything into std subdir --- std/util/async_test.ts | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 std/util/async_test.ts (limited to 'std/util/async_test.ts') diff --git a/std/util/async_test.ts b/std/util/async_test.ts new file mode 100644 index 000000000..adaac1e22 --- /dev/null +++ b/std/util/async_test.ts @@ -0,0 +1,73 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { test, runIfMain } from "../testing/mod.ts"; +import { assert, assertEquals, assertStrictEq } from "../testing/asserts.ts"; +import { collectUint8Arrays, deferred, MuxAsyncIterator } from "./async.ts"; + +test(async function asyncDeferred(): Promise { + const d = deferred(); + d.resolve(12); +}); + +async function* gen123(): AsyncIterableIterator { + yield 1; + yield 2; + yield 3; +} + +async function* gen456(): AsyncIterableIterator { + yield 4; + yield 5; + yield 6; +} + +test(async function asyncMuxAsyncIterator(): Promise { + const mux = new MuxAsyncIterator(); + mux.add(gen123()); + mux.add(gen456()); + const results = new Set(); + for await (const value of mux) { + results.add(value); + } + assertEquals(results.size, 6); +}); + +test(async function collectUint8Arrays0(): Promise { + async function* gen(): AsyncIterableIterator {} + const result = await collectUint8Arrays(gen()); + assert(result instanceof Uint8Array); + assertEquals(result.length, 0); +}); + +test(async function collectUint8Arrays0(): Promise { + async function* gen(): AsyncIterableIterator {} + const result = await collectUint8Arrays(gen()); + assert(result instanceof Uint8Array); + assertStrictEq(result.length, 0); +}); + +test(async function collectUint8Arrays1(): Promise { + const buf = new Uint8Array([1, 2, 3]); + async function* gen(): AsyncIterableIterator { + yield buf; + } + const result = await collectUint8Arrays(gen()); + assertStrictEq(result, buf); + assertStrictEq(result.length, 3); +}); + +test(async function collectUint8Arrays4(): Promise { + async function* gen(): AsyncIterableIterator { + yield new Uint8Array([1, 2, 3]); + yield new Uint8Array([]); + yield new Uint8Array([4, 5]); + yield new Uint8Array([6]); + } + const result = await collectUint8Arrays(gen()); + assert(result instanceof Uint8Array); + assertStrictEq(result.length, 6); + for (let i = 0; i < 6; i++) { + assertStrictEq(result[i], i + 1); + } +}); + +runIfMain(import.meta); -- cgit v1.2.3