-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1594459
commit f44bc81
Showing
7 changed files
with
215 additions
and
6 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
test/built-ins/Iterator/prototype/flatMap/flattens-iteratable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.flatMap | ||
description: > | ||
Iterator.prototype.flatMap flattens iterables returned by the mapper | ||
info: | | ||
%Iterator.prototype%.flatMap ( mapper ) | ||
includes: [iterators.js, compareArray.js] | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
|
||
function* g() { | ||
yield 0; | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
} | ||
|
||
let iter = g().flatMap((v, count) => { | ||
let result = []; | ||
for (let i = 0; i < v; ++i) { | ||
result.push(v); | ||
} | ||
return result; | ||
}); | ||
|
||
assert.compareArray(Array.from(iter), [1, 2, 2, 3, 3, 3]); |
42 changes: 42 additions & 0 deletions
42
test/built-ins/Iterator/prototype/flatMap/flattens-iterator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.flatMap | ||
description: > | ||
Iterator.prototype.flatMap flattens non-iterable iterators returned by the mapper | ||
info: | | ||
%Iterator.prototype%.flatMap ( mapper ) | ||
includes: [iterators.js, compareArray.js] | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
|
||
function* g() { | ||
yield 0; | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
} | ||
|
||
let iter = g().flatMap((v, count) => { | ||
let i = 0; | ||
return { | ||
next: function() { | ||
if (i < v) { | ||
++i; | ||
return { | ||
value: v, | ||
done: false | ||
}; | ||
} else { | ||
return { | ||
value: undefined, | ||
done: true | ||
}; | ||
} | ||
} | ||
}; | ||
}); | ||
|
||
assert.compareArray(Array.from(iter), [1, 2, 2, 3, 3, 3]); |
34 changes: 34 additions & 0 deletions
34
test/built-ins/Iterator/prototype/flatMap/flattens-only-depth-1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.flatMap | ||
description: > | ||
Iterator.prototype.flatMap does not flatten recursively | ||
info: | | ||
%Iterator.prototype%.flatMap ( mapper ) | ||
includes: [iterators.js, compareArray.js] | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
|
||
let arr = [ | ||
{ | ||
[Symbol.iterator]: function() { | ||
throw new Test262Error; | ||
} | ||
}, | ||
{ | ||
next: function() { | ||
throw new Test262Error; | ||
} | ||
} | ||
]; | ||
|
||
function* g() { | ||
yield arr; | ||
} | ||
|
||
let iter = g().flatMap(v => v); | ||
|
||
assert.compareArray(Array.from(iter), arr); |
35 changes: 35 additions & 0 deletions
35
test/built-ins/Iterator/prototype/flatMap/iterable-primitives-are-not-flattened.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.flatMap | ||
description: > | ||
Iterator.prototype.flatMap does not respect the iterability of any primitive | ||
info: | | ||
%Iterator.prototype%.flatMap ( mapper ) | ||
includes: [iterators.js, compareArray.js] | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
|
||
function* g() { | ||
yield 0; | ||
} | ||
|
||
Number.prototype[Symbol.iterator] = function* () { | ||
let i = 0; | ||
let target = this >>> 0; | ||
while (i < target) { | ||
yield i; | ||
++i; | ||
} | ||
}; | ||
|
||
assert.compareArray(Array.from(5), [0, 1, 2, 3, 4]); | ||
|
||
assert.throws(TypeError, function () { | ||
for(let unused of g().flatMap(v => 5)); | ||
}); | ||
|
||
let iter = g().flatMap(v => new Number(5)); | ||
assert.compareArray(Array.from(iter), [0, 1, 2, 3, 4]); |
33 changes: 33 additions & 0 deletions
33
test/built-ins/Iterator/prototype/flatMap/iterable-to-iterator-fallback.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.flatMap | ||
description: > | ||
Iterator.prototype.flatMap falls back to treating mapper return values as iterators if the Symbol.iterator property is not callable | ||
info: | | ||
%Iterator.prototype%.flatMap ( mapper ) | ||
includes: [iterators.js, compareArray.js] | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
|
||
function* g() { | ||
yield 0; | ||
} | ||
|
||
function* h() { | ||
yield 0; | ||
yield 1; | ||
yield 2; | ||
} | ||
|
||
let iter = g().flatMap(v => { | ||
let n = h(); | ||
return { | ||
[Symbol.iterator]: 0, | ||
next: () => n.next() | ||
}; | ||
}); | ||
|
||
assert.compareArray(Array.from(iter), [0, 1, 2]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
test/built-ins/Iterator/prototype/flatMap/strings-are-not-flattened.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.flatMap | ||
description: > | ||
Iterator.prototype.flatMap does not respect the iterability of primitive strings | ||
info: | | ||
%Iterator.prototype%.flatMap ( mapper ) | ||
includes: [iterators.js, compareArray.js] | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
|
||
function* g() { | ||
yield 0; | ||
} | ||
|
||
assert.throws(TypeError, function () { | ||
for(let unused of g().flatMap(v => 'string')); | ||
}); | ||
|
||
let iter = g().flatMap(v => new String('string')); | ||
assert.compareArray(Array.from(iter), ['s', 't', 'r', 'i', 'n', 'g']); |