• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { mustCall } from '../common/index.mjs';
2import { Readable } from 'stream';
3import assert from 'assert';
4
5// These tests are manually ported from the draft PR for the test262 test suite
6// Authored by Rick Waldron in https://github.com/tc39/test262/pull/2818/files
7
8// test262 license:
9// The << Software identified by reference to the Ecma Standard* ("Software)">>
10// is protected by copyright and is being made available under the
11// "BSD License", included below. This Software may be subject to third party
12// rights (rights from parties other than Ecma International), including patent
13// rights, and no licenses under such third party rights are granted under this
14// license even if the third party concerned is a member of Ecma International.
15// SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT
16// http://www.ecma-international.org/memento/codeofconduct.htm FOR INFORMATION
17// REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA
18// INTERNATIONAL STANDARDS*
19
20// Copyright (C) 2012-2013 Ecma International
21// All rights reserved.
22
23// Redistribution and use in source and binary forms, with or without
24// modification, are permitted provided that the following conditions are met:
25// 1.   Redistributions of source code must retain the above copyright notice,
26//      this list of conditions and the following disclaimer.
27// 2.   Redistributions in binary form must reproduce the above copyright
28//      notice, this list of conditions and the following disclaimer in the
29//      documentation and/or other materials provided with the distribution.
30// 3.   Neither the name of the authors nor Ecma International may be used to
31//      endorse or promote products derived from this software without specific
32//      prior written permission.
33
34// THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS
35// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
37// NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT,
38// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
40// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
43// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44//
45// * Ecma International Standards hereafter means Ecma International Standards
46// as well as Ecma Technical Reports
47
48
49// Note all the tests that check AsyncIterator's prototype itself and things
50// that happen before stream conversion were not ported.
51{
52  // asIndexedPairs/is-function
53  assert.strictEqual(typeof Readable.prototype.asIndexedPairs, 'function');
54  // asIndexedPairs/indexed-pairs.js
55  const iterator = Readable.from([0, 1]);
56  const indexedPairs = iterator.asIndexedPairs();
57
58  for await (const [i, v] of indexedPairs) {
59    assert.strictEqual(i, v);
60  }
61  // asIndexedPairs/length.js
62  assert.strictEqual(Readable.prototype.asIndexedPairs.length, 0);
63  const descriptor = Object.getOwnPropertyDescriptor(
64    Readable.prototype,
65    'asIndexedPairs'
66  );
67  assert.strictEqual(descriptor.enumerable, false);
68  assert.strictEqual(descriptor.configurable, true);
69  assert.strictEqual(descriptor.writable, true);
70}
71{
72  // drop/length
73  assert.strictEqual(Readable.prototype.drop.length, 1);
74  const descriptor = Object.getOwnPropertyDescriptor(
75    Readable.prototype,
76    'drop'
77  );
78  assert.strictEqual(descriptor.enumerable, false);
79  assert.strictEqual(descriptor.configurable, true);
80  assert.strictEqual(descriptor.writable, true);
81  // drop/limit-equals-total
82  const iterator = Readable.from([1, 2]).drop(2);
83  const result = await iterator[Symbol.asyncIterator]().next();
84  assert.deepStrictEqual(result, { done: true, value: undefined });
85  // drop/limit-greater-than-total.js
86  const iterator2 = Readable.from([1, 2]).drop(3);
87  const result2 = await iterator2[Symbol.asyncIterator]().next();
88  assert.deepStrictEqual(result2, { done: true, value: undefined });
89  // drop/limit-less-than-total.js
90  const iterator3 = Readable.from([1, 2]).drop(1);
91  const result3 = await iterator3[Symbol.asyncIterator]().next();
92  assert.deepStrictEqual(result3, { done: false, value: 2 });
93  // drop/limit-rangeerror
94  assert.throws(() => Readable.from([1]).drop(-1), RangeError);
95  assert.throws(() => {
96    Readable.from([1]).drop({
97      valueOf() {
98        throw new Error('boom');
99      }
100    });
101  }, /boom/);
102  // drop/limit-tointeger
103  const two = await Readable.from([1, 2]).drop({ valueOf: () => 1 }).toArray();
104  assert.deepStrictEqual(two, [2]);
105  // drop/name
106  assert.strictEqual(Readable.prototype.drop.name, 'drop');
107  // drop/non-constructible
108  assert.throws(() => new Readable.prototype.drop(1), TypeError);
109  // drop/proto
110  const proto = Object.getPrototypeOf(Readable.prototype.drop);
111  assert.strictEqual(proto, Function.prototype);
112}
113{
114  // every/abrupt-iterator-close
115  const stream = Readable.from([1, 2, 3]);
116  const e = new Error();
117  await assert.rejects(stream.every(mustCall(() => {
118    throw e;
119  }, 1)), e);
120}
121{
122  // every/callable-fn
123  await assert.rejects(Readable.from([1, 2]).every({}), TypeError);
124}
125{
126  // every/callable
127  Readable.prototype.every.call(Readable.from([]), () => {});
128  // eslint-disable-next-line array-callback-return
129  Readable.from([]).every(() => {});
130  assert.throws(() => {
131    const r = Readable.from([]);
132    new r.every(() => {});
133  }, TypeError);
134}
135
136{
137  // every/false
138  const iterator = Readable.from([1, 2, 3]);
139  const result = await iterator.every((v) => v === 1);
140  assert.strictEqual(result, false);
141}
142{
143  // every/every
144  const iterator = Readable.from([1, 2, 3]);
145  const result = await iterator.every((v) => true);
146  assert.strictEqual(result, true);
147}
148
149{
150  // every/is-function
151  assert.strictEqual(typeof Readable.prototype.every, 'function');
152}
153{
154  // every/length
155  assert.strictEqual(Readable.prototype.every.length, 1);
156  // every/name
157  assert.strictEqual(Readable.prototype.every.name, 'every');
158  // every/propdesc
159  const descriptor = Object.getOwnPropertyDescriptor(
160    Readable.prototype,
161    'every'
162  );
163  assert.strictEqual(descriptor.enumerable, false);
164  assert.strictEqual(descriptor.configurable, true);
165  assert.strictEqual(descriptor.writable, true);
166}
167