Skip to content
Permalink
Browse files

Add WPT test for invalidating WebIDL iterators during forEach

  • Loading branch information
Manishearth committed Nov 26, 2019
1 parent d233558 commit 38a666742fbfcd5b392d8644622af890c0ff25cd
{}
]
],
"WebIDL/ecmascript-binding/iterator-invalidation-foreach.html": [
[
"WebIDL/ecmascript-binding/iterator-invalidation-foreach.html",
{}
]
],
"WebIDL/ecmascript-binding/iterator-prototype-object.html": [
[
"WebIDL/ecmascript-binding/iterator-prototype-object.html",
"03ada7aa0d4d43811652fc679a00a41b9653013d",
"testharness"
],
"WebIDL/ecmascript-binding/iterator-invalidation-foreach.html": [
"d6498c3e388e0c637830fa080cca78b0ab0e5305",
"testharness"
],
"WebIDL/ecmascript-binding/iterator-prototype-object.html": [
"5a935fa20135d88a7268b872b68ab7fe548ab5c7",
"testharness"
@@ -0,0 +1,40 @@
<!doctype html>
<meta charset="utf-8">
<title>Behavior of iterators when modified within foreach</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://heycam.github.io/webidl/#es-forEach">
<link rel="author" title="Manish Goregaokar" href="mailto:manishsmail@gmail.com">
<script>
test(function() {
let params = new URLSearchParams("a=1&b=2&c=3");
let arr = [];
params.forEach((p) => {
arr.push(p);
params.delete("b");
})
assert_array_equals(arr, ["1", "3"]);
}, "forEach will not iterate over elements removed during iteration");
test(function() {
let params = new URLSearchParams("a=1&b=2&c=3&d=4");
let arr = [];
params.forEach((p) => {
arr.push(p);
if (p == "2") {
params.delete("a");
}
})
assert_array_equals(arr, ["1", "2", "4"]);
}, "Removing elements already iterated over during forEach will cause iterator to skip an element");
test(function() {
let params = new URLSearchParams("a=1&b=2&c=3&d=4");
let arr = [];
params.forEach((p) => {
arr.push(p);
if (p == "2") {
params.append("e", "5");
}
})
assert_array_equals(arr, ["1", "2", "3", "4", "5"]);
}, "Elements added during iteration with forEach will be reached");
</script>

0 comments on commit 38a6667

Please sign in to comment.
You can’t perform that action at this time.