Implement pair iterators in WebIDL interfaces.

This commit is contained in:
Josh Matthews 2016-07-28 18:41:30 -04:00
parent 34bb937aee
commit 812a761abf
9 changed files with 390 additions and 13 deletions

View file

@ -51,4 +51,45 @@
assert_array_equals(entry, expected[i++]);
}
}, "Iterators iterate over values");
test(function() {
var t = new TestBindingPairIterable();
var empty = true;
t.forEach(function() { empty = false; });
assert_true(empty);
}, "Empty pair iterator");
test(function() {
var t = new TestBindingPairIterable();
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()));
}, "Pair iterable iterators are iterators");
test(function() {
var t = new TestBindingPairIterable();
t.add("first", 0);
t.add("second", 1);
t.add("third", 2);
assert_array_equals(collect(t.keys()), ["first", "second", "third"]);
assert_array_equals(collect(t.values()), [0, 1, 2]);
var expected = [["first", 0], ["second", 1], ["third", 2]];
var i = 0;
for (entry of t.entries()) {
assert_array_equals(entry, expected[i++]);
}
t.add("fourth", 3);
assert_array_equals(collect(t.keys()), ["first", "second", "third", "fourth"]);
assert_array_equals(collect(t.values()), [0, 1, 2, 3]);
var expected = [["first", 0], ["second", 1], ["third", 2], ["fourth", 3]];
var i = 0;
for (entry of t.entries()) {
assert_array_equals(entry, expected[i++]);
}
}, "Pair iterators iterate over key/value pairs");
</script>