Skip to content

Commit 815b78e

Browse files
michaelficarraljharb
authored andcommitted
start of flatMap tests
1 parent e1e2e0d commit 815b78e

38 files changed

+948
-4
lines changed

test/built-ins/Iterator/prototype/every/this-non-callable-next.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ features: [iterator-helpers]
1414
flags: []
1515
---*/
1616
assert.throws(TypeError, function() {
17-
Iterator.prototype.every.call({ next: 0 }, () => {});
17+
Iterator.prototype.every.call({ next: 0 }, () => true);
1818
});

test/built-ins/Iterator/prototype/filter/name.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: >
77
info: |
88
17 ECMAScript Standard Built-in Objects
99
10-
filter built-in Function object, including constructors, that is not
10+
Every built-in Function object, including constructors, that is not
1111
identified as an anonymous function has a name property whose value is a
1212
String. Unless otherwise specified, this value is the name that is given to
1313
the function in this specification.

test/built-ins/Iterator/prototype/filter/non-constructible.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ assert.throws(TypeError, () => {
1818
}, '`new Iterator.prototype.filter(() => true)` throws a TypeError exception');
1919

2020
assert.throws(TypeError, () => {
21-
new (class extends Iterator {}).filter(() => {});
21+
new (class extends Iterator {}).filter(() => true);
2222
}, '`new (new class extends Iterator {}).filter(() => true)` throws a TypeError exception');

