-
Notifications
You must be signed in to change notification settings - Fork 462
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
4b504a3
commit 84bf66f
Showing
29 changed files
with
827 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
test/built-ins/Iterator/prototype/reduce/argument-effect-order.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,58 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Arguments and this value are evaluated in the correct order | ||
info: | | ||
%Iterator.prototype%.reduce ( reducer ) | ||
includes: [compareArray.js] | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
let effects = []; | ||
|
||
assert.throws(TypeError, function () { | ||
Iterator.prototype.reduce.call( | ||
{ | ||
get next() { | ||
effects.push('get next'); | ||
return function () { | ||
return { done: true, value: undefined }; | ||
}; | ||
}, | ||
}, | ||
{ | ||
valueOf() { | ||
effects.push('reducer valueOf'); | ||
} | ||
}, | ||
{ | ||
valueOf() { | ||
effects.push('initial value valueOf'); | ||
} | ||
} | ||
); | ||
}); | ||
|
||
assert.compareArray(effects, []); | ||
|
||
Iterator.prototype.reduce.call( | ||
{ | ||
get next() { | ||
effects.push('get next'); | ||
return function () { | ||
return { done: true, value: undefined }; | ||
}; | ||
}, | ||
}, | ||
() => {}, | ||
{ | ||
valueOf() { | ||
effects.push('initial value valueOf'); | ||
} | ||
} | ||
); | ||
|
||
assert.compareArray(effects, ['get next']); |
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,13 @@ | ||
// Copyright (C) 2020 Rick Waldron. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Iterator.prototype.reduce is callable | ||
features: [iterator-helpers] | ||
---*/ | ||
function* g() {} | ||
Iterator.prototype.reduce.call(g(), () => {}, 0); | ||
|
||
let iter = g(); | ||
iter.reduce(() => {}, 0); |
36 changes: 36 additions & 0 deletions
36
test/built-ins/Iterator/prototype/reduce/get-next-method-only-once.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,36 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Gets the next method from the iterator only once | ||
info: | | ||
%Iterator.prototype%.reduce ( reducer ) | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
let nextGets = 0; | ||
|
||
class TestIterator extends Iterator { | ||
get next() { | ||
++nextGets; | ||
let counter = 5; | ||
return function () { | ||
if (counter <= 0) { | ||
return { done: true, value: undefined }; | ||
} else { | ||
return { done: false, value: --counter }; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
let iterator = new TestIterator(); | ||
|
||
assert.sameValue(nextGets, 0); | ||
assert.sameValue( | ||
iterator.reduce(x => x), | ||
4 | ||
); | ||
assert.sameValue(nextGets, 1); |
23 changes: 23 additions & 0 deletions
23
test/built-ins/Iterator/prototype/reduce/get-next-method-throws.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,23 @@ | ||
// Copyright (C) 2020 Rick Waldron. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Iterator has throwing next getter | ||
info: | | ||
%Iterator.prototype%.reduce ( reducer ) | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
class IteratorThrows extends Iterator { | ||
get next() { | ||
throw new Test262Error(); | ||
} | ||
} | ||
|
||
let iterator = new IteratorThrows(); | ||
|
||
assert.throws(Test262Error, function () { | ||
iterator.reduce(() => {}); | ||
}); |
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,10 @@ | ||
// Copyright (C) 2020 Rick Waldron. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Iterator.prototype.reduce is a built-in function | ||
features: [iterator-helpers] | ||
---*/ | ||
|
||
assert.sameValue(typeof Iterator.prototype.reduce, 'function'); |
21 changes: 21 additions & 0 deletions
21
test/built-ins/Iterator/prototype/reduce/iterator-already-exhausted-initial-value.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,21 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Iterator.prototype.reduce returns the initial value when the iterator has already been exhausted | ||
info: | | ||
%Iterator.prototype%.reduce ( reducer ) | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
let iterator = (function* () {})(); | ||
|
||
let { value, done } = iterator.next(); | ||
assert.sameValue(value, undefined); | ||
assert.sameValue(done, true); | ||
|
||
const initialValue = {}; | ||
let result = iterator.reduce(() => {}, initialValue); | ||
assert.sameValue(result, initialValue); |
21 changes: 21 additions & 0 deletions
21
test/built-ins/Iterator/prototype/reduce/iterator-already-exhausted-no-initial-value.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,21 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Iterator.prototype.reduce without an initial value throws when the iterator has already been exhausted | ||
info: | | ||
%Iterator.prototype%.reduce ( reducer ) | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
let iterator = (function* () {})(); | ||
|
||
let { value, done } = iterator.next(); | ||
assert.sameValue(value, undefined); | ||
assert.sameValue(done, true); | ||
|
||
assert.throws(TypeError, function() { | ||
iterator.reduce(() => {}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
test/built-ins/Iterator/prototype/reduce/iterator-yields-once-initial-value.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.reduce | ||
description: > | ||
Iterator.prototype.reduce calls reducer once for each value yielded by the underlying iterator when passed an initial value | ||
info: | | ||
%Iterator.prototype%.reduce ( reducer ) | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
function* g() { | ||
yield 'a'; | ||
} | ||
|
||
let iter = g(); | ||
|
||
let assertionCount = 0; | ||
let initialValue = {}; | ||
let result = iter.reduce((memo, v, count) => { | ||
switch (v) { | ||
case 'a': | ||
assert.sameValue(memo, initialValue); | ||
assert.sameValue(count, 0); | ||
break; | ||
default: | ||
throw new Error(); | ||
} | ||
++assertionCount; | ||
return v; | ||
}, initialValue); | ||
|
||
assert.sameValue(result, 'a'); | ||
assert.sameValue(assertionCount, 1); |
26 changes: 26 additions & 0 deletions
26
test/built-ins/Iterator/prototype/reduce/iterator-yields-once-no-initial-value.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,26 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Iterator.prototype.reduce calls reducer once for each value yielded by the underlying iterator except the first when not passed an initial value | ||
info: | | ||
%Iterator.prototype%.reduce ( reducer ) | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
function* g() { | ||
yield 'a'; | ||
} | ||
|
||
let iter = g(); | ||
|
||
let assertionCount = 0; | ||
let result = iter.reduce((memo, v, count) => { | ||
++assertionCount; | ||
return v; | ||
}); | ||
|
||
assert.sameValue(result, 'a'); | ||
assert.sameValue(assertionCount, 0); |
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,22 @@ | ||
// Copyright (C) 2020 Rick Waldron. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Iterator.prototype.reduce has a "length" property whose value is 1. | ||
info: | | ||
ECMAScript Standard Built-in Objects | ||
Unless otherwise specified, the length property of a built-in | ||
Function object has the attributes { [[Writable]]: false, [[Enumerable]]: | ||
false, [[Configurable]]: true }. | ||
features: [iterator-helpers] | ||
includes: [propertyHelper.js] | ||
---*/ | ||
|
||
verifyProperty(Iterator.prototype.reduce, 'length', { | ||
value: 1, | ||
writable: false, | ||
enumerable: false, | ||
configurable: true, | ||
}); |
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,29 @@ | ||
// Copyright (C) 2020 Rick Waldron. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
The "name" property of Iterator.prototype.reduce | ||
info: | | ||
17 ECMAScript Standard Built-in Objects | ||
Every built-in Function object, including constructors, that is not | ||
identified as an anonymous function has a name property whose value is a | ||
String. Unless otherwise specified, this value is the name that is given to | ||
the function in this specification. | ||
... | ||
Unless otherwise specified, the name property of a built-in Function | ||
object, if it exists, has the attributes { [[Writable]]: false, | ||
[[Enumerable]]: false, [[Configurable]]: true }. | ||
features: [iterator-helpers] | ||
includes: [propertyHelper.js] | ||
---*/ | ||
|
||
verifyProperty(Iterator.prototype.reduce, 'name', { | ||
value: 'reduce', | ||
writable: false, | ||
enumerable: false, | ||
configurable: true, | ||
}); |
23 changes: 23 additions & 0 deletions
23
test/built-ins/Iterator/prototype/reduce/next-method-returns-non-object.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,23 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Underlying iterator next returns non-object | ||
info: | | ||
%Iterator.prototype%.reduce ( reducer ) | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
class NonObjectIterator extends Iterator { | ||
next() { | ||
return null; | ||
} | ||
} | ||
|
||
let iterator = new NonObjectIterator(); | ||
|
||
assert.throws(TypeError, function () { | ||
iterator.reduce(() => {}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
test/built-ins/Iterator/prototype/reduce/next-method-returns-throwing-done.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,31 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Underlying iterator next returns object with throwing done getter | ||
info: | | ||
%Iterator.prototype%.reduce ( reducer ) | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
class ThrowingIterator extends Iterator { | ||
next() { | ||
return { | ||
get done() { | ||
throw new Test262Error(); | ||
}, | ||
value: 1, | ||
}; | ||
} | ||
return() { | ||
throw new Error(); | ||
} | ||
} | ||
|
||
let iterator = new ThrowingIterator(); | ||
|
||
assert.throws(Test262Error, function () { | ||
iterator.reduce(() => {}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
test/built-ins/Iterator/prototype/reduce/next-method-returns-throwing-value-done.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,28 @@ | ||
// Copyright (C) 2023 Michael Ficarra. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-iteratorprototype.reduce | ||
description: > | ||
Underlying iterator next returns object with throwing value getter, but is already done | ||
info: | | ||
%Iterator.prototype%.reduce ( reducer ) | ||
features: [iterator-helpers] | ||
flags: [] | ||
---*/ | ||
class ThrowingIterator extends Iterator { | ||
next() { | ||
return { | ||
done: true, | ||
get value() { | ||
throw new Test262Error(); | ||
}, | ||
}; | ||
} | ||
return() { | ||
throw new Error(); | ||
} | ||
} | ||
|
||
let iterator = new ThrowingIterator(); | ||
iterator.reduce(() => {}, 0); |
Oops, something went wrong.