summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/AsyncIterator/prototype/flatMap/throw-when-inner-not-iterable.js
blob: e854a8c8ae2b505bf899db3101a12924d3675a1e (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))

//
//
/*---
esid: pending
description: %AsyncIterator.prototype%.flatMap closes the iterator and throws when mapped isn't iterable.
info: >
  Iterator Helpers proposal 2.1.6.7
  1. Repeat,
    ...
    h. Let innerIterator be GetIterator(mapped, async).
    i. IfAbruptCloseAsyncIterator(innerIterator, iterated).
features: [iterator-helpers]
---*/

class NotIterable {
  async next() {
    return {done: true};
  }
}

class InvalidIterable {
  [Symbol.asyncIterator]() {
    return {};
  }
}

class TestIterator extends AsyncIterator {
  async next() {
    return {done: false, value: 0};
  }

  closed = false;
  async return(value) {
    this.closed = true;
    return {done: true, value};
  }
}

const nonIterables = [
  new NotIterable(),
  new InvalidIterable(),
  undefined,
  null,
  0,
  false,
  Symbol(''),
  0n,
  {},
];

(async () => {
  for (const value of nonIterables) {
    const iter = new TestIterator();
    const mapped = iter.flatMap(x => value);

    assertEq(iter.closed, false);
    console.log("here!");
    try {
      await mapped.next();
      assertEq(true, false, 'Expected reject');
    } catch (exc) {
      assertEq(exc instanceof TypeError, true);
    }
    assertEq(iter.closed, true);
  }
})();

if (typeof reportCompare == 'function')
  reportCompare(0, 0);