Skip to content

Commit

Permalink
Add tests for %WrapForValidIteratorPrototype%.return()
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Aug 30, 2024
1 parent d62fa93 commit f617443
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/built-ins/Iterator/from/get-return-method-when-call-return.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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]
flags: []
---*/

let returnGetCount = 0;
const iter = {
get return() {
returnGetCount++;
return function () {
return { value: 5, done: true };
};
},
};
const wrapper = Iterator.from(iter);
assert.sameValue(returnGetCount, 0);

wrapper.return();
assert.sameValue(returnGetCount, 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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).
includes: [deepEqual.js]
features: [iterator-helpers]
flags: []
---*/

let returnCallCount = 0;
const iter = {
return () {
returnCallCount++;
return { value: 5, done: true };
},
};
const wrapper = Iterator.from(iter);

assert.deepEqual(wrapper.return(), { value: 5, done: true });
assert.sameValue(returnCallCount, 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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).
includes: [deepEqual.js]
features: [iterator-helpers]
flags: []
---*/

const iter = {};
const wrapper = Iterator.from(iter);

assert.deepEqual(wrapper.return(), { value: undefined, done: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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]
flags: []
---*/

const iter = {};
const WrapForValidIteratorPrototype = Object.getPrototypeOf(Iterator.from(iter));

assert.throws(TypeError, function() {
WrapForValidIteratorPrototype.return.call({});
});

let returnCallCount = 0;
assert.throws(TypeError, function() {
WrapForValidIteratorPrototype.return.call({
return () {
returnCallCount++;
return { value: 5, done: true };
}
});
});
assert.sameValue(returnCallCount, 0);

0 comments on commit f617443

Please sign in to comment.