Skip to content

Commit d405213

Browse files
authored
add tests for "iterator helpers close receiver on argument validation failure" (#4350)
* add tests for "iterator helpers close receiver on argument validation failure" * address comments * avoid re-using Test262Error
1 parent d8e2cb2 commit d405213

11 files changed

+380
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iteratorprototype.drop
5+
description: >
6+
Underlying iterator is closed when argument validation fails
7+
info: |
8+
%Iterator.prototype%.drop ( limit )
9+
10+
features: [iterator-helpers]
11+
flags: []
12+
---*/
13+
14+
let closed = false;
15+
let closable = {
16+
__proto__: Iterator.prototype,
17+
get next() {
18+
throw new Test262Error('next should not be read');
19+
},
20+
return() {
21+
closed = true;
22+
return {};
23+
},
24+
};
25+
26+
assert.throws(RangeError, function() {
27+
closable.drop();
28+
});
29+
assert.sameValue(closed, true);
30+
31+
closed = false;
32+
assert.throws(RangeError, function() {
33+
closable.drop(NaN);
34+
});
35+
assert.sameValue(closed, true);
36+
37+
closed = false;
38+
assert.throws(RangeError, function() {
39+
closable.drop(-1);
40+
});
41+
assert.sameValue(closed, true);
42+
43+
closed = false;
44+
class ShouldNotGetValueOf {}
45+
assert.throws(ShouldNotGetValueOf, function() {
46+
closable.drop({ get valueOf() { throw new ShouldNotGetValueOf(); }});
47+
});
48+
assert.sameValue(closed, true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iteratorprototype.every
5+
description: >
6+
Underlying iterator is closed when argument validation fails
7+
info: |
8+
%Iterator.prototype%.every ( predicate )
9+
10+
features: [iterator-helpers]
11+
flags: []
12+
---*/
13+
14+
let closed = false;
15+
let closable = {
16+
__proto__: Iterator.prototype,
17+
get next() {
18+
throw new Test262Error('next should not be read');
19+
},
20+
return() {
21+
closed = true;
22+
return {};
23+
},
24+
};
25+
26+
assert.throws(TypeError, function() {
27+
closable.every();
28+
});
29+
assert.sameValue(closed, true);
30+
31+
closed = false;
32+
assert.throws(TypeError, function() {
33+
closable.every({});
34+
});
35+
assert.sameValue(closed, true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iteratorprototype.filter
5+
description: >
6+
Underlying iterator is closed when argument validation fails
7+
info: |
8+
%Iterator.prototype%.filter ( predicate )
9+
10+
features: [iterator-helpers]
11+
flags: []
12+
---*/
13+
14+
let closed = false;
15+
let closable = {
16+
__proto__: Iterator.prototype,
17+
get next() {
18+
throw new Test262Error('next should not be read');
19+
},
20+
return() {
21+
closed = true;
22+
return {};
23+
},
24+
};
25+
26+
assert.throws(TypeError, function() {
27+
closable.filter();
28+
});
29+
assert.sameValue(closed, true);
30+
31+
closed = false;
32+
assert.throws(TypeError, function() {
33+
closable.filter({});
34+
});
35+
assert.sameValue(closed, true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iteratorprototype.find
5+
description: >
6+
Underlying iterator is closed when argument validation fails
7+
info: |
8+
%Iterator.prototype%.find ( predicate )
9+
10+
features: [iterator-helpers]
11+
flags: []
12+
---*/
13+
14+
let closed = false;
15+
let closable = {
16+
__proto__: Iterator.prototype,
17+
get next() {
18+
throw new Test262Error('next should not be read');
19+
},
20+
return() {
21+
closed = true;
22+
return {};
23+
},
24+
};
25+
26+
assert.throws(TypeError, function() {
27+
closable.find();
28+
});
29+
assert.sameValue(closed, true);
30+
31+
closed = false;
32+
assert.throws(TypeError, function() {
33+
closable.find({});
34+
});
35+
assert.sameValue(closed, true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iteratorprototype.flatMap
5+
description: >
6+
Underlying iterator is closed when argument validation fails
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
features: [iterator-helpers]
11+
flags: []
12+
---*/
13+
14+
let closed = false;
15+
let closable = {
16+
__proto__: Iterator.prototype,
17+
get next() {
18+
throw new Test262Error('next should not be read');
19+
},
20+
return() {
21+
closed = true;
22+
return {};
23+
},
24+
};
25+
26+
assert.throws(TypeError, function() {
27+
closable.flatMap();
28+
});
29+
assert.sameValue(closed, true);
30+
31+
closed = false;
32+
assert.throws(TypeError, function() {
33+
closable.flatMap({});
34+
});
35+
assert.sameValue(closed, true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iteratorprototype.forEach
5+
description: >
6+
Underlying iterator is closed when argument validation fails
7+
info: |
8+
%Iterator.prototype%.forEach ( predicate )
9+
10+
features: [iterator-helpers]
11+
flags: []
12+
---*/
13+
14+
let closed = false;
15+
let closable = {
16+
__proto__: Iterator.prototype,
17+
get next() {
18+
throw new Test262Error('next should not be read');
19+
},
20+
return() {
21+
closed = true;
22+
return {};
23+
},
24+
};
25+
26+
assert.throws(TypeError, function() {
27+
closable.forEach();
28+
});
29+
assert.sameValue(closed, true);
30+
31+
closed = false;
32+
assert.throws(TypeError, function() {
33+
closable.forEach({});
34+
});
35+
assert.sameValue(closed, true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iteratorprototype.map
5+
description: >
6+
Underlying iterator is closed when argument validation fails
7+
info: |
8+
%Iterator.prototype%.map ( mapper )
9+
10+
features: [iterator-helpers]
11+
flags: []
12+
---*/
13+
14+
let closed = false;
15+
let closable = {
16+
__proto__: Iterator.prototype,
17+
get next() {
18+
throw new Test262Error('next should not be read');
19+
},
20+
return() {
21+
closed = true;
22+
return {};
23+
},
24+
};
25+
26+
assert.throws(TypeError, function() {
27+
closable.map();
28+
});
29+
assert.sameValue(closed, true);
30+
31+
closed = false;
32+
assert.throws(TypeError, function() {
33+
closable.map({});
34+
});
35+
assert.sameValue(closed, true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iteratorprototype.reduce
5+
description: >
6+
Underlying iterator is closed when argument validation fails
7+
info: |
8+
%Iterator.prototype%.reduce ( reducer, [ initialValue ] )
9+
10+
features: [iterator-helpers]
11+
flags: []
12+
---*/
13+
14+
let closed = false;
15+
let closable = {
16+
__proto__: Iterator.prototype,
17+
get next() {
18+
throw new Test262Error('next should not be read');
19+
},
20+
return() {
21+
closed = true;
22+
return {};
23+
},
24+
};
25+
26+
assert.throws(TypeError, function() {
27+
closable.reduce();
28+
});
29+
assert.sameValue(closed, true);
30+
31+
closed = false;
32+
assert.throws(TypeError, function() {
33+
closable.reduce({});
34+
});
35+
assert.sameValue(closed, true);

test/built-ins/Iterator/prototype/reduce/non-callable-reducer.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ features: [iterator-helpers]
1111
flags: []
1212
---*/
1313
let nonCallable = {};
14-
let iterator = (function* () {
14+
function* gen() {
1515
yield 1;
16-
})();
16+
}
1717

1818
assert.throws(TypeError, function () {
19-
iterator.reduce(nonCallable);
19+
gen().reduce(nonCallable);
2020
});
2121

22-
iterator.reduce(() => {});
22+
gen().reduce(() => {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2024 Kevin Gibbons. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iteratorprototype.some
5+
description: >
6+
Underlying iterator is closed when argument validation fails
7+
info: |
8+
%Iterator.prototype%.some ( predicate )
9+
10+
features: [iterator-helpers]
11+
flags: []
12+
---*/
13+
14+
let closed = false;
15+
let closable = {
16+
__proto__: Iterator.prototype,
17+
get next() {
18+
throw new Test262Error('next should not be read');
19+
},
20+
return() {
21+
closed = true;
22+
return {};
23+
},
24+
};
25+
26+
assert.throws(TypeError, function() {
27+
closable.some();
28+
});
29+
assert.sameValue(closed, true);
30+
31+
closed = false;
32+
assert.throws(TypeError, function() {
33+
closable.some({});
34+
});
35+
assert.sameValue(closed, true);

0 commit comments

Comments
 (0)