Skip to content

Commit 2e74125

Browse files
bnoordhuisMs2ger
authored andcommitted
Test iterator close behavior of Set methods
Set.prototype.isDisjointFrom and Set.prototype.isSupersetOf should call return() on the set-like iterator but only when it is not exhausted. Port of the downstream tests I added in quickjs-ng/quickjs@6167dcb127.
1 parent 8a8a7af commit 2e74125

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (C) 2025 Ben Noordhuis. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-set.prototype.isdisjointfrom
5+
description: >
6+
Set.prototype.isDisjointFrom calls return() on the set-like iterator, but
7+
only if the iterator is not exhausted, i.e., the set-like is not disjoint.
8+
features: [set-methods, Array.prototype.includes]
9+
---*/
10+
11+
const iter = {
12+
a: [4, 5, 6],
13+
nextCalls: 0,
14+
returnCalls: 0,
15+
next() {
16+
const done = this.nextCalls >= this.a.length;
17+
const value = this.a[this.nextCalls];
18+
this.nextCalls++;
19+
return {done, value};
20+
},
21+
return() {
22+
this.returnCalls++;
23+
return this;
24+
},
25+
};
26+
27+
const setlike = {
28+
size: iter.a.length,
29+
has(v) { return iter.a.includes(v); },
30+
keys() { return iter; },
31+
};
32+
33+
// Set must be bigger than iter.a to hit iter.next and iter.return.
34+
assert.sameValue(new Set([4, 5, 6, 7]).isDisjointFrom(setlike), false);
35+
assert.sameValue(iter.nextCalls, 1);
36+
assert.sameValue(iter.returnCalls, 1);
37+
iter.nextCalls = iter.returnCalls = 0;
38+
39+
assert.sameValue(new Set([0, 1, 2, 3]).isDisjointFrom(setlike), true);
40+
assert.sameValue(iter.nextCalls, 4);
41+
assert.sameValue(iter.returnCalls, 0);
42+
iter.nextCalls = iter.returnCalls = 0;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2025 Ben Noordhuis. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-set.prototype.issupersetof
5+
description: >
6+
Set.prototype.isSupersetOf calls return() on the set-like iterator, but
7+
only if the iterator is not exhausted, i.e., the set is not a superset
8+
of the set-like.
9+
features: [set-methods, Array.prototype.includes]
10+
---*/
11+
12+
const iter = {
13+
a: [4, 5, 6],
14+
nextCalls: 0,
15+
returnCalls: 0,
16+
next() {
17+
const done = this.nextCalls >= this.a.length;
18+
const value = this.a[this.nextCalls];
19+
this.nextCalls++;
20+
return {done, value};
21+
},
22+
return() {
23+
this.returnCalls++;
24+
return this;
25+
},
26+
};
27+
28+
const setlike = {
29+
size: iter.a.length,
30+
has(v) { return iter.a.includes(v); },
31+
keys() { return iter; },
32+
};
33+
34+
// Set must be bigger than iter.a to hit iter.next and iter.return.
35+
assert.sameValue(new Set([4, 5, 6, 7]).isSupersetOf(setlike), true);
36+
assert.sameValue(iter.nextCalls, 4);
37+
assert.sameValue(iter.returnCalls, 0);
38+
iter.nextCalls = iter.returnCalls = 0;
39+
40+
assert.sameValue(new Set([0, 1, 2, 3]).isSupersetOf(setlike), false);
41+
assert.sameValue(iter.nextCalls, 1);
42+
assert.sameValue(iter.returnCalls, 1);
43+
iter.nextCalls = iter.returnCalls = 0;

0 commit comments

Comments
 (0)