Skip to content

Commit

Permalink
add tests for reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelficarra committed May 25, 2023
1 parent 4b504a3 commit 84bf66f
Show file tree
Hide file tree
Showing 29 changed files with 827 additions and 0 deletions.
58 changes: 58 additions & 0 deletions test/built-ins/Iterator/prototype/reduce/argument-effect-order.js
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']);
13 changes: 13 additions & 0 deletions test/built-ins/Iterator/prototype/reduce/callable.js
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);
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 test/built-ins/Iterator/prototype/reduce/get-next-method-throws.js
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(() => {});
});
10 changes: 10 additions & 0 deletions test/built-ins/Iterator/prototype/reduce/is-function.js
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');
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);
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(() => {});
});
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);
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);
22 changes: 22 additions & 0 deletions test/built-ins/Iterator/prototype/reduce/length.js
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,
});
29 changes: 29 additions & 0 deletions test/built-ins/Iterator/prototype/reduce/name.js
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,
});
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(() => {});
});
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(() => {});
});
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);
Loading

0 comments on commit 84bf66f

Please sign in to comment.