Skip to content

Commit 1662936

Browse files
add some more flatMap tests
1 parent c07669f commit 1662936

File tree

7 files changed

+215
-6
lines changed

7 files changed

+215
-6
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 flattens iterables returned by the mapper
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js, compareArray.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
15+
function* g() {
16+
yield 0;
17+
yield 1;
18+
yield 2;
19+
yield 3;
20+
}
21+
22+
let iter = g().flatMap((v, count) => {
23+
let result = [];
24+
for (let i = 0; i < v; ++i) {
25+
result.push(v);
26+
}
27+
return result;
28+
});
29+
30+
assert.compareArray(Array.from(iter), [1, 2, 2, 3, 3, 3]);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 flattens non-iterable iterators returned by the mapper
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js, compareArray.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
15+
function* g() {
16+
yield 0;
17+
yield 1;
18+
yield 2;
19+
yield 3;
20+
}
21+
22+
let iter = g().flatMap((v, count) => {
23+
let i = 0;
24+
return {
25+
next: function() {
26+
if (i < v) {
27+
++i;
28+
return {
29+
value: v,
30+
done: false
31+
};
32+
} else {
33+
return {
34+
value: undefined,
35+
done: true
36+
};
37+
}
38+
}
39+
};
40+
});
41+
42+
assert.compareArray(Array.from(iter), [1, 2, 2, 3, 3, 3]);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 does not flatten recursively
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js, compareArray.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
15+
let arr = [
16+
{
17+
[Symbol.iterator]: function() {
18+
throw new Test262Error;
19+
}
20+
},
21+
{
22+
next: function() {
23+
throw new Test262Error;
24+
}
25+
}
26+
];
27+
28+
function* g() {
29+
yield arr;
30+
}
31+
32+
let iter = g().flatMap(v => v);
33+
34+
assert.compareArray(Array.from(iter), arr);
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 does not respect the iterability of any primitive
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js, compareArray.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
15+
function* g() {
16+
yield 0;
17+
}
18+
19+
Number.prototype[Symbol.iterator] = function* () {
20+
let i = 0;
21+
let target = this >>> 0;
22+
while (i < target) {
23+
yield i;
24+
++i;
25+
}
26+
};
27+
28+
assert.compareArray(Array.from(5), [0, 1, 2, 3, 4]);
29+
30+
assert.throws(TypeError, function () {
31+
for(let unused of g().flatMap(v => 5));
32+
});
33+
34+
let iter = g().flatMap(v => new Number(5));
35+
assert.compareArray(Array.from(iter), [0, 1, 2, 3, 4]);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 falls back to treating mapper return values as iterators if the Symbol.iterator property is not callable
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js, compareArray.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
15+
function* g() {
16+
yield 0;
17+
}
18+
19+
function* h() {
20+
yield 0;
21+
yield 1;
22+
yield 2;
23+
}
24+
25+
let iter = g().flatMap(v => {
26+
let n = h();
27+
return {
28+
[Symbol.iterator]: 0,
29+
next: () => n.next()
30+
};
31+
});
32+
33+
assert.compareArray(Array.from(iter), [0, 1, 2]);

test/built-ins/Iterator/prototype/flatMap/mapper-args.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,40 @@ function* g() {
1515
yield 'a';
1616
yield 'b';
1717
yield 'c';
18+
yield 'd';
19+
yield 'e';
1820
}
1921

2022
let assertionCount = 0;
2123
let iter = g().flatMap((v, count) => {
2224
switch (v) {
2325
case 'a':
2426
assert.sameValue(count, 0);
25-
break;
27+
++assertionCount;
28+
return [0];
2629
case 'b':
2730
assert.sameValue(count, 1);
28-
break;
31+
++assertionCount;
32+
return [0];
2933
case 'c':
3034
assert.sameValue(count, 2);
31-
break;
35+
++assertionCount;
36+
return [1, 2];
37+
case 'd':
38+
assert.sameValue(count, 3);
39+
++assertionCount;
40+
return [3, 4, 5];
41+
case 'e':
42+
assert.sameValue(count, 4);
43+
++assertionCount;
44+
return [6, 7, 8, 9];
3245
default:
3346
throw new Error;
3447
}
35-
++assertionCount;
36-
return [v];
3748
});
3849

3950
assert.sameValue(assertionCount, 0);
4051

4152
for (let i of iter);
4253

43-
assert.sameValue(assertionCount, 3);
54+
assert.sameValue(assertionCount, 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+
Iterator.prototype.flatMap does not respect the iterability of primitive strings
7+
info: |
8+
%Iterator.prototype%.flatMap ( mapper )
9+
10+
includes: [iterators.js, compareArray.js]
11+
features: [iterator-helpers]
12+
flags: []
13+
---*/
14+
15+
function* g() {
16+
yield 0;
17+
}
18+
19+
assert.throws(TypeError, function () {
20+
for(let unused of g().flatMap(v => 'string'));
21+
});
22+
23+
let iter = g().flatMap(v => new String('string'));
24+
assert.compareArray(Array.from(iter), ['s', 't', 'r', 'i', 'n', 'g']);

0 commit comments

Comments
 (0)