Skip to content

Commit

Permalink
Bug 1615031 [wpt PR 21762] - Eliminate last uses of assert_throws., a…
Browse files Browse the repository at this point in the history
…=testonly

Automatic update from web-platform-tests
Eliminate last uses of assert_throws.

The one remaining use inside promise_rejects will go away soon when
promise_rejects goes away.

--

wpt-commits: e4727526d79aa4add8c0f23b64b5fd46df391cc7
wpt-pr: 21762
  • Loading branch information
bzbarsky authored and moz-wptsync-bot committed Feb 17, 2020
1 parent aafa60e commit 5ef4d6b
Show file tree
Hide file tree
Showing 32 changed files with 86 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<script>
promise_test(t => {
let evil = false;
assert_throws(new EvalError(), _ => {
assert_throws_js(EvalError, _ => {
eval("evil = '1234567890123456789012345678901234567890';");
});
assert_false(evil);
Expand All @@ -21,7 +21,7 @@
}, "Unsafe eval violation sample is clipped to 40 characters.");

promise_test(t => {
assert_throws(new EvalError(), _ => {
assert_throws_js(EvalError, _ => {
new Function("a", "b", "return '1234567890123456789012345678901234567890';");
});
return waitUntilCSPEventForTrustedTypes(t).then(t.step_func_done(e => {
Expand All @@ -32,7 +32,7 @@

promise_test(t => {
const a = document.createElement("a");
assert_throws(new TypeError(), _ => {
assert_throws_js(TypeError, _ => {
a.innerHTML = "1234567890123456789012345678901234567890xxxx";
});
assert_equals(a.innerHTML, "");
Expand Down
13 changes: 0 additions & 13 deletions testing/web-platform/tests/cors/support.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
// For ignoring exception names (just for testing)
/*
_real_assert_throws = assert_throws;
function assert_throws(d, func, desc) {
try {
func();
} catch(e) {
return true;
}
assert_unreached("Didn't throw!");
}
*/

function dirname(path) {
return path.replace(/\/[^\/]*$/, '/')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

test(() => {
let i = (new TestElement()).internals;
assert_throws(new TypeError(), () => { i.states.supports('foo'); });
assert_throws(new SyntaxError(), () => { i.states.add(''); });
assert_throws('InvalidCharacterError', () => { i.states.add('a\tb'); });
assert_throws_js(TypeError, () => { i.states.supports('foo'); });
assert_throws_dom('SyntaxError', () => { i.states.add(''); });
assert_throws_dom('InvalidCharacterError', () => { i.states.add('a\tb'); });
}, 'DOMTokenList behavior of ElementInternals.states: Exceptions');

test(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
customElements.define('container-element', ContainerElement);

test(() => {
assert_throws(new SyntaxError(), () => { document.querySelector(':state'); });
assert_throws(new SyntaxError(), () => { document.querySelector(':state('); });
assert_throws(new SyntaxError(), () => { document.querySelector(':state()'); });
assert_throws(new SyntaxError(), () => { document.querySelector(':state(=)'); });
assert_throws(new SyntaxError(), () => { document.querySelector(':state(name=value)'); });
assert_throws(new SyntaxError(), () => { document.querySelector(':state( foo bar)'); });
assert_throws(new SyntaxError(), () => { document.querySelector(':state(16px)'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state('); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state()'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state(=)'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state(name=value)'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state( foo bar)'); });
assert_throws_dom('SyntaxError', () => { document.querySelector(':state(16px)'); });
}, ':state() parsing failures');

test(() => {
Expand Down
31 changes: 23 additions & 8 deletions testing/web-platform/tests/docs/writing-tests/testharness-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,18 @@ test may begin to execute before the returned promise has settled. Use
resetting global state that need to happen consistently before the next test
starts.

`promise_rejects` can be used to test Promises that need to reject:
`promise_rejects_dom`, `promise_rejects_js`, and `promise_rejects_exactly` can
be used to test Promises that need to reject:

```js
promise_rejects(test_object, code, promise, description)
promise_rejects_dom(test_object, code, promise, description)
promise_rejects_js(test_object, constructor, promise, description)
promise_rejects_exactly(test_object, value, promise, description)
```

The `code` argument is equivalent to the same argument to the `assert_throws`
function.
The `code`, `constructor`, and `value` arguments are equivalent to the same
argument to the `assert_throws_dom`, `assert_throws_js`, and
`assert_throws_exactly` functions.

Here's an example where the `bar()` function returns a Promise that rejects
with a TypeError:
Expand Down Expand Up @@ -854,17 +858,28 @@ attribute attribute_name following the conditions specified by WebIDL
### `assert_readonly(object, property_name, description)`
assert that property `property_name` on object is readonly
### `assert_throws(code, func, description)`
### `assert_throws_dom(code, func, description)`
`code` - the expected exception. This can take several forms:
* string - asserts that the thrown exception must be a DOMException
with the given name, e.g., "TimeoutError". (For
compatibility with existing tests, the name of a
DOMException constant can also be given, e.g.,
"TIMEOUT_ERR")
* object - asserts that the thrown exception must be any other kind
of exception, with a property called "name" that matches
`code.name`.
* number - asserts that the thrown exception must be a DOMException
with the fiven code value (e.g. DOMException.TIMEOUT_ERR).
`func` - a function that should throw
### `assert_throws_js(constructor, func, description)`
`constructor` - the expected exception. This is the constructor object
that the exception should have as its .constructor. For example,
`TypeError` or `someOtherWindow.RangeError`.

`func` - a function that should throw

### `assert_throws_exactly(value, func, description)`
`valie` - the exact value that `func` is expected to throw if called.

`func` - a function that should throw

Expand Down
2 changes: 1 addition & 1 deletion testing/web-platform/tests/fetch/http-cache/http-cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global btoa fetch token promise_test step_timeout */
/* global assert_equals assert_true assert_own_property assert_throws assert_less_than */
/* global assert_equals assert_true assert_own_property assert_throws_js assert_less_than */

const templates = {
'fresh': {
Expand Down
2 changes: 1 addition & 1 deletion testing/web-platform/tests/html/dom/new-harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ ReflectionHarness.test = function(fun, description) {

ReflectionHarness.assertEquals = assert_equals;

ReflectionHarness.assertThrows = assert_throws;
ReflectionHarness.assertThrows = assert_throws_dom;
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exports.expectScopes = (inputArray, baseURL, outputArray, warnings = []) => {

exports.expectBad = (input, baseURL, warnings = []) => {
const checkWarnings = testWarningHandler(warnings);
expect(() => parseFromString(input, baseURL)).toThrow(TypeError);
expect(parseFromString(input, baseURL)).toThrow('TypeError');
checkWarnings();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { expectBad, expectWarnings, expectSpecifierMap } = require('./helpers/par
const nonObjectStrings = ['null', 'true', '1', '"foo"', '[]'];

test('Invalid JSON', () => {
expect(() => parseFromString('{ imports: {} }', 'https://base.example/')).toThrow(SyntaxError);
expect(parseFromString('{ imports: {} }', 'https://base.example/')).toThrow('SyntaxError');
});

describe('Mismatching the top-level schema', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ function expect(v) {
return {
toMatchURL: expected => assert_equals(v, expected),
toThrow: expected => {
if (expected.test && expected.test('not yet implemented')) {
if (v.localName === 'iframe') {
// `v` is the result of parseFromString(), and thus toThrow()
// should be examining the error in that iframe.
assert_throws_js(v.contentWindow[expected], () => { throw v.contentWindow.windowError });
} else if (expected.test && expected.test('not yet implemented')) {
// We override /not yet implemented/ expectation.
assert_throws_js(TypeError, v);
} else {
assert_throws(expected(), v);
assert_throws_js(expected, v);
}
},
toEqual: expected => {
Expand Down Expand Up @@ -116,11 +120,6 @@ function parseFromString(mapString, mapBaseURL) {
`);
iframe.contentDocument.close();

// Rethrow window's error event.
if (iframe.contentWindow.windowError) {
throw iframe.contentWindow.windowError;
}

return iframe;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ function test_method_exists(method, method_name, properties)
wp_test(function() { assert_true(typeof method === 'function', msg); }, msg, properties);
}

function test_method_throw_exception(func_str, exception, msg)
{
var exception_name = typeof exception === "object" ? exception.name : exception;
var msg = 'Invocation of ' + func_str + ' should throw ' + exception_name + ' Exception.';
wp_test(function() { assert_throws(exception, function() {eval(func_str)}, msg); }, msg);
}

function test_noless_than(value, greater_than, msg, properties)
{
wp_test(function () { assert_true(value >= greater_than, msg); }, msg, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ <h1>Sample HTML5 API Tests</h1>
{
"status_string": "FAIL",
"name": "Test throw SyntaxError DOMException where JS SyntaxError expected; expected to fail",
"message": "assert_throws_js: function \"function () {document.querySelector(\"\")}\" threw object \"SyntaxError: '' is not a valid selector\" (\"SyntaxError\") expected instance of function \"function SyntaxError() {\n [native code]\n}\" (\"SyntaxError\")",
"message": "assert_throws_js: function \"function () {document.querySelector(\"\")}\" threw object \"SyntaxError: Document.querySelector: '' is not a valid selector\" (\"SyntaxError\") expected instance of function \"function SyntaxError() {\n [native code]\n}\" (\"SyntaxError\")",
"properties": {}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
const member = i.members[0];
assert_true(member.isUnscopable);
mock_interface_A({});
// assert_throws can't be used because it rethrows AssertionErrors.
// assert_throws_* can't be used because they rethrow AssertionErrors.
try {
i.do_member_unscopable_asserts(member);
} catch(e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<script>
'use strict';

// Neither the `assert_throws` function nor its variants may be used for this
// The `assert_throws_*` functions cannot be used for this
// purpose because they fail in response to AssertionError exceptions, even
// when this is expressed as the expected error.
function test_failure(fn, name) {
Expand Down
5 changes: 2 additions & 3 deletions testing/web-platform/tests/resources/testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ policies and contribution forms [3].

function promise_rejects(test, expected, promise, description) {
return promise.then(test.unreached_func("Should have rejected: " + description)).catch(function(e) {
assert_throws(expected, function() { throw e }, description);
assert_throws_DO_NOT_USE(expected, function() { throw e }, description);
});
}

Expand Down Expand Up @@ -1474,7 +1474,7 @@ policies and contribution forms [3].
* @param {Function} func Function which should throw.
* @param {string} description Error description for the case that the error is not thrown.
*/
function assert_throws(code, func, description)
function assert_throws_DO_NOT_USE(code, func, description)
{
try {
func.call(this);
Expand Down Expand Up @@ -1615,7 +1615,6 @@ policies and contribution forms [3].
}
}
}
expose(assert_throws, "assert_throws");

/**
* Assert a JS Error with the expected constructor is thrown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
const request = new Request(url, test.request_init);

if (test.should_reject) {
return assert_promise_rejects(
return promise_rejects_js(
t,
frame.contentWindow.TypeError,
frame.contentWindow.fetch(request),
new TypeError(),
'Must fail to fetch: url=' + url);
}
return frame.contentWindow.fetch(request).then((response) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,6 @@
* environments, so code should for example not rely on the DOM.
*/

// Returns a promise that fulfills after the provided |promise| is fulfilled.
// The |test| succeeds only if |promise| rejects with an exception matching
// |code|. Accepted values for |code| follow those accepted for assert_throws().
// The optional |description| describes the test being performed.
//
// E.g.:
// assert_promise_rejects(
// new Promise(...), // something that should throw an exception.
// 'NotFoundError',
// 'Should throw NotFoundError.');
//
// assert_promise_rejects(
// new Promise(...),
// new TypeError(),
// 'Should throw TypeError');
function assert_promise_rejects(promise, code, description) {
return promise.then(
function() {
throw 'assert_promise_rejects: ' + description + ' Promise did not reject.';
},
function(e) {
if (code !== undefined) {
assert_throws(code, function() { throw e; }, description);
}
});
}

// Asserts that two objects |actual| and |expected| are weakly equal under the
// following definition:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
speechSynthesis.speak(utter);
// the spec doesn't say what exception to throw:
// https://github.com/w3c/speech-api/issues/8
assert_throws(null, () => {
let threw = false;
try {
iframe.contentWindow.speechSynthesis.speak(utter);
});
} catch (e) {
threw = true;
}
assert_true(threw);
}));
}, 'Using the same SpeechSynthesisUtterance with two SpeechSynthesis instances');
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
container.appendChild(new_script);
assert_equals(inserted_script.textContent, "3*4");

// We expect 3 SPV events: two for the two assert_throws cases, and one
// We expect 3 SPV events: two for the two assert_throws_js cases, and one
// for script element, which will be rejected at the time of execution.
return checkSecurityPolicyViolationEvent(3);
}, "Spot tests around script + innerHTML interaction.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
});
}

// Like assert_throws, but we don't care about the exact error. We just want
// Like assert_throws_*, but we don't care about the exact error. We just want
// to run the code and continue.
function expect_throws(fn) {
try { fn(); assert_unreached(); } catch (err) { /* ignore */ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
});
}

// Like assert_throws, but we don't care about the exact error. We just want
// Like assert_throws_*, but we don't care about the exact error. We just want
// to run the code and continue.
function expect_throws(fn) {
try { fn(); assert_unreached(); } catch (err) { /* ignore */ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
});
}

// Like assert_throws, but we don't care about the exact error. We just want
// Like assert_throws_*, but we don't care about the exact error. We just want
// to run the code and continue.
function expect_throws(fn) {
try { fn(); assert_unreached(); } catch (err) { /* ignore */ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
});
}

// Like assert_throws, but we don't care about the exact error. We just want
// Like assert_throws_*, but we don't care about the exact error. We just want
// to run the code and continue.
function expect_throws(fn) {
try { fn(); assert_unreached(); } catch (err) { /* ignore */ }
Expand Down
6 changes: 3 additions & 3 deletions testing/web-platform/tests/user-timing/measure_exception.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ <h1>Description</h1>
<div id="log"></div>
<script>
performance.mark('ExistMark');
test_method_throw_exception('performance.measure()', TypeError());
test_method_throw_exception('performance.measure()', TypeError);
test_method_throw_exception('performance.measure("Exception1", "NonExistMark1")', 'SYNTAX_ERR');
test_method_throw_exception('performance.measure("Exception2", "NonExistMark1", "navigationStart")', 'SYNTAX_ERR');
test_method_throw_exception('performance.measure("Exception3", "navigationStart", "NonExistMark1")', 'SYNTAX_ERR');
test_method_throw_exception('performance.measure("Exception4", "NonExistMark1", "ExistMark")', 'SYNTAX_ERR');
test_method_throw_exception('performance.measure("Exception5", "ExistMark", "NonExistMark1")', 'SYNTAX_ERR');
test_method_throw_exception('performance.measure("Exception6", "NonExistMark1", "NonExistMark2")', 'SYNTAX_ERR');
test_method_throw_exception('performance.measure("Exception7", "redirectStart")', 'INVALID_ACCESS_ERR');
test_method_throw_exception('performance.measure("Exception8", {"detail": "non-empty"})', TypeError());
test_method_throw_exception('performance.measure("Exception9", {"start": 1, "duration": 2, "end": 3})', TypeError());
test_method_throw_exception('performance.measure("Exception8", {"detail": "non-empty"})', TypeError);
test_method_throw_exception('performance.measure("Exception9", {"start": 1, "duration": 2, "end": 3})', TypeError);
</script>
</body>
</html>
Loading

0 comments on commit 5ef4d6b

Please sign in to comment.