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) => {
test_with_window((w) => {
w.customElements.define('a-a', w.HTMLButtonElement);
assert_reports(w, w.TypeError, () => {
assert_reports_js(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, w.TypeError, () => {
assert_reports_js(w, w.TypeError, () => {
w.document.createElement('a-a');
}, 'If NewTarget is equal to active function object, TypeError should be ' +
'reported');
......
......@@ -76,7 +76,7 @@ function test_create_element_synchronous(description, define_and_create_element)
test_with_window((w) => {
const err = new Error('check this is reported');
err.name = 'reported';
assert_reports(w, err, () => {
assert_reports_exactly(w, err, () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); throw err; }
});
......@@ -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`);
test_with_window((w) => {
assert_reports(w, w.TypeError, () => {
assert_reports_js(w, w.TypeError, () => {
define_and_create_element(w, class {
constructor() {}
});
......@@ -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`);
test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => {
assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); this.setAttribute('a', 'a'); }
});
......@@ -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`);
test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => {
assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); this.appendChild(w.document.createElement('a')); }
});
......@@ -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`);
test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => {
assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); this.append('a'); }
});
......@@ -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`);
test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => {
assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); w.document.createElement('div').appendChild(this); }
});
......@@ -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`);
test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => {
assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); return w.document.implementation.createHTMLDocument().createElement('div'); }
});
......@@ -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
// NotSupportedError for some elements.
test_with_window((w) => {
assert_reports(w, w.TypeError, () => {
assert_reports_js(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'); }
});
......@@ -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)`);
test_with_window((w) => {
assert_reports(w, 'NotSupportedError', () => {
assert_reports_dom(w, 'NotSupportedError', () => {
define_and_create_element(w, class extends w.HTMLElement {
constructor() { super(); return document.createElement('div'); }
});
......
......@@ -52,9 +52,13 @@ function assert_is_upgraded(element, className, description) {
assert_equals(Object.getPrototypeOf(element), className.prototype, description);
}
// Asserts that func synchronously invokes the error event handler in w
// with the expected error.
function assert_reports(w, expected_error, func, description) {
// Asserts that func synchronously invokes the error event handler in w.
// Captures and returns the error that is reported.
//
// 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 errors = [];
w.onerror = (event, source, line_number, column_number, error) => {
......@@ -69,33 +73,26 @@ function assert_reports(w, expected_error, func, description) {
w.onerror = old_onerror;
}
assert_equals(errors.length, 1, 'only one error should have been reported');
// 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]; });
}
return errors[0];
}
// Asserts that func synchronously invokes the error event handler in w
// with the expected DOMException.
function assert_reports_dom(w, expected_error, func, description) {
const e = assert_reports_impl(w, func);
assert_throws_dom(expected_error, () => { throw e; }, description);
}
// 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) {
let old_onerror = w.onerror;
let errors = [];
w.onerror = (event, source, line_number, column_number, error) => {
errors.push(error);
return true; // the error is handled
};
try {
func();
} catch (e) {
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]; });
const e = assert_reports_impl(w, func);
assert_throws_js(expected_error, () => { throw e; }, description);
}
// Asserts that func synchronously invokes the error event handler in w
// with exactly the expected error.
function assert_reports_exactly(w, expected_error, func, description) {
const e = assert_reports_impl(w, func);
assert_throws_exactly(expected_error, () => { throw e; }, description);
}
......@@ -35,7 +35,7 @@ test_with_window(w => {
// Set element's custom element state to "failed".
// https://html.spec.whatwg.org/multipage/scripting.html#upgrades
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');
// "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