summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/AsyncIterator/prototype/lazy-methods-multiple-throw-close-iterator-once.js
blob: 45e88be26a38afc67701a42cdfe1cd3617e6196d (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))

//
//
/*---
esid: pending
description: Calling `throw` on a lazy %AsyncIterator.prototype% method multiple times closes the source iterator once.
info: >
  Iterator Helpers proposal 2.1.6
features: [iterator-helpers]
---*/

class TestError extends Error {}

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

  closeCount = 0;
  async return(value) {
    this.closeCount++;
    return {done: true, value};
  }
}

async function* gen(x) { yield x; }

const methods = [
  iter => iter.map(x => x),
  iter => iter.filter(x => true),
  iter => iter.take(Infinity),
  iter => iter.drop(0),
  iter => iter.asIndexedPairs(),
  iter => iter.flatMap(gen),
];

(async () => {
  // Call `throw` after stepping the iterator once:
  for (const method of methods) {
    const iter = new TestIterator();
    const iterHelper = method(iter);

    await iterHelper.next()
    assertEq(iter.closeCount, 0);
      
    try {
      await iterHelper.throw(new TestError());
      assertEq(true, false, 'Expected reject');
    } catch (exc) {
      assertEq(exc instanceof TestError, true);
    }
    assertEq(iter.closeCount, 1);

    try {
      await iterHelper.throw(new TestError());
      assertEq(true, false, 'Expected reject');
    } catch (exc) {
      assertEq(exc instanceof TestError, true);
    }
    assertEq(iter.closeCount, 1);
  }

  // Call `throw` before stepping the iterator:
  for (const method of methods) {
    const iter = new TestIterator();
    const iterHelper = method(iter);

    assertEq(iter.closeCount, 0);

    try {
      await iterHelper.throw(new TestError());
      assertEq(true, false, 'Expected reject');
    } catch (exc) {
      assertEq(exc instanceof TestError, true);
    }
    assertEq(iter.closeCount, 1);

    try {
      await iterHelper.throw(new TestError());
      assertEq(true, false, 'Expected reject');
    } catch (exc) {
      assertEq(exc instanceof TestError, true);
    }
    assertEq(iter.closeCount, 1);
  }
})();

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