1// META: global=window,worker
2// META: script=resources/readable-stream-from-array.js
3// META: script=resources/readable-stream-to-array.js
4
5'use strict';
6
7const error1 = new Error('error1');
8error1.name = 'error1';
9
10promise_test(t => {
11  const ts = new TextEncoderStream();
12  const writer = ts.writable.getWriter();
13  const reader = ts.readable.getReader();
14  const writePromise = writer.write({
15    toString() { throw error1; }
16  });
17  const readPromise = reader.read();
18  return Promise.all([
19    promise_rejects_exactly(t, error1, readPromise, 'read should reject with error1'),
20    promise_rejects_exactly(t, error1, writePromise, 'write should reject with error1'),
21    promise_rejects_exactly(t, error1, reader.closed, 'readable should be errored with error1'),
22    promise_rejects_exactly(t, error1, writer.closed, 'writable should be errored with error1'),
23  ]);
24}, 'a chunk that cannot be converted to a string should error the streams');
25
26const oddInputs = [
27  {
28    name: 'undefined',
29    value: undefined,
30    expected: 'undefined'
31  },
32  {
33    name: 'null',
34    value: null,
35    expected: 'null'
36  },
37  {
38    name: 'numeric',
39    value: 3.14,
40    expected: '3.14'
41  },
42  {
43    name: 'object',
44    value: {},
45    expected: '[object Object]'
46  },
47  {
48    name: 'array',
49    value: ['hi'],
50    expected: 'hi'
51  }
52];
53
54for (const input of oddInputs) {
55  promise_test(async () => {
56    const outputReadable = readableStreamFromArray([input.value])
57          .pipeThrough(new TextEncoderStream())
58          .pipeThrough(new TextDecoderStream());
59    const output = await readableStreamToArray(outputReadable);
60    assert_equals(output.length, 1, 'output should contain one chunk');
61    assert_equals(output[0], input.expected, 'output should be correct');
62  }, `input of type ${input.name} should be converted correctly to string`);
63}
64