Skip to content

Commit 64d791b

Browse files
iterator includes tests
1 parent b66872a commit 64d791b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1045
-0
lines changed

features.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ hashbang
159159
import-attributes
160160
import.meta
161161
iterator-helpers
162+
iterator-includes
162163
iterator-sequencing
163164
Int8Array
164165
Int16Array
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (C) 2026 Michael Ficarra. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iterator.prototype.includes
5+
description: >
6+
Arguments and this value are validated in the correct order
7+
info: |
8+
Iterator.prototype.includes ( searchElement [ , skippedElements ] )
9+
10+
1. Let O be the this value.
11+
2. If O is not an Object, throw a TypeError exception.
12+
3. Let iterated be the Iterator Record { [[Iterator]]: O, [[NextMethod]]: undefined, [[Done]]: false }.
13+
4. If skippedElements is undefined, let toSkip be 0.
14+
5. Else if skippedElements is not one of +Infinity, -Infinity, or an integral Number,
15+
a. Let error be ThrowCompletion(a newly created TypeError object).
16+
b. Return ? IteratorClose(iterated, error).
17+
9. Set iterated to ? GetIteratorDirect(O).
18+
19+
includes: [compareArray.js]
20+
features: [iterator-includes]
21+
---*/
22+
23+
let effects = [];
24+
25+
assert.throws(TypeError, function() {
26+
Iterator.prototype.includes.call(null, 0, NaN);
27+
});
28+
29+
assert.compareArray(effects, []);
30+
31+
assert.throws(TypeError, function() {
32+
Iterator.prototype.includes.call(
33+
{
34+
get next() {
35+
effects.push('get next');
36+
return function() {
37+
return { done: true, value: undefined };
38+
};
39+
},
40+
return() {
41+
effects.push('return');
42+
return {};
43+
},
44+
},
45+
0,
46+
NaN
47+
);
48+
});
49+
50+
assert.compareArray(effects, ['return']);
51+
52+
effects = [];
53+
54+
Iterator.prototype.includes.call(
55+
{
56+
get next() {
57+
effects.push('get next');
58+
return function() {
59+
return { done: true, value: undefined };
60+
};
61+
},
62+
},
63+
0,
64+
0
65+
);
66+
67+
assert.compareArray(effects, ['get next']);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (C) 2026 Michael Ficarra. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iterator.prototype.includes
5+
description: >
6+
Underlying iterator is closed when skippedElements validation fails
7+
features: [iterator-includes]
8+
---*/
9+
10+
let closed = false;
11+
let closable = {
12+
__proto__: Iterator.prototype,
13+
get next() {
14+
throw new Test262Error('next should not be read');
15+
},
16+
return() {
17+
closed = true;
18+
return {};
19+
},
20+
};
21+
22+
assert.throws(RangeError, function() {
23+
closable.includes(null, -2);
24+
});
25+
assert.sameValue(closed, true, 'iterator closed when skippedElements validation produces a RangeError');
26+
27+
closed = false;
28+
assert.throws(TypeError, function() {
29+
closable.includes(null, 'a string');
30+
});
31+
assert.sameValue(closed, true, 'iterator closed when skippedElements validation produces a TypeError');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (C) 2026 Michael Ficarra. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iterator.prototype.includes
5+
description: >
6+
Basic positive and negative matches
7+
features: [iterator-includes]
8+
---*/
9+
10+
let arr = [3, 6, 9];
11+
12+
assert.sameValue(arr.values().includes(0), false);
13+
assert.sameValue(arr.values().includes(1), false);
14+
assert.sameValue(arr.values().includes(2), false);
15+
assert.sameValue(arr.values().includes(3), true);
16+
assert.sameValue(arr.values().includes(4), false);
17+
assert.sameValue(arr.values().includes(5), false);
18+
assert.sameValue(arr.values().includes(6), true);
19+
assert.sameValue(arr.values().includes(7), false);
20+
assert.sameValue(arr.values().includes(8), false);
21+
assert.sameValue(arr.values().includes(9), true);
22+
assert.sameValue(arr.values().includes(10), false);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (C) 2026 Michael Ficarra. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iterator.prototype.includes
5+
description: >
6+
Iterator.prototype.includes is callable
7+
features: [iterator-includes]
8+
---*/
9+
10+
function* g() {
11+
yield 0;
12+
}
13+
14+
Iterator.prototype.includes.call(g(), 0);
15+
16+
g().includes(0);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (C) 2026 Michael Ficarra. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iterator.prototype.includes
5+
description: >
6+
Iterator.prototype.includes closes the iterator after a successful match
7+
features: [iterator-includes]
8+
---*/
9+
10+
let closed = false;
11+
let i = 0;
12+
let iter = {
13+
__proto__: Iterator.prototype,
14+
next() {
15+
++i;
16+
return { done: false, value: i };
17+
},
18+
return() {
19+
closed = true;
20+
return {};
21+
},
22+
};
23+
24+
assert.sameValue(iter.includes(5), true);
25+
assert.sameValue(closed, true);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (C) 2026 Michael Ficarra. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iterator.prototype.includes
5+
description: >
6+
Exhausting the iterator without finding a match does not call return
7+
features: [iterator-includes]
8+
---*/
9+
10+
let index = 0;
11+
let returnCalls = 0;
12+
13+
let iterator = {
14+
__proto__: Iterator.prototype,
15+
next() {
16+
++index;
17+
if (index <= 3) {
18+
return { done: false, value: index };
19+
}
20+
return { done: true, value: undefined };
21+
},
22+
return() {
23+
++returnCalls;
24+
return {};
25+
},
26+
};
27+
28+
assert.sameValue(iterator.includes(99), false);
29+
assert.sameValue(returnCalls, 0);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (C) 2026 Michael Ficarra. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iterator.prototype.includes
5+
description: >
6+
Gets the next method from the iterator only once
7+
features: [iterator-includes]
8+
---*/
9+
10+
let nextGets = 0;
11+
12+
class TestIterator extends Iterator {
13+
get next() {
14+
++nextGets;
15+
let counter = 5;
16+
return function() {
17+
if (counter < 0) {
18+
return { done: true, value: undefined };
19+
}
20+
return { done: false, value: --counter };
21+
};
22+
}
23+
}
24+
25+
let iterator = new TestIterator();
26+
27+
assert.sameValue(nextGets, 0);
28+
assert.sameValue(iterator.includes(3), true);
29+
assert.sameValue(nextGets, 1);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (C) 2026 Michael Ficarra. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iterator.prototype.includes
5+
description: >
6+
Iterator has throwing next getter
7+
features: [iterator-includes]
8+
---*/
9+
10+
class IteratorThrows extends Iterator {
11+
get next() {
12+
throw new Test262Error();
13+
}
14+
}
15+
16+
let iterator = new IteratorThrows();
17+
18+
assert.throws(Test262Error, function() {
19+
iterator.includes(0);
20+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (C) 2026 Michael Ficarra. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
/*---
4+
esid: sec-iterator.prototype.includes
5+
description: >
6+
Iterator has throwing return getter
7+
features: [iterator-includes]
8+
---*/
9+
10+
class IteratorThrows extends Iterator {
11+
next() {
12+
return {
13+
done: false,
14+
value: 0,
15+
};
16+
}
17+
18+
get return() {
19+
throw new Test262Error();
20+
}
21+
}
22+
23+
let iterator = new IteratorThrows();
24+
25+
assert.throws(Test262Error, function() {
26+
iterator.includes(0);
27+
});

0 commit comments

Comments
 (0)