Support value iterators in WebIDL interfaces.

This commit is contained in:
Josh Matthews 2016-07-28 18:41:24 -04:00
parent 221bc84693
commit 34bb937aee
7 changed files with 161 additions and 0 deletions

View file

@ -6858,6 +6858,12 @@
"url": "/_mozilla/mozilla/interfaces.worker"
}
],
"mozilla/iterable.html": [
{
"path": "mozilla/iterable.html",
"url": "/_mozilla/mozilla/iterable.html"
}
],
"mozilla/lenient_this.html": [
{
"path": "mozilla/lenient_this.html",

View file

@ -0,0 +1,3 @@
[iterable.html]
type: testharness
prefs: [dom.testbinding.enabled:true]

View file

@ -0,0 +1,54 @@
<!doctype html>
<meta charset="utf-8">
<title>Value and pair iterable bindings</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
function collect(iter) {
var collection = [];
for (element of iter) {
collection.push(element);
}
return collection;
}
test(function() {
var t = new TestBindingIterable();
var empty = true;
t.forEach(function() { empty = false; });
assert_true(empty);
}, "Empty value iterator");
test(function() {
var t = new TestBindingIterable();
function is_iterator(o) {
return o[Symbol.iterator]() === o;
}
assert_true(is_iterator(t.keys()));
assert_true(is_iterator(t.values()));
assert_true(is_iterator(t.entries()));
}, "Iterable iterators are iterators");
test(function() {
var t = new TestBindingIterable();
t.add("first");
t.add("second");
t.add("third");
assert_array_equals(collect(t.keys()), [0, 1, 2]);
assert_array_equals(collect(t.values()), ["first", "second", "third"]);
var expected = [[0, "first"], [1, "second"], [2, "third"]];
var i = 0;
for (entry of t.entries()) {
assert_array_equals(entry, expected[i++]);
}
t.add("fourth");
assert_array_equals(collect(t.keys()), [0, 1, 2, 3]);
assert_array_equals(collect(t.values()), ["first", "second", "third", "fourth"]);
var expected = [[0, "first"], [1, "second"], [2, "third"], [3, "fourth"]];
var i = 0;
for (entry of t.entries()) {
assert_array_equals(entry, expected[i++]);
}
}, "Iterators iterate over values");
</script>