test/built-ins/Iterator/prototype/filter/this-non-callable-next.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ features: [iterator-helpers]
1414
flags: []
1515
---*/
1616
assert.throws(TypeError, function() {
17-
Iterator.prototype.filter.call({ next: 0 }, 1);
17+
Iterator.prototype.filter.call({ next: 0 }, () => true);
1818
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (C) 2023 Michael Ficarra. 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+
Arguments and this value are evaluated in the correct order
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js, compareArray.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
let effects = [];
15+
16+
assert.throws(TypeError, function() {
17+
Iterator.prototype.flatMap.call(
18+
{
19+
get next() {
20+
effects.push('get next');
21+
return function() {
22+
return { done: true, value: undefined };
23+
};
24+
}
25+
},
26+
null
27+
);
28+
});
29+
30+
assert.compareArray(effects, [
31+
]);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (C) 2020 Rick Waldron. 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+
Iterator.prototype.flatMap is callable
7+
features: [iterator-helpers]
8+
---*/
9+
function* g() {}
10+
Iterator.prototype.flatMap.call(g(), () => []);
11+
12+
let iter = g();
13+
iter.flatMap(() => []);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2023 Michael Ficarra. 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 return is not called when result iterator is exhausted
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
class TestIterator extends Iterator {
15+
constructor() {
16+
super();
17+
this._remaining = 3;
18+
}
19+
next() {
20+
if (this._remaining > 0) {
21+
return {
22+
done: false,
23+
value: this._remaining--
24+
};
25+
} else {
26+
return {
27+
done: true,
28+
value: undefined
29+
};
30+
}
31+
}
32+
return() {
33+
if (this._remaining <= 0) {
34+
throw new Test262Error;
35+
}
36+
return {};
37+
}
38+
}
39+
40+
let iterator = new TestIterator().flatMap(() => []);
41+
iterator.next();
42+
iterator.next();
43+
iterator.next();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2023 Michael Ficarra. 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+
Gets the next method from the underlying iterator only once
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
let nextGets = 0;
15+
let nextCalls = 0;
16+
17+
class CountingIterator extends Iterator {
18+
get next() {
19+
nextGets++;
20+
let iter = function* () {
21+
for (let i = 1; i < 5; ++i) {
22+
yield i;
23+
}
24+
}();
25+
return function () {
26+
nextCalls++;
27+
return iter.next();
28+
};
29+
}
30+
}
31+
32+
let iterator = new CountingIterator;
33+
34+
assert.sameValue(nextGets, 0, 'The value of `nextGets` is 0');
35+
assert.sameValue(nextCalls, 0, 'The value of `nextCalls` is 0');
36+
37+
for (const value of iterator.flatMap(() => []));
38+
39+
assert.sameValue(nextGets, 1, 'The value of `nextGets` is 1');
40+
assert.sameValue(nextCalls, 5, 'The value of `nextCalls` is 5');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (C) 2023 Michael Ficarra. 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 has throwing next getter
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
class ThrowingIterator extends Iterator {
15+
get next() {
16+
throw new Test262Error;
17+
}
18+
}
19+
20+
let iterator = new ThrowingIterator;
21+
22+
assert.throws(Test262Error, function () {
23+
iterator.flatMap(() => []);
24+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (C) 2023 Michael Ficarra. 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 return is throwing getter
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
class TestIterator extends Iterator {
15+
next() {
16+
return {
17+
done: false,
18+
value: 1
19+
};
20+
}
21+
get return() {
22+
throw new Test262Error;
23+
}
24+
}
25+
26+
let iterator = new TestIterator().flatMap(x => [x]);
27+
iterator.next();
28+
29+
assert.throws(Test262Error, function() {
30+
iterator.return();
31+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (C) 2020 Rick Waldron. 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+
Iterator.prototype.flatMap is a built-in function
7+
features: [iterator-helpers]
8+
---*/
9+
10+
assert.sameValue(typeof Iterator.prototype.flatMap, 'function', 'The value of `typeof Iterator.prototype.flatMap` is "function"');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (C) 2023 Michael Ficarra. 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+
Iterator.prototype.flatMap returns an empty iterator when the iterator has already been exhausted
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
let iterator = new Test262Iterator([]);
15+
16+
let {value, done} = iterator.next();
17+
assert.sameValue(value, undefined, 'The value of `value` is expected to equal `undefined`');
18+
assert.sameValue(done, true, 'The value of `done` is true');
19+
20+
iterator = iterator.flatMap(x => [x]);
21+
({value, done} = iterator.next());
22+
assert.sameValue(value, undefined, 'The value of `value` is expected to equal `undefined`');
23+
assert.sameValue(done, true, 'The value of `done` is true');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (C) 2023 Michael Ficarra. 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+
Iterator has throwing return
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
class Test262IteratorThrows extends Test262Iterator {
15+
return() {
16+
throw new Test262Error();
17+
}
18+
}
19+
20+
let iterator = new Test262IteratorThrows([1, 2]).flatMap(() => []);
21+
22+
assert.throws(Test262Error, function() {
23+
iterator.return();
24+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (C) 2020 Rick Waldron. 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+
Iterator.prototype.flatMap has a "length" property whose value is 0.
7+
info: |
8+
ECMAScript Standard Built-in Objects
9+
10+
Unless otherwise specified, the length property of a built-in
11+
Function object has the attributes { [[Writable]]: false, [[Enumerable]]:
12+
false, [[Configurable]]: true }.
13+
features: [iterator-helpers]
14+
includes: [propertyHelper.js]
15+
---*/
16+
17+
assert.sameValue(Iterator.prototype.flatMap.length, 1, 'The value of Iterator.prototype.flatMap.length is 1');
18+
19+
verifyNotEnumerable(Iterator.prototype.flatMap, 'length');
20+
verifyNotWritable(Iterator.prototype.flatMap, 'length');
21+
verifyConfigurable(Iterator.prototype.flatMap, 'length');
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2023 Michael Ficarra. 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+
Iterator.prototype.flatMap mapper is passed the yielded value and a counter as arguments
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
function* g() {
15+
yield 'a';
16+
yield 'b';
17+
yield 'c';
18+
}
19+
20+
let assertionCount = 0;
21+
let iter = g().flatMap((v, count) => {
22+
switch (v) {
23+
case 'a':
24+
assert.sameValue(count, 0);
25+
break;
26+
case 'b':
27+
assert.sameValue(count, 1);
28+
break;
29+
case 'c':
30+
assert.sameValue(count, 2);
31+
break;
32+
default:
33+
throw new Error;
34+
}
35+
++assertionCount;
36+
return [v];
37+
});
38+
39+
assert.sameValue(assertionCount, 0);
40+
41+
for (let i of iter);
42+
43+
assert.sameValue(assertionCount, 3);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2023 Michael Ficarra. 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+
Iterator.prototype.flatMap mapper return value must be an object
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
function* g() {
15+
yield 0;
16+
yield 0;
17+
yield 0;
18+
yield 1;
19+
}
20+
21+
let iter = g();
22+
23+
let mapperCalls = 0;
24+
iter = iter.flatMap(v => {
25+
++mapperCalls;
26+
return null;
27+
});
28+
29+
assert.sameValue(mapperCalls, 0);
30+
31+
assert.throws(TypeError, function() {
32+
iter.next();
33+
});
34+
35+
assert.sameValue(mapperCalls, 1);

0 commit comments

Comments
 (0)