Commit 614aabbe authored by Stephen McGruer's avatar Stephen McGruer Committed by Commit Bot

Split assert_reports into assert_reports_{dom, js, exactly}

This mirrors the WPT functions for assert_throws_{dom, js, exactly}, and
provides a clearer API for what is being tested. (Previously the code
would guess based on the type of the input).

Bug: None
Change-Id: I916636d9cf3561c3d6cde7cbb0b73a4a8c436dc5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2128379
Commit-Queue: Stephen McGruer <smcgruer@chromium.org>
Reviewed-by: default avatarMason Freed <masonfreed@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755074}
parent e3337bf3
...@@ -82,7 +82,7 @@ test_with_window((w) => { ...@@ -82,7 +82,7 @@ test_with_window((w) => {
test_with_window((w) => { test_with_window((w) => {
w.customElements.define('a-a', w.HTMLButtonElement); w.customElements.define('a-a', w.HTMLButtonElement);
assert_reports(w, w.TypeError, () => { assert_reports_js(w, w.TypeError, () => {
w.document.createElement('a-a'); w.document.createElement('a-a');
}, 'If NewTarget is equal to active function object, TypeError should be ' + }, 'If NewTarget is equal to active function object, TypeError should be ' +
'reported'); 'reported');
...@@ -90,7 +90,7 @@ test_with_window((w) => { ...@@ -90,7 +90,7 @@ test_with_window((w) => {
test_with_window((w) => { test_with_window((w) => {
w.customElements.define('a-a', class extends w.HTMLButtonElement {} ); w.customElements.define('a-a', class extends w.HTMLButtonElement {} );
assert_reports(w, w.TypeError, () => { assert_reports_js(w, w.TypeError, () => {
w.document.createElement('a-a'); w.document.createElement('a-a');
}, 'If NewTarget is equal to active function object, TypeError should be ' + }, 'If NewTarget is equal to active function object, TypeError should be ' +
'reported'); 'reported');
......
...@@ -76,7 +76,7 @@ function test_create_element_synchronous(description, define_and_create_element) ...@@ -76,7 +76,7 @@ function test_create_element_synchronous(description, define_and_create_element)
test_with_window((w) => { test_with_window((w) => {
const err = new Error('check this is reported'); const err = new Error('check this is reported');
err.name = 'reported'; err.name = 'reported';
assert_reports(w, err, () => { assert_reports_exactly(w, err, () => {
define_and_create_element(w, class extends w.HTMLElement { define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); throw err; } constructor() { super(); throw err; }
}); });
...@@ -84,7 +84,7 @@ function test_create_element_synchronous(description, define_and_create_element) ...@@ -84,7 +84,7 @@ function test_create_element_synchronous(description, define_and_create_element)
}, `${description}6.1.2. Errors in Construct(C) should be reported`); }, `${description}6.1.2. Errors in Construct(C) should be reported`);
test_with_window((w) => { test_with_window((w) => {
assert_reports(w, w.TypeError, () => { assert_reports_js(w, w.TypeError, () => {
define_and_create_element(w, class { define_and_create_element(w, class {
constructor() {} constructor() {}
}); });
...@@ -92,7 +92,7 @@ function test_create_element_synchronous(description, define_and_create_element) ...@@ -92,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`); }, `${description}6.1.3. If result does not implement the HTMLElement interface, throw a TypeError`);
test_with_window((w) => { test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => { assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement { define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); this.setAttribute('a', 'a'); } constructor() { super(); this.setAttribute('a', 'a'); }
}); });
...@@ -100,7 +100,7 @@ function test_create_element_synchronous(description, define_and_create_element) ...@@ -100,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`); }, `${description}6.1.4. If result\'s attribute list is not empty, then throw a NotSupportedError`);
test_with_window((w) => { test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => { assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement { define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); this.appendChild(w.document.createElement('a')); } constructor() { super(); this.appendChild(w.document.createElement('a')); }
}); });
...@@ -108,7 +108,7 @@ function test_create_element_synchronous(description, define_and_create_element) ...@@ -108,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`); }, `${description}6.1.5. If result has children, then throw a NotSupportedError`);
test_with_window((w) => { test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => { assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement { define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); this.append('a'); } constructor() { super(); this.append('a'); }
}); });
...@@ -116,7 +116,7 @@ function test_create_element_synchronous(description, define_and_create_element) ...@@ -116,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`); }, `${description}6.1.5. If result has children, then throw a NotSupportedError`);
test_with_window((w) => { test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => { assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement { define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); w.document.createElement('div').appendChild(this); } constructor() { super(); w.document.createElement('div').appendChild(this); }
}); });
...@@ -124,7 +124,7 @@ function test_create_element_synchronous(description, define_and_create_element) ...@@ -124,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`); }, `${description}6.1.6. If result\'s parent is not null, then throw a NotSupportedError`);
test_with_window((w) => { test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => { assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement { define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); return w.document.implementation.createHTMLDocument().createElement('div'); } constructor() { super(); return w.document.implementation.createHTMLDocument().createElement('div'); }
}); });
...@@ -134,7 +134,7 @@ function test_create_element_synchronous(description, define_and_create_element) ...@@ -134,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 // See the note in step 6.1.8. In future, it may be possible to throw
// NotSupportedError for some elements. // NotSupportedError for some elements.
test_with_window((w) => { test_with_window((w) => {
assert_reports(w, w.TypeError, () => { assert_reports_js(w, w.TypeError, () => {
define_and_create_element(w, class extends w.HTMLElement { define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); return w.document.createElementNS('http://www.w3.org/2000/svg', 'g'); } constructor() { super(); return w.document.createElementNS('http://www.w3.org/2000/svg', 'g'); }
}); });
...@@ -142,7 +142,7 @@ function test_create_element_synchronous(description, define_and_create_element) ...@@ -142,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)`); }, `${description}6.1.8. If result\'s namespace is not the HTML namespace, then throw (a NotSupportedError, currently TypeError)`);
test_with_window((w) => { test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => { assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement { define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); return document.createElement('div'); } constructor() { super(); return document.createElement('div'); }
}); });
......
...@@ -52,9 +52,13 @@ function assert_is_upgraded(element, className, description) { ...@@ -52,9 +52,13 @@ function assert_is_upgraded(element, className, description) {
assert_equals(Object.getPrototypeOf(element), className.prototype, description); assert_equals(Object.getPrototypeOf(element), className.prototype, description);
} }
// Asserts that func synchronously invokes the error event handler in w // Asserts that func synchronously invokes the error event handler in w.
// with the expected error. // Captures and returns the error that is reported.
function assert_reports(w, expected_error, func, description) { //
// Do not use this function directly; instead use one of assert_reports_js,
// assert_reports_dom, or assert_reports_exactly. Those functions also check
// that the error reported is the expected one.
function assert_reports_impl(w, func) {
let old_onerror = w.onerror; let old_onerror = w.onerror;
let errors = []; let errors = [];
w.onerror = (event, source, line_number, column_number, error) => { w.onerror = (event, source, line_number, column_number, error) => {
...@@ -69,33 +73,26 @@ function assert_reports(w, expected_error, func, description) { ...@@ -69,33 +73,26 @@ function assert_reports(w, expected_error, func, description) {
w.onerror = old_onerror; w.onerror = old_onerror;
} }
assert_equals(errors.length, 1, 'only one error should have been reported'); assert_equals(errors.length, 1, 'only one error should have been reported');
// The testharness.js methods have error expectation matchers that can only be return errors[0];
// accessed by throwing the error. }
if (typeof(expected_error) == 'string') {
assert_throws_dom(expected_error, () => { throw errors[0]; }); // Asserts that func synchronously invokes the error event handler in w
} else if (typeof(expected_error) == 'function') { // with the expected DOMException.
assert_throws_js(expected_error, () => { throw errors[0]; }); function assert_reports_dom(w, expected_error, func, description) {
} else { const e = assert_reports_impl(w, func);
assert_throws_exactly(expected_error, () => { throw errors[0]; }); assert_throws_dom(expected_error, () => { throw e; }, description);
}
} }
// Asserts that func synchronously invokes the error event handler in w // Asserts that func synchronously invokes the error event handler in w
// with the expected error. // with the expected JavaScript error.
function assert_reports_js(w, expected_error, func, description) { function assert_reports_js(w, expected_error, func, description) {
let old_onerror = w.onerror; const e = assert_reports_impl(w, func);
let errors = []; assert_throws_js(expected_error, () => { throw e; }, description);
w.onerror = (event, source, line_number, column_number, error) => { }
errors.push(error);
return true; // the error is handled // Asserts that func synchronously invokes the error event handler in w
}; // with exactly the expected error.
try { function assert_reports_exactly(w, expected_error, func, description) {
func(); const e = assert_reports_impl(w, func);
} catch (e) { assert_throws_exactly(expected_error, () => { throw e; }, description);
assert_unreached(`should report, not throw, an exception: ${e}`);
} finally {
w.onerror = old_onerror;
}
assert_equals(errors.length, 1, 'only one error should have been reported');
assert_throws_js(expected_error, () => { throw errors[0]; });
} }
...@@ -35,7 +35,7 @@ test_with_window(w => { ...@@ -35,7 +35,7 @@ test_with_window(w => {
// Set element's custom element state to "failed". // Set element's custom element state to "failed".
// https://html.spec.whatwg.org/multipage/scripting.html#upgrades // https://html.spec.whatwg.org/multipage/scripting.html#upgrades
let container = document.body; let container = document.body;
assert_reports(window, error, () => container.appendChild(element)); assert_reports_exactly(window, error, () => container.appendChild(element));
assert_equals(constructorCount, 1, 'constructor should be invoked once'); assert_equals(constructorCount, 1, 'constructor should be invoked once');
// "failed" is not "defined" // "failed" is not "defined"
......
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