-
Notifications
You must be signed in to change notification settings - Fork 506
Add tests for Joint Iteration proposal #4528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anba
wants to merge
12
commits into
tc39:main
Choose a base branch
from
anba:iterator-zip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1dca44e
Add standard built-ins tests for Iterator.zip and Iterator.zipKeyed
anba 347bce9
Test options processing for zip and zipKeyed
anba 7e98f44
Test iterables processing for zip
anba 480f662
Test padding processing for zip
anba 1d71d90
Test basic functionality for zip
anba 986de0b
Test IteratorZip iteration for zip
anba 31e846b
Add tests from #4496 for zip
anba 49b20c0
Test iterables processing for zipKeyed
anba 7fcd411
Test padding processing for zipKeyed
anba 933a459
Test basic functionality for zipKeyed
anba b4c1c67
Test IteratorZip iteration for zipKeyed
anba 2593444
Add tests from #4496 for zipKeyed
anba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,236 @@ | ||
| // Copyright (C) 2025 André Bargull. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| esid: sec-iterator.zip | ||
| description: > | ||
| Basic Iterator.zip test using all three possible modes. | ||
| includes: [compareArray.js, propertyHelper.js] | ||
| features: [joint-iteration] | ||
| ---*/ | ||
|
|
||
| // Assert |result| is an object created by CreateIteratorResultObject. | ||
| function assertIteratorResult(result, value, done) { | ||
| assert.sameValue( | ||
| Object.getPrototypeOf(result), | ||
| Object.prototype, | ||
| "[[Prototype]] of iterator result is Object.prototype" | ||
| ); | ||
|
|
||
| assert(Object.isExtensible(result), "iterator result is extensible"); | ||
|
|
||
| var ownKeys = Reflect.ownKeys(result); | ||
| assert.sameValue(ownKeys.length, 2, "iterator result has two own properties"); | ||
| assert.sameValue(ownKeys[0], "value", "first property is 'value'"); | ||
| assert.sameValue(ownKeys[1], "done", "second property is 'done'"); | ||
|
|
||
| verifyProperty(result, "value", { | ||
| value: value, | ||
| writable: true, | ||
| enumerable: true, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| verifyProperty(result, "done", { | ||
| value: done, | ||
| writable: true, | ||
| enumerable: true, | ||
| configurable: true, | ||
| }); | ||
| } | ||
|
|
||
| // Assert |array| is a packed array with default property attributes. | ||
| function assertIsPackedArray(array) { | ||
| assert(Array.isArray(array), "array is an array exotic object"); | ||
|
|
||
| assert.sameValue( | ||
| Object.getPrototypeOf(array), | ||
| Array.prototype, | ||
| "[[Prototype]] of array is Array.prototype" | ||
| ); | ||
|
|
||
| assert(Object.isExtensible(array), "array is extensible"); | ||
|
|
||
| // Ensure "length" property has its default property attributes. | ||
| verifyProperty(array, "length", { | ||
| writable: true, | ||
| enumerable: false, | ||
| configurable: false, | ||
| }); | ||
|
|
||
| // Ensure no holes and all elements have the default property attributes. | ||
| for (var i = 0; i < array.length; i++) { | ||
| verifyProperty(array, i, { | ||
| writable: true, | ||
| enumerable: true, | ||
| configurable: true, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Yield all prefixes of the string |s|. | ||
| function* prefixes(s) { | ||
| for (var i = 0; i <= s.length; ++i) { | ||
| yield s.slice(0, i); | ||
| } | ||
| } | ||
|
|
||
| // Empty iterable doesn't yield any values. | ||
| var empty = { | ||
| *[Symbol.iterator]() { | ||
| } | ||
| }; | ||
|
|
||
| // Yield a single value. | ||
| var single = { | ||
| *[Symbol.iterator]() { | ||
| yield 1000; | ||
| } | ||
| }; | ||
|
|
||
| // Yield an infinite amount of numbers. | ||
| var numbers = { | ||
| *[Symbol.iterator]() { | ||
| var i = 0; | ||
| while (true) { | ||
| yield 100 + i++; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // |iterables| is an array whose elements are array(-like) objects. Pass it as | ||
| // the "iterables" argument to |Iterator.zip|, using |options| as the "options" | ||
| // argument. | ||
| // | ||
| // Then iterate over the returned |Iterator.zip| iterator and check all | ||
| // returned iteration values have the expected values. | ||
| function test(iterables, options) { | ||
| var mode = (options && options.mode) || "shortest"; | ||
| var padding = options && options.padding; | ||
|
|
||
| var lengths = iterables.map(function(array) { | ||
| return array.length; | ||
| }); | ||
|
|
||
| var min = Math.min.apply(null, lengths); | ||
| var max = Math.max.apply(null, lengths); | ||
|
|
||
| // Expected number of iterations. | ||
| var count; | ||
| switch (mode) { | ||
| case "shortest": | ||
| count = min; | ||
| break; | ||
| case "longest": | ||
| count = max; | ||
| break; | ||
| case "strict": | ||
| count = max; | ||
| break; | ||
| } | ||
|
|
||
| // Compute padding array when |mode| is "longest". | ||
| if (mode === "longest") { | ||
| if (padding) { | ||
| padding = Iterator.from(padding).take(iterables.length).toArray(); | ||
| } else { | ||
| padding = []; | ||
| } | ||
|
|
||
| // Fill with undefined until there are exactly |iterables.length| elements. | ||
| padding = padding.concat(Array(iterables.length - padding.length).fill(undefined)); | ||
| assert.sameValue(padding.length, iterables.length); | ||
| } | ||
|
|
||
| // Last returned elements array. | ||
| var last = null; | ||
|
|
||
| var it = Iterator.zip(iterables, options); | ||
| for (var i = 0; i < count; i++) { | ||
| // "strict" mode throws an error if number of elements don't match. | ||
| if (mode === "strict" && min < max && i === min) { | ||
| assert.throws(TypeError, function() { | ||
| it.next(); | ||
| }); | ||
| break; | ||
| } | ||
|
|
||
| var result = it.next(); | ||
| var value = result.value; | ||
|
|
||
| // Test IteratorResult structure. | ||
| assertIteratorResult(result, value, false); | ||
|
|
||
| // Ensure value is a new array. | ||
| assert.notSameValue(value, last, "returns a new array"); | ||
| last = value; | ||
|
|
||
| // Ensure all array elements have the expected value. | ||
| var expected = iterables.map(function(array, k) { | ||
| if (i < array.length) { | ||
| return array[i]; | ||
| } | ||
| assert.sameValue(mode, "longest", "padding is only used for 'longest' mode"); | ||
| return padding[k]; | ||
| }); | ||
| assert.compareArray(value, expected); | ||
|
|
||
| // Ensure value is a packed array with default data properties. | ||
| // | ||
| // This operation is destructive, so it has to happen last. | ||
| assertIsPackedArray(value); | ||
| } | ||
|
|
||
| // Iterator is closed. | ||
| assertIteratorResult(it.next(), undefined, true); | ||
| } | ||
|
|
||
| var validOptions = [ | ||
| undefined, | ||
| {}, | ||
| {mode: "shortest"}, | ||
| {mode: "longest"}, | ||
| {mode: "longest", padding: empty}, | ||
| {mode: "longest", padding: single}, | ||
| {mode: "longest", padding: numbers}, | ||
| {mode: "strict"}, | ||
| ]; | ||
|
|
||
| for (var options of validOptions) { | ||
| // Zip an empty iterable. | ||
| var it = Iterator.zip([], options); | ||
| assertIteratorResult(it.next(), undefined, true); | ||
|
|
||
| // Zip a single iterator. | ||
| for (var prefix of prefixes("abcd")) { | ||
| // Split prefix into an array. | ||
| test([prefix.split("")], options); | ||
|
|
||
| // Use String wrapper as the iterable. | ||
| test([new String(prefix)], options); | ||
| } | ||
|
|
||
| // Zip two iterators. | ||
| for (var prefix1 of prefixes("abcd")) { | ||
| for (var prefix2 of prefixes("efgh")) { | ||
| // Split prefixes into arrays. | ||
| test([prefix1.split(""), prefix2.split("")], options); | ||
|
|
||
| // Use String wrappers as the iterables. | ||
| test([new String(prefix1), new String(prefix2)], options); | ||
| } | ||
| } | ||
|
|
||
| // Zip three iterators. | ||
| for (var prefix1 of prefixes("abcd")) { | ||
| for (var prefix2 of prefixes("efgh")) { | ||
| for (var prefix3 of prefixes("ijkl")) { | ||
| // Split prefixes into arrays. | ||
| test([prefix1.split(""), prefix2.split(""), prefix3.split("")], options); | ||
|
|
||
| // Use String wrappers as the iterables. | ||
| test([new String(prefix1), new String(prefix2), new String(prefix3)], options); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,15 @@ | ||
| // Copyright (C) 2025 André Bargull. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| esid: sec-iterator.zip | ||
| description: > | ||
| Iterator.zip is a built-in function | ||
| features: [joint-iteration] | ||
| ---*/ | ||
|
|
||
| assert.sameValue( | ||
| typeof Iterator.zip, | ||
| "function", | ||
| "The value of `typeof Iterator.zip` is 'function'" | ||
| ); |
51 changes: 51 additions & 0 deletions
51
test/built-ins/Iterator/zip/iterables-iteration-after-reading-options.js
This file contains hidden or 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,51 @@ | ||
| // Copyright (C) 2025 André Bargull. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| esid: sec-iterator.zip | ||
| description: > | ||
| Perform iteration of the "iterables" argument after reading all properties. | ||
| info: | | ||
| Iterator.zip ( iterables [ , options ] ) | ||
| ... | ||
| 3. Let mode be ? Get(options, "mode"). | ||
| ... | ||
| 7. If mode is "longest", then | ||
| a. Set paddingOption to ? Get(options, "padding"). | ||
| ... | ||
| 10. Let inputIter be ? GetIterator(iterables, sync). | ||
| ... | ||
| includes: [compareArray.js] | ||
| features: [joint-iteration] | ||
| ---*/ | ||
|
|
||
| var log = []; | ||
|
|
||
| var iterables = { | ||
| [Symbol.iterator]() { | ||
| log.push("get iterator"); | ||
| return this; | ||
| }, | ||
| next() { | ||
| return {done: true}; | ||
| } | ||
| }; | ||
|
|
||
| var options = { | ||
| get mode() { | ||
| log.push("get mode"); | ||
| return "longest"; | ||
| }, | ||
| get padding() { | ||
| log.push("get padding"); | ||
| return []; | ||
| } | ||
| }; | ||
|
|
||
| Iterator.zip(iterables, options); | ||
|
|
||
| assert.compareArray(log, [ | ||
| "get mode", | ||
| "get padding", | ||
| "get iterator", | ||
| ]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer we don't compute a large list of expected results inline in a test using what is essentially a mini-polyfill. When the test fails, it's unclear whether the implementation or the mini-polyfill is wrong.
There was some more discussion on this re. locale-sensitive testing in the talk I presented in the April TC39 meeting. slides, discussion
Same for
zipKeyed/basic.js.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is really a mini-polyfill, just verifying several properties at once. I've split it into two tests + a new harness file, if you think that's an improvement: anba#1