mirror of https://github.com/tc39/test262.git
Add tests for `%WrapForValidIteratorPrototype%.return()` from Iterator Helpers Proposal (#4214)
* Add tests for `%WrapForValidIteratorPrototype%.return()` * Address review - Remove unused `flags - Do not use `deepEqual.js` - Assert `return` never got when `this` value is invalid - Use `TemporalHelpers` to observer get / call
This commit is contained in:
parent
77e98fb683
commit
8cbcc309e6
|
@ -0,0 +1,35 @@
|
||||||
|
// Copyright (C) 2024 Sosuke Suzuki. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-iterator.from
|
||||||
|
description: >
|
||||||
|
Gets the base iterator return method when the wrapper return method is called.
|
||||||
|
info: |
|
||||||
|
%WrapForValidIteratorPrototype%.return ( )
|
||||||
|
...
|
||||||
|
5. Let returnMethod be ? GetMethod(iterator, "return").
|
||||||
|
|
||||||
|
features: [iterator-helpers]
|
||||||
|
includes: [temporalHelpers.js, compareArray.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
const calls = [];
|
||||||
|
|
||||||
|
const iter = TemporalHelpers.propertyBagObserver(calls, {
|
||||||
|
return () {
|
||||||
|
return { value: 5, done: true };
|
||||||
|
},
|
||||||
|
}, "originalIter");
|
||||||
|
|
||||||
|
const wrapper = Iterator.from(iter);
|
||||||
|
assert.compareArray(calls, [
|
||||||
|
"get originalIter[Symbol.iterator]",
|
||||||
|
"get originalIter.next",
|
||||||
|
]);
|
||||||
|
|
||||||
|
wrapper.return();
|
||||||
|
assert.compareArray(calls, [
|
||||||
|
"get originalIter[Symbol.iterator]",
|
||||||
|
"get originalIter.next",
|
||||||
|
"get originalIter.return"
|
||||||
|
]);
|
|
@ -0,0 +1,41 @@
|
||||||
|
// Copyright (C) 2024 Sosuke Suzuki. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-iterator.from
|
||||||
|
description: >
|
||||||
|
%WrapForValidIteratorPrototype%.return() call base iterator's return method when it exists.
|
||||||
|
info: |
|
||||||
|
%WrapForValidIteratorPrototype%.return ( )
|
||||||
|
5. Let returnMethod be ? GetMethod(iterator, "return").
|
||||||
|
6. If returnMethod is undefined, then
|
||||||
|
...
|
||||||
|
7. Return ? Call(returnMethod, iterator).
|
||||||
|
|
||||||
|
features: [iterator-helpers]
|
||||||
|
includes: [temporalHelpers.js, compareArray.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
const calls = [];
|
||||||
|
|
||||||
|
const expectedIteratorResult = { value: 5, done: true };
|
||||||
|
const originalIter = {
|
||||||
|
return () {
|
||||||
|
return expectedIteratorResult;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
TemporalHelpers.observeMethod(calls, originalIter, "return", "originalIter");
|
||||||
|
const iter = TemporalHelpers.propertyBagObserver(calls, originalIter, "originalIter");
|
||||||
|
|
||||||
|
const wrapper = Iterator.from(iter);
|
||||||
|
assert.compareArray(calls, [
|
||||||
|
"get originalIter[Symbol.iterator]",
|
||||||
|
"get originalIter.next",
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.sameValue(wrapper.return(), expectedIteratorResult);
|
||||||
|
assert.compareArray(calls, [
|
||||||
|
"get originalIter[Symbol.iterator]",
|
||||||
|
"get originalIter.next",
|
||||||
|
"get originalIter.return",
|
||||||
|
"call originalIter.return",
|
||||||
|
]);
|
|
@ -0,0 +1,23 @@
|
||||||
|
// Copyright (C) 2024 Sosuke Suzuki. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-iterator.from
|
||||||
|
description: >
|
||||||
|
%WrapForValidIteratorPrototype%.return() should return an iterator result object that value is undefined when base object does not have return method.
|
||||||
|
info: |
|
||||||
|
%WrapForValidIteratorPrototype%.return ( )
|
||||||
|
...
|
||||||
|
5. Let returnMethod be ? GetMethod(iterator, "return").
|
||||||
|
6. If returnMethod is undefined, then
|
||||||
|
a. Return CreateIterResultObject(undefined, true).
|
||||||
|
|
||||||
|
features: [iterator-helpers]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
const iter = {};
|
||||||
|
const wrapper = Iterator.from(iter);
|
||||||
|
|
||||||
|
const result = wrapper.return();
|
||||||
|
assert(result.hasOwnProperty("value"));
|
||||||
|
assert.sameValue(result.value, undefined);
|
||||||
|
assert.sameValue(result.done, true);
|
|
@ -0,0 +1,39 @@
|
||||||
|
// Copyright (C) 2024 Sosuke Suzuki. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
esid: sec-iterator.from
|
||||||
|
description: >
|
||||||
|
%WrapForValidIteratorPrototype%.return() requires [[iterated]] internal slot
|
||||||
|
info: |
|
||||||
|
%WrapForValidIteratorPrototype%.return ( )
|
||||||
|
...
|
||||||
|
2. Perform ? RequireInternalSlot(O, [[Iterated]]).
|
||||||
|
|
||||||
|
features: [iterator-helpers]
|
||||||
|
includes: [temporalHelpers.js, compareArray.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
const WrapForValidIteratorPrototype = Object.getPrototypeOf(Iterator.from({}));
|
||||||
|
|
||||||
|
{
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
WrapForValidIteratorPrototype.return.call({});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const originalIter = {
|
||||||
|
return() {
|
||||||
|
return { value: 5, done: true };
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const calls = [];
|
||||||
|
TemporalHelpers.observeMethod(calls, originalIter, "return", "originalIter");
|
||||||
|
const iter = TemporalHelpers.propertyBagObserver(calls, originalIter, "originalIter");
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
WrapForValidIteratorPrototype.return.call(iter);
|
||||||
|
});
|
||||||
|
assert.compareArray(calls, []);
|
||||||
|
}
|
Loading…
Reference in New Issue