-
Notifications
You must be signed in to change notification settings - Fork 507
Add tests for Joint Iteration proposal + split tests #4609
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
bakkot
wants to merge
18
commits into
tc39:main
Choose a base branch
from
bakkot:iterator-zip-split
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 13 commits
Commits
Show all changes
18 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 ba10b5f
split basic.js into harness+tests, add some more tests
bakkot 4ff876b
address my own comments
michaelficarra 0977346
fix bug
michaelficarra 0d5fe46
add back test for iterator that's not iterable
michaelficarra 57f18f8
fix syntax error
michaelficarra e457776
Merge branch 'main' into iterator-zip-split
michaelficarra 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,223 @@ | ||
| // Copyright (C) 2025 André Bargull. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
| /*--- | ||
| description: | | ||
| Utility functions for testing Iterator.prototype.zip and Iterator.prototype.zipKeyed. Requires inclusion of propertyHelper.js. | ||
| defines: | ||
| - forEachSequenceCombination | ||
| - forEachSequenceCombinationKeyed | ||
| - assertZipped | ||
| - assertZippedKeyed | ||
| - assertIteratorResult | ||
| - assertIsPackedArray | ||
| ---*/ | ||
|
|
||
| // Assert |result| is an object created by CreateIteratorResultObject. | ||
| function assertIteratorResult(result, value, done, label) { | ||
| assert.sameValue( | ||
| Object.getPrototypeOf(result), | ||
| Object.prototype, | ||
| label + ": [[Prototype]] of iterator result is Object.prototype" | ||
| ); | ||
|
|
||
| assert(Object.isExtensible(result), label + ": iterator result is extensible"); | ||
|
|
||
| var ownKeys = Reflect.ownKeys(result); | ||
| assert.compareArray(ownKeys, ["value", "done"], label + ": iterator result properties"); | ||
|
|
||
| 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, label) { | ||
| assert(Array.isArray(array), label + ": array is an array exotic object"); | ||
|
|
||
| assert.sameValue( | ||
| Object.getPrototypeOf(array), | ||
| Array.prototype, | ||
| label + ": [[Prototype]] of array is Array.prototype" | ||
| ); | ||
|
|
||
| assert(Object.isExtensible(array), label + ": 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, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Assert |array| is an extensible null-prototype object with default property attributes. | ||
| function _assertIsNullProtoMutableObject(object, label) { | ||
| assert.sameValue( | ||
| Object.getPrototypeOf(object), | ||
| null, | ||
| label + ": [[Prototype]] of object is null" | ||
| ); | ||
|
|
||
| assert(Object.isExtensible(object), label + ": object is extensible"); | ||
|
|
||
| // Ensure all properties have the default property attributes. | ||
| var keys = Object.getOwnPropertyNames(object); | ||
| for (var i = 0; i < keys.length; i++) { | ||
| verifyProperty(object, keys[i], { | ||
| writable: true, | ||
| enumerable: true, | ||
| configurable: true, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Assert that the `zipped` iterator results as for Iterator.zip for the first `count` elements. | ||
| // Assumes inputs is an array of arrays of length >= count. | ||
| // Advances `zipped` by `count` steps. | ||
| function assertZipped(zipped, inputs, count, label) { | ||
| // Last returned elements array. | ||
| var last = null; | ||
|
|
||
| for (var i = 0; i < count; i++) { | ||
| var itemLabel = label + ", step " + i; | ||
|
|
||
| var result = zipped.next(); | ||
| var value = result.value; | ||
|
|
||
| // Test IteratorResult structure. | ||
| assertIteratorResult(result, value, false, itemLabel); | ||
|
|
||
| // Ensure value is a new array. | ||
| assert.notSameValue(value, last, itemLabel + ": returns a new array"); | ||
| last = value; | ||
|
|
||
| // Ensure all array elements have the expected value. | ||
| var expected = inputs.map(function (array) { | ||
| return array[i]; | ||
| }); | ||
| assert.compareArray(value, expected, itemLabel + ": values"); | ||
|
|
||
| // Ensure value is a packed array with default data properties. | ||
| // | ||
| // This operation is destructive, so it has to happen last. | ||
| assertIsPackedArray(value, itemLabel); | ||
| } | ||
| } | ||
|
|
||
| // Assert that the `zipped` iterator results as for Iterator.zipKeyed for the first `count` elements. | ||
michaelficarra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Assumes inputs is an object whose values are arrays of length >= count. | ||
michaelficarra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Advances `zipped` by `count` steps. | ||
| function assertZippedKeyed(zipped, inputs, count, label) { | ||
| // Last returned elements array. | ||
| var last = null; | ||
|
|
||
| var expectedKeys = Object.keys(inputs); | ||
|
|
||
| for (var i = 0; i < count; i++) { | ||
| var itemLabel = label + ", step " + i; | ||
|
|
||
| var result = zipped.next(); | ||
| var value = result.value; | ||
|
|
||
| // Test IteratorResult structure. | ||
| assertIteratorResult(result, value, false, itemLabel); | ||
|
|
||
| // Ensure resulting object is a new object. | ||
| assert.notSameValue(value, last, itemLabel + ": returns a new object"); | ||
| last = value; | ||
|
|
||
| // Ensure resulting object has the expected keys and values. | ||
| assert.compareArray(Reflect.ownKeys(value), expectedKeys, itemLabel + ": result object keys"); | ||
|
|
||
| var expectedValues = Object.values(inputs).map(function (array) { | ||
| return array[i]; | ||
| }); | ||
| assert.compareArray(Object.values(value), expectedValues, itemLabel + ": result object values"); | ||
|
|
||
| // Ensure resulting object is a null-prototype mutable object with default data properties. | ||
| // | ||
| // This operation is destructive, so it has to happen last. | ||
| _assertIsNullProtoMutableObject(value, itemLabel); | ||
| } | ||
| } | ||
|
|
||
| function forEachSequenceCombinationKeyed(callback) { | ||
| return forEachSequenceCombination(function(inputs, inputsLabel, min, max) { | ||
| var object = {}; | ||
| for (var i = 0; i < inputs.length; ++i) { | ||
| object["prop_" + i] = inputs[i]; | ||
| } | ||
| inputsLabel = "inputs = " + JSON.stringify(object); | ||
| callback(object, inputsLabel, min, max); | ||
| }); | ||
| } | ||
|
|
||
| function forEachSequenceCombination(callback) { | ||
| function test(inputs) { | ||
| if (inputs.length === 0) { | ||
| callback(inputs, "inputs = []", 0, 0); | ||
| return; | ||
| } | ||
|
|
||
| var lengths = inputs.map(function(array) { | ||
| return array.length; | ||
| }); | ||
|
|
||
| var min = Math.min.apply(null, lengths); | ||
| var max = Math.max.apply(null, lengths); | ||
|
|
||
| var inputsLabel = "inputs = " + JSON.stringify(inputs); | ||
|
|
||
| callback(inputs, inputsLabel, min, max); | ||
| } | ||
|
|
||
| // Yield all prefixes of the string |s|. | ||
| function* prefixes(s) { | ||
| for (var i = 0; i <= s.length; ++i) { | ||
| yield s.slice(0, i); | ||
| } | ||
| } | ||
|
|
||
| // Zip an empty iterable. | ||
| test([]); | ||
|
|
||
| // Zip a single iterator. | ||
| for (var prefix of prefixes("abcd")) { | ||
| test([prefix.split("")]); | ||
| } | ||
|
|
||
| // Zip two iterators. | ||
| for (var prefix1 of prefixes("abcd")) { | ||
| for (var prefix2 of prefixes("efgh")) { | ||
| test([prefix1.split(""), prefix2.split("")]); | ||
| } | ||
| } | ||
|
|
||
| // Zip three iterators. | ||
| for (var prefix1 of prefixes("abcd")) { | ||
| for (var prefix2 of prefixes("efgh")) { | ||
| for (var prefix3 of prefixes("ijkl")) { | ||
| test([prefix1.split(""), prefix2.split(""), prefix3.split("")]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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,85 @@ | ||
| // 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 with "longest" mode. | ||
| includes: [compareArray.js, propertyHelper.js, iteratorZipUtils.js] | ||
| features: [joint-iteration] | ||
| ---*/ | ||
|
|
||
| function testSequence(inputs, inputsLabel, minLength, maxLength) { | ||
| function test(options, optionsLabel, getPaddingForInput) { | ||
| var label = optionsLabel + ", " + inputsLabel; | ||
| var it = Iterator.zip(inputs, options); | ||
| assertZipped(it, inputs, minLength, label); | ||
|
|
||
| for (var i = minLength; i < maxLength; i++) { | ||
| var itemLabel = label + ", step " + i; | ||
|
|
||
| var result = it.next(); | ||
| var value = result.value; | ||
|
|
||
| // Test IteratorResult structure. | ||
| assertIteratorResult(result, value, false, itemLabel); | ||
|
|
||
| var expected = inputs.map(function (input, j) { | ||
| return i < input.length ? input[i] : getPaddingForInput(j); | ||
| }); | ||
| assert.compareArray(value, expected, itemLabel + ": values"); | ||
| } | ||
| assertIteratorResult(it.next(), undefined, true, label + ": after completion"); | ||
| } | ||
|
|
||
| test( | ||
| { mode: "longest" }, | ||
| "options = { mode: 'longest' }", | ||
| function () { | ||
| return undefined; | ||
| }, | ||
| ); | ||
|
|
||
| test( | ||
| { mode: "longest", padding: [] }, | ||
| "options = { mode: 'longest', padding: [] }", | ||
| function () { | ||
| return undefined; | ||
| }, | ||
| ); | ||
|
|
||
| test( | ||
| { mode: "longest", padding: ["pad"] }, | ||
| "options = { mode: 'longest', padding: ['pad'] }", | ||
| function (idx) { | ||
| return idx === 0 ? "pad" : undefined; | ||
| }, | ||
| ); | ||
|
|
||
| test( | ||
| { mode: "longest", padding: Array(inputs.length).fill("pad") }, | ||
| "options = { mode: 'longest', padding: ['pad', 'pad', ..., 'pad'] }", | ||
| function (idx) { | ||
| return "pad"; | ||
| }, | ||
| ); | ||
|
|
||
| // Yield an infinite amount of numbers. | ||
| var numbers = { | ||
| *[Symbol.iterator]() { | ||
| var i = 0; | ||
| while (true) { | ||
| yield 100 + i++; | ||
| } | ||
| } | ||
| }; | ||
| test( | ||
| { mode: "longest", padding: numbers }, | ||
| "options = { mode: 'longest', padding: numbers }", | ||
| function (idx) { | ||
| return 100 + idx; | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| forEachSequenceCombination(testSequence); |
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,29 @@ | ||
| // 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 with "shortest" mode. | ||
| includes: [compareArray.js, propertyHelper.js, iteratorZipUtils.js] | ||
| features: [joint-iteration] | ||
| ---*/ | ||
|
|
||
| function testSequence(inputs, inputsLabel, minLength, maxLength) { | ||
| function test(options, optionsLabel) { | ||
| var label = optionsLabel + ", " + inputsLabel; | ||
| var it = Iterator.zip(inputs, options); | ||
| assertZipped(it, inputs, minLength, label); | ||
|
|
||
| // Iterator is closed after `minLength` items. | ||
| assertIteratorResult(it.next(), undefined, true, label + ": after completion"); | ||
| } | ||
|
|
||
| test(undefined, "options = undefined"); | ||
|
|
||
| test({}, "options = {}"); | ||
|
|
||
| test({ mode: "shortest" }, "options = { mode: 'shortest' }"); | ||
| } | ||
|
|
||
| forEachSequenceCombination(testSequence); |
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,30 @@ | ||
| // 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 with "strict" mode. | ||
| includes: [compareArray.js, propertyHelper.js, iteratorZipUtils.js] | ||
| features: [joint-iteration] | ||
| ---*/ | ||
|
|
||
| function testSequence(inputs, inputsLabel, minLength, maxLength) { | ||
| function test(options, optionsLabel) { | ||
| var label = optionsLabel + ", " + inputsLabel; | ||
| var it = Iterator.zip(inputs, options); | ||
| assertZipped(it, inputs, minLength, label); | ||
|
|
||
| if (minLength === maxLength) { | ||
| assertIteratorResult(it.next(), undefined, true, label + ": after completion"); | ||
| } else { | ||
| assert.throws(TypeError, function() { | ||
| it.next(); | ||
| }, label + " should throw after " + minLength + " items."); | ||
| } | ||
| } | ||
|
|
||
| test({ mode: "strict" }, "options = { mode: 'strict' }"); | ||
| } | ||
|
|
||
| forEachSequenceCombination(testSequence); |
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'" | ||
| ); |
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.
Uh oh!
There was an error while loading. Please reload this page.