11cb0ef41Sopenharmony_ciimport { mustCall } from '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport { Readable } from 'stream';
31cb0ef41Sopenharmony_ciimport assert from 'assert';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// These tests are manually ported from the draft PR for the test262 test suite
61cb0ef41Sopenharmony_ci// Authored by Rick Waldron in https://github.com/tc39/test262/pull/2818/files
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// test262 license:
91cb0ef41Sopenharmony_ci// The << Software identified by reference to the Ecma Standard* ("Software)">>
101cb0ef41Sopenharmony_ci// is protected by copyright and is being made available under the
111cb0ef41Sopenharmony_ci// "BSD License", included below. This Software may be subject to third party
121cb0ef41Sopenharmony_ci// rights (rights from parties other than Ecma International), including patent
131cb0ef41Sopenharmony_ci// rights, and no licenses under such third party rights are granted under this
141cb0ef41Sopenharmony_ci// license even if the third party concerned is a member of Ecma International.
151cb0ef41Sopenharmony_ci// SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT
161cb0ef41Sopenharmony_ci// http://www.ecma-international.org/memento/codeofconduct.htm FOR INFORMATION
171cb0ef41Sopenharmony_ci// REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA
181cb0ef41Sopenharmony_ci// INTERNATIONAL STANDARDS*
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci// Copyright (C) 2012-2013 Ecma International
211cb0ef41Sopenharmony_ci// All rights reserved.
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
241cb0ef41Sopenharmony_ci// modification, are permitted provided that the following conditions are met:
251cb0ef41Sopenharmony_ci// 1.   Redistributions of source code must retain the above copyright notice,
261cb0ef41Sopenharmony_ci//      this list of conditions and the following disclaimer.
271cb0ef41Sopenharmony_ci// 2.   Redistributions in binary form must reproduce the above copyright
281cb0ef41Sopenharmony_ci//      notice, this list of conditions and the following disclaimer in the
291cb0ef41Sopenharmony_ci//      documentation and/or other materials provided with the distribution.
301cb0ef41Sopenharmony_ci// 3.   Neither the name of the authors nor Ecma International may be used to
311cb0ef41Sopenharmony_ci//      endorse or promote products derived from this software without specific
321cb0ef41Sopenharmony_ci//      prior written permission.
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS
351cb0ef41Sopenharmony_ci// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
361cb0ef41Sopenharmony_ci// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
371cb0ef41Sopenharmony_ci// NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT,
381cb0ef41Sopenharmony_ci// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
391cb0ef41Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
401cb0ef41Sopenharmony_ci// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
411cb0ef41Sopenharmony_ci// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
421cb0ef41Sopenharmony_ci// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
431cb0ef41Sopenharmony_ci// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
441cb0ef41Sopenharmony_ci//
451cb0ef41Sopenharmony_ci// * Ecma International Standards hereafter means Ecma International Standards
461cb0ef41Sopenharmony_ci// as well as Ecma Technical Reports
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci// Note all the tests that check AsyncIterator's prototype itself and things
501cb0ef41Sopenharmony_ci// that happen before stream conversion were not ported.
511cb0ef41Sopenharmony_ci{
521cb0ef41Sopenharmony_ci  // asIndexedPairs/is-function
531cb0ef41Sopenharmony_ci  assert.strictEqual(typeof Readable.prototype.asIndexedPairs, 'function');
541cb0ef41Sopenharmony_ci  // asIndexedPairs/indexed-pairs.js
551cb0ef41Sopenharmony_ci  const iterator = Readable.from([0, 1]);
561cb0ef41Sopenharmony_ci  const indexedPairs = iterator.asIndexedPairs();
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  for await (const [i, v] of indexedPairs) {
591cb0ef41Sopenharmony_ci    assert.strictEqual(i, v);
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci  // asIndexedPairs/length.js
621cb0ef41Sopenharmony_ci  assert.strictEqual(Readable.prototype.asIndexedPairs.length, 0);
631cb0ef41Sopenharmony_ci  const descriptor = Object.getOwnPropertyDescriptor(
641cb0ef41Sopenharmony_ci    Readable.prototype,
651cb0ef41Sopenharmony_ci    'asIndexedPairs'
661cb0ef41Sopenharmony_ci  );
671cb0ef41Sopenharmony_ci  assert.strictEqual(descriptor.enumerable, false);
681cb0ef41Sopenharmony_ci  assert.strictEqual(descriptor.configurable, true);
691cb0ef41Sopenharmony_ci  assert.strictEqual(descriptor.writable, true);
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci{
721cb0ef41Sopenharmony_ci  // drop/length
731cb0ef41Sopenharmony_ci  assert.strictEqual(Readable.prototype.drop.length, 1);
741cb0ef41Sopenharmony_ci  const descriptor = Object.getOwnPropertyDescriptor(
751cb0ef41Sopenharmony_ci    Readable.prototype,
761cb0ef41Sopenharmony_ci    'drop'
771cb0ef41Sopenharmony_ci  );
781cb0ef41Sopenharmony_ci  assert.strictEqual(descriptor.enumerable, false);
791cb0ef41Sopenharmony_ci  assert.strictEqual(descriptor.configurable, true);
801cb0ef41Sopenharmony_ci  assert.strictEqual(descriptor.writable, true);
811cb0ef41Sopenharmony_ci  // drop/limit-equals-total
821cb0ef41Sopenharmony_ci  const iterator = Readable.from([1, 2]).drop(2);
831cb0ef41Sopenharmony_ci  const result = await iterator[Symbol.asyncIterator]().next();
841cb0ef41Sopenharmony_ci  assert.deepStrictEqual(result, { done: true, value: undefined });
851cb0ef41Sopenharmony_ci  // drop/limit-greater-than-total.js
861cb0ef41Sopenharmony_ci  const iterator2 = Readable.from([1, 2]).drop(3);
871cb0ef41Sopenharmony_ci  const result2 = await iterator2[Symbol.asyncIterator]().next();
881cb0ef41Sopenharmony_ci  assert.deepStrictEqual(result2, { done: true, value: undefined });
891cb0ef41Sopenharmony_ci  // drop/limit-less-than-total.js
901cb0ef41Sopenharmony_ci  const iterator3 = Readable.from([1, 2]).drop(1);
911cb0ef41Sopenharmony_ci  const result3 = await iterator3[Symbol.asyncIterator]().next();
921cb0ef41Sopenharmony_ci  assert.deepStrictEqual(result3, { done: false, value: 2 });
931cb0ef41Sopenharmony_ci  // drop/limit-rangeerror
941cb0ef41Sopenharmony_ci  assert.throws(() => Readable.from([1]).drop(-1), RangeError);
951cb0ef41Sopenharmony_ci  assert.throws(() => {
961cb0ef41Sopenharmony_ci    Readable.from([1]).drop({
971cb0ef41Sopenharmony_ci      valueOf() {
981cb0ef41Sopenharmony_ci        throw new Error('boom');
991cb0ef41Sopenharmony_ci      }
1001cb0ef41Sopenharmony_ci    });
1011cb0ef41Sopenharmony_ci  }, /boom/);
1021cb0ef41Sopenharmony_ci  // drop/limit-tointeger
1031cb0ef41Sopenharmony_ci  const two = await Readable.from([1, 2]).drop({ valueOf: () => 1 }).toArray();
1041cb0ef41Sopenharmony_ci  assert.deepStrictEqual(two, [2]);
1051cb0ef41Sopenharmony_ci  // drop/name
1061cb0ef41Sopenharmony_ci  assert.strictEqual(Readable.prototype.drop.name, 'drop');
1071cb0ef41Sopenharmony_ci  // drop/non-constructible
1081cb0ef41Sopenharmony_ci  assert.throws(() => new Readable.prototype.drop(1), TypeError);
1091cb0ef41Sopenharmony_ci  // drop/proto
1101cb0ef41Sopenharmony_ci  const proto = Object.getPrototypeOf(Readable.prototype.drop);
1111cb0ef41Sopenharmony_ci  assert.strictEqual(proto, Function.prototype);
1121cb0ef41Sopenharmony_ci}
1131cb0ef41Sopenharmony_ci{
1141cb0ef41Sopenharmony_ci  // every/abrupt-iterator-close
1151cb0ef41Sopenharmony_ci  const stream = Readable.from([1, 2, 3]);
1161cb0ef41Sopenharmony_ci  const e = new Error();
1171cb0ef41Sopenharmony_ci  await assert.rejects(stream.every(mustCall(() => {
1181cb0ef41Sopenharmony_ci    throw e;
1191cb0ef41Sopenharmony_ci  }, 1)), e);
1201cb0ef41Sopenharmony_ci}
1211cb0ef41Sopenharmony_ci{
1221cb0ef41Sopenharmony_ci  // every/callable-fn
1231cb0ef41Sopenharmony_ci  await assert.rejects(Readable.from([1, 2]).every({}), TypeError);
1241cb0ef41Sopenharmony_ci}
1251cb0ef41Sopenharmony_ci{
1261cb0ef41Sopenharmony_ci  // every/callable
1271cb0ef41Sopenharmony_ci  Readable.prototype.every.call(Readable.from([]), () => {});
1281cb0ef41Sopenharmony_ci  // eslint-disable-next-line array-callback-return
1291cb0ef41Sopenharmony_ci  Readable.from([]).every(() => {});
1301cb0ef41Sopenharmony_ci  assert.throws(() => {
1311cb0ef41Sopenharmony_ci    const r = Readable.from([]);
1321cb0ef41Sopenharmony_ci    new r.every(() => {});
1331cb0ef41Sopenharmony_ci  }, TypeError);
1341cb0ef41Sopenharmony_ci}
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci{
1371cb0ef41Sopenharmony_ci  // every/false
1381cb0ef41Sopenharmony_ci  const iterator = Readable.from([1, 2, 3]);
1391cb0ef41Sopenharmony_ci  const result = await iterator.every((v) => v === 1);
1401cb0ef41Sopenharmony_ci  assert.strictEqual(result, false);
1411cb0ef41Sopenharmony_ci}
1421cb0ef41Sopenharmony_ci{
1431cb0ef41Sopenharmony_ci  // every/every
1441cb0ef41Sopenharmony_ci  const iterator = Readable.from([1, 2, 3]);
1451cb0ef41Sopenharmony_ci  const result = await iterator.every((v) => true);
1461cb0ef41Sopenharmony_ci  assert.strictEqual(result, true);
1471cb0ef41Sopenharmony_ci}
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci{
1501cb0ef41Sopenharmony_ci  // every/is-function
1511cb0ef41Sopenharmony_ci  assert.strictEqual(typeof Readable.prototype.every, 'function');
1521cb0ef41Sopenharmony_ci}
1531cb0ef41Sopenharmony_ci{
1541cb0ef41Sopenharmony_ci  // every/length
1551cb0ef41Sopenharmony_ci  assert.strictEqual(Readable.prototype.every.length, 1);
1561cb0ef41Sopenharmony_ci  // every/name
1571cb0ef41Sopenharmony_ci  assert.strictEqual(Readable.prototype.every.name, 'every');
1581cb0ef41Sopenharmony_ci  // every/propdesc
1591cb0ef41Sopenharmony_ci  const descriptor = Object.getOwnPropertyDescriptor(
1601cb0ef41Sopenharmony_ci    Readable.prototype,
1611cb0ef41Sopenharmony_ci    'every'
1621cb0ef41Sopenharmony_ci  );
1631cb0ef41Sopenharmony_ci  assert.strictEqual(descriptor.enumerable, false);
1641cb0ef41Sopenharmony_ci  assert.strictEqual(descriptor.configurable, true);
1651cb0ef41Sopenharmony_ci  assert.strictEqual(descriptor.writable, true);
1661cb0ef41Sopenharmony_ci}
167