Skip to content

Commit

Permalink
[WebIDL] Add tests for iterator objects and prototypes (#8796)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyGu authored Feb 23, 2018
1 parent 998ec50 commit 73f03e3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
27 changes: 27 additions & 0 deletions WebIDL/ecmascript-binding/default-iterator-object.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!doctype html>
<meta charset="utf-8">
<title>Default iterator objects</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
test(() => {
const iterator1 = new URLSearchParams()[Symbol.iterator]();
const iterator2 = new URLSearchParams().keys();
const iterator3 = new URLSearchParams().values();
const iterator4 = new URLSearchParams().entries();
assert_equals(Object.getPrototypeOf(iterator1), Object.getPrototypeOf(iterator2));
assert_equals(Object.getPrototypeOf(iterator1), Object.getPrototypeOf(iterator3));
assert_equals(Object.getPrototypeOf(iterator1), Object.getPrototypeOf(iterator4));
}, "Default iterator objects for an interface have the same prototype");

test(() => {
const iterator = new URLSearchParams().entries();
assert_equals(Object.prototype.toString.call(iterator), "[object URLSearchParams Iterator]");
}, "Object.prototype.toString returns correct value");

test(() => {
const iterator = new URLSearchParams().entries();
assert_equals(iterator[Symbol.toStringTag], "URLSearchParams Iterator");
assert_equals(Object.getOwnPropertyDescriptor(iterator, Symbol.toStringTag), undefined);
}, "@@toStringTag has correct value from prototype");
</script>
56 changes: 56 additions & 0 deletions WebIDL/ecmascript-binding/iterator-prototype-object.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!doctype html>
<meta charset="utf-8">
<title>Iterator prototype objects</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
test(() => {
const esIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
const iteratorProto = Object.getPrototypeOf(new URLSearchParams().entries());
assert_equals(Object.getPrototypeOf(iteratorProto), esIteratorPrototype);
}, "Has %IteratorPrototype% as prototype");

test(() => {
const iteratorProto = Object.getPrototypeOf(new URLSearchParams().entries());
const desc = Object.getOwnPropertyDescriptor(iteratorProto, "next");
assert_equals(typeof desc.value, "function");
assert_equals(desc.writable, true);
assert_equals(desc.enumerable, true);
assert_equals(desc.configurable, true);
}, "next() exists and is writable, enumerable, and configurable");

test(() => {
const usp = new URLSearchParams();
const iteratorProto = Object.getPrototypeOf(usp.entries());

assert_throws(new TypeError(), () => {
iteratorProto.next();
});
assert_throws(new TypeError(), () => {
iteratorProto.next.call(undefined);
});
assert_throws(new TypeError(), () => {
iteratorProto.next.call(42);
});
assert_throws(new TypeError(), () => {
iteratorProto.next.call(new Headers().entries());
});
}, "next() throws TypeError when called on ineligible receiver");

test(() => {
const iteratorProto = Object.getPrototypeOf(new URLSearchParams().entries());
assert_equals(Object.prototype.toString.call(iteratorProto), "[object URLSearchParams Iterator]");
}, "Object.prototype.toString returns correct value");

test(() => {
const iteratorProto = Object.getPrototypeOf(new URLSearchParams().entries());
assert_equals(Object.getOwnPropertyDescriptor(iteratorProto, Symbol.toStringTag).value, "URLSearchParams Iterator");
// Property attributes have not yet been fully spec'd.
}, "@@toStringTag has correct value");

test(() => {
const iteratorProto1 = Object.getPrototypeOf(new URLSearchParams().entries());
const iteratorProto2 = Object.getPrototypeOf(new Headers().entries());
assert_not_equals(iteratorProto1, iteratorProto2);
}, "Is specific to an interface");
</script>

0 comments on commit 73f03e3

Please sign in to comment.