All files / streams / _test_utils.ts

100.00% Branches 1/1
100.00% Lines 16/16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
 
x2
 
 
 
 
 
 
 
x2
x2
x2
x2
 
x17
x17
x17
x17
x133
x133
x133
x133
x17
x17
x17
























// Copyright 2018-2025 the Deno authors. MIT license.

import { assert, assertEquals } from "@std/assert";

/**
 * Verify that a transform stream produces the expected output data
 * @param transform The transform stream to test
 * @param inputs Source input data
 * @param outputs Expected output data
 */
export async function testTransformStream<T, U>(
  transform: TransformStream<T, U>,
  inputs: Iterable<T> | AsyncIterable<T>,
  outputs: Iterable<U> | AsyncIterable<U>,
) {
  const reader = ReadableStream.from(inputs)
    .pipeThrough(transform)
    .getReader();
  for await (const output of outputs) {
    const { value, done } = await reader.read();
    assertEquals(value, output);
    assertEquals(done, false);
  }
  const f = await reader.read();
  assert(f.done, `stream not done, value was: ${f.value}`);
}