Commit b2a39960 authored by Stephen McGruer's avatar Stephen McGruer Committed by Commit Bot

Remove 'assert_throws' usage in custom-elements/

Tackling the set of calls that avoided the automated refactor (for
various reasons). There are about 50 in total, split up into a few CLs
to make reviewing it easier.

Bug: 1051932
Change-Id: I83ebbfadd97056940fa69d82a2fc8959cdf2eb3d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2083388
Commit-Queue: Stephen McGruer <smcgruer@chromium.org>
Reviewed-by: default avatarMason Freed <masonfreed@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754544}
parent b5d03e09
......@@ -14,13 +14,13 @@ setup({
});
test_with_window((w) => {
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
w.HTMLElement();
}, 'calling the HTMLElement constructor should throw a TypeError');
}, 'HTMLElement constructor, invoke');
test_with_window((w) => {
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
new w.HTMLElement();
}, 'invoking the HTMLElement constructor with a construct call should ' +
'throw a TypeError');
......@@ -29,7 +29,7 @@ test_with_window((w) => {
test_with_window((w) => {
class X extends w.HTMLElement {}
w.customElements.define('a-a', X);
assert_throws(TypeError.prototype, () => {
assert_throws_js(TypeError, () => {
X();
}, 'calling a custom element constructor should throw a TypeError');
}, 'Custom element constructor, invoke');
......@@ -69,12 +69,12 @@ test_with_window((w) => {
class C extends w.HTMLElement { }
class D extends C {}
w.customElements.define('a-a', C); // Note: Not D.
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
new D();
}, 'constructing a custom element with a new.target with no registry ' +
'entry should throw a TypeError');
assert_throws(TypeError.prototype, () => {
assert_throws_js(TypeError, () => {
Reflect.construct(C, [], 42);
}, 'constructing a custom element with a non-object new.target should ' +
'throw a TypeError');
......@@ -82,7 +82,7 @@ test_with_window((w) => {
test_with_window((w) => {
w.customElements.define('a-a', w.HTMLButtonElement);
assert_reports(w, TypeError.prototype, () => {
assert_reports(w, w.TypeError, () => {
w.document.createElement('a-a');
}, 'If NewTarget is equal to active function object, TypeError should be ' +
'reported');
......@@ -90,7 +90,7 @@ test_with_window((w) => {
test_with_window((w) => {
w.customElements.define('a-a', class extends w.HTMLButtonElement {} );
assert_reports(w, TypeError.prototype, () => {
assert_reports(w, w.TypeError, () => {
w.document.createElement('a-a');
}, 'If NewTarget is equal to active function object, TypeError should be ' +
'reported');
......
......@@ -11,9 +11,6 @@ setup({
allow_uncaught_exception: true,
});
const expectTypeError = TypeError.prototype;
const expectNotSupportedError = 'NOT_SUPPORTED_ERR';
// https://dom.spec.whatwg.org/#concept-create-element
// 5. If definition is non-null and the definition represents a customized built-in element:
// 5.3. If the synchronous custom elements flag is set:
......@@ -87,7 +84,7 @@ function test_create_element_synchronous(description, define_and_create_element)
}, `${description}6.1.2. Errors in Construct(C) should be reported`);
test_with_window((w) => {
assert_reports(w, expectTypeError, () => {
assert_reports(w, w.TypeError, () => {
define_and_create_element(w, class {
constructor() {}
});
......@@ -95,7 +92,7 @@ function test_create_element_synchronous(description, define_and_create_element)
}, `${description}6.1.3. If result does not implement the HTMLElement interface, throw a TypeError`);
test_with_window((w) => {
assert_reports(w, expectNotSupportedError, () => {
assert_reports(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); this.setAttribute('a', 'a'); }
});
......@@ -103,7 +100,7 @@ function test_create_element_synchronous(description, define_and_create_element)
}, `${description}6.1.4. If result\'s attribute list is not empty, then throw a NotSupportedError`);
test_with_window((w) => {
assert_reports(w, expectNotSupportedError, () => {
assert_reports(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); this.appendChild(w.document.createElement('a')); }
});
......@@ -111,7 +108,7 @@ function test_create_element_synchronous(description, define_and_create_element)
}, `${description}6.1.5. If result has children, then throw a NotSupportedError`);
test_with_window((w) => {
assert_reports(w, expectNotSupportedError, () => {
assert_reports(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); this.append('a'); }
});
......@@ -119,7 +116,7 @@ function test_create_element_synchronous(description, define_and_create_element)
}, `${description}6.1.5. If result has children, then throw a NotSupportedError`);
test_with_window((w) => {
assert_reports(w, expectNotSupportedError, () => {
assert_reports(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); w.document.createElement('div').appendChild(this); }
});
......@@ -127,7 +124,7 @@ function test_create_element_synchronous(description, define_and_create_element)
}, `${description}6.1.6. If result\'s parent is not null, then throw a NotSupportedError`);
test_with_window((w) => {
assert_reports(w, expectNotSupportedError, () => {
assert_reports(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); return w.document.implementation.createHTMLDocument().createElement('div'); }
});
......@@ -137,7 +134,7 @@ function test_create_element_synchronous(description, define_and_create_element)
// See the note in step 6.1.8. In future, it may be possible to throw
// NotSupportedError for some elements.
test_with_window((w) => {
assert_reports(w, expectTypeError, () => {
assert_reports(w, w.TypeError, () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); return w.document.createElementNS('http://www.w3.org/2000/svg', 'g'); }
});
......@@ -145,7 +142,7 @@ function test_create_element_synchronous(description, define_and_create_element)
}, `${description}6.1.8. If result\'s namespace is not the HTML namespace, then throw (a NotSupportedError, currently TypeError)`);
test_with_window((w) => {
assert_reports(w, expectNotSupportedError, () => {
assert_reports(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); return document.createElement('div'); }
});
......
......@@ -13,13 +13,13 @@
'use strict';
test_with_window((w) => {
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
w.customElements.define('a-a', 42);
}, 'defining a number "constructor" should throw a TypeError');
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
w.customElements.define('a-a', () => {});
}, 'defining an arrow function "constructor" should throw a TypeError');
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
w.customElements.define('a-a', { m() {} }.m);
}, 'defining a concise method "constructor" should throw a TypeError');
}, 'A "constructor" that is not a constructor');
......@@ -107,8 +107,8 @@ promise_test((t) => {
.then(([w1, w2]) => {
class X extends w2.HTMLElement { };
w1.customElements.define('x-x', X);
assert_throws(
TypeError.prototype, () => new X(),
assert_throws_js(
w2.TypeError, () => new X(),
'the current global object (w2) should not find the definition in w1');
});
}, 'HTMLElement constructor looks up definitions in the current global');
......@@ -180,7 +180,7 @@ test_with_window((w) => {
'<first-name></first-name>');
test_with_window((w) => {
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
let not_a_constructor = () => {};
let invalid_name = 'annotation-xml';
w.customElements.define(invalid_name, not_a_constructor);
......@@ -265,10 +265,11 @@ test_with_window((w) => {
test_with_window((w) => {
class Y extends w.HTMLElement {}
let X = (function () {}).bind({});
let exception = { name: 42 };
Object.defineProperty(X, 'prototype', {
get() { throw { name: 42 }; }
get() { throw exception; }
});
assert_throws({ name: 42 }, () => {
assert_throws_exactly(exception, () => {
w.customElements.define('a-a', X);
}, 'should rethrow constructor exception');
w.customElements.define('a-a', Y);
......@@ -278,10 +279,11 @@ test_with_window((w) => {
// 14.1 Let prototype be Get(constructor, "prototype"). Rethrow any exceptions.
test_with_window((w) => {
let X = (function () {}).bind({});
let exception = {name: 'prototype throws' };
Object.defineProperty(X, 'prototype', {
get() { throw { name: 'prototype throws' }; }
get() { throw exception; }
});
assert_throws({ name: 'prototype throws' }, () => {
assert_throws_exactly(exception, () => {
w.customElements.define('a-a', X);
}, 'Exception from Get(constructor, prototype) should be rethrown');
}, 'Rethrow any exceptions thrown while getting prototype');
......@@ -290,7 +292,7 @@ test_with_window((w) => {
test_with_window((w) => {
function F() {}
F.prototype = 42;
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
w.customElements.define('a-a', F);
}, 'defining an element with a constructor with a prototype that is not an ' +
'object should throw a TypeError');
......@@ -305,10 +307,11 @@ let callbacks_in_reverse = ['attributeChangedCallback', 'disconnectedCallback',
function F_for_callbacks_in_reverse() {};
callbacks_in_reverse.forEach((callback) => {
test_with_window((w) => {
let exception = { name: callback };
Object.defineProperty(F_for_callbacks_in_reverse.prototype, callback, {
get() { throw { name: callback }; }
get() { throw exception; }
});
assert_throws({ name: callback }, () => {
assert_throws_exactly(exception, () => {
w.customElements.define('a-a', F_for_callbacks_in_reverse);
}, 'Exception from Get(prototype, callback) should be rethrown');
}, 'Rethrow any exceptions thrown while retrieving ' + callback);
......@@ -326,7 +329,7 @@ callbacks_in_reverse.forEach((callback) => {
Object.defineProperty(F.prototype, callback, {
get() { return {}; }
});
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
w.customElements.define('a-a', F);
}, 'defining an element with a constructor with a callback that is ' +
'not undefined and not callable should throw a TypeError');
......@@ -336,12 +339,13 @@ callbacks_in_reverse.forEach((callback) => {
// 14.9.2 Let observedAttributesIterable be Get(constructor, "observedAttributes").
// Rethrow any exceptions.
test_with_window((w) => {
let exception = { name: 'observedAttributes throws' };
class X extends w.HTMLElement{
constructor() { super(); }
attributeChangedCallback() {}
static get observedAttributes() { throw { name: 'observedAttributes throws' }; }
static get observedAttributes() { throw exception; }
}
assert_throws({ name: 'observedAttributes throws' }, () => {
assert_throws_exactly(exception, () => {
w.customElements.define('a-a', X);
}, 'Exception from Get(constructor, observedAttributes) should be rethrown');
}, 'Rethrow any exceptions thrown while getting observedAttributes');
......@@ -376,7 +380,7 @@ test_with_window((w) => {
let constructor = function () {};
constructor.prototype.attributeChangedCallback = function () { };
constructor.observedAttributes = {[Symbol.iterator]: 1};
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
w.customElements.define('a-a', constructor);
}, 'converting value that is not an object should throw TypeError');
}, 'Converting non-object observedAttributes to sequence<DOMString>');
......@@ -387,7 +391,7 @@ test_with_window((w) => {
attributeChangedCallback() {}
static get observedAttributes() { return new RegExp(); }
}
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
w.customElements.define('a-a', X);
}, 'converting RegExp should throw TypeError');
}, 'Converting regular expression observedAttributes to sequence<DOMString>');
......@@ -396,7 +400,7 @@ test_with_window((w) => {
let constructor = function () {};
constructor.prototype.attributeChangedCallback = function () { };
constructor.observedAttributes = {};
assert_throws(TypeError.prototype, () => {
assert_throws_js(w.TypeError, () => {
w.customElements.define('a-a', constructor);
}, 'If iterator method is undefined, it should throw TypeError');
}, 'Converting observedAttributes without iterator method to sequence<DOMString>');
......@@ -425,7 +429,7 @@ test_with_window((w) => {
return attributes;
}
}
assert_throws(TypeError.prototype, () => {
assert_throws_js(TypeError, () => {
w.customElements.define('x-x', X);
});
}, 'Throwing an exception in observedAttributes');
......
......@@ -21,9 +21,12 @@ function test_with_window(f, name, srcdoc) {
}, name);
}
// TODO(1066131): After https://github.com/web-platform-tests/wpt/pull/21876 is
// rolled into Chromium, this function can be replaced with:
// assert_throws_dom(code, global_context.DOMException, func, description);
function assert_throws_dom_exception(global_context, code, func, description) {
let exception;
assert_throws(code, () => {
assert_throws_dom(code, () => {
try {
func.call(this);
} catch(e) {
......@@ -66,9 +69,15 @@ function assert_reports(w, expected_error, func, description) {
w.onerror = old_onerror;
}
assert_equals(errors.length, 1, 'only one error should have been reported');
// assert_throws has an error expectation matcher that can only be
// accessed by throwing the error
assert_throws(expected_error, () => { throw errors[0]; });
// The testharness.js methods have error expectation matchers that can only be
// accessed by throwing the error.
if (typeof(expected_error) == 'string') {
assert_throws_dom(expected_error, () => { throw errors[0]; });
} else if (typeof(expected_error) == 'function') {
assert_throws_js(expected_error, () => { throw errors[0]; });
} else {
assert_throws_exactly(expected_error, () => { throw errors[0]; });
}
}
// Asserts that func synchronously invokes the error event handler in w
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment