mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-19 16:25:38 +00:00
Bug 1452706 - Make the 'expected' argument to Assert.throws/rejects required rather than optional. r=mikedeboer
MozReview-Commit-ID: 25flGw5sWga --HG-- extra : rebase_source : 5212db5e4f940906646078542641f449d46a1684
This commit is contained in:
parent
2a76b96907
commit
0e18da0420
@ -327,6 +327,19 @@ proto.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
|||||||
this.report(actual === expected, actual, expected, message, "!==");
|
this.report(actual === expected, actual, expected, message, "!==");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function checkExpectedArgument(instance, funcName, expected) {
|
||||||
|
if (!expected) {
|
||||||
|
instance.ok(false, `Error: The 'expected' argument was not supplied to Assert.${funcName}()`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!instanceOf(expected, "RegExp") &&
|
||||||
|
typeof expected !== "function" &&
|
||||||
|
typeof expected !== "object") {
|
||||||
|
instance.ok(false,
|
||||||
|
`Error: The 'expected' argument to Assert.${funcName}() must be a RegExp, function or an object`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function expectedException(actual, expected) {
|
function expectedException(actual, expected) {
|
||||||
if (!actual || !expected) {
|
if (!actual || !expected) {
|
||||||
return false;
|
return false;
|
||||||
@ -356,27 +369,21 @@ function expectedException(actual, expected) {
|
|||||||
* Assert.throws(() => testBody(), TypeError);
|
* Assert.throws(() => testBody(), TypeError);
|
||||||
* // The following will verify that an error was thrown with an error message matching "hello":
|
* // The following will verify that an error was thrown with an error message matching "hello":
|
||||||
* Assert.throws(() => testBody(), /hello/);
|
* Assert.throws(() => testBody(), /hello/);
|
||||||
* // The following will verify that any error was thrown and will use "hello" in the test report:
|
|
||||||
* Assert.throws(() => testBody(), "hello");
|
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* @param block
|
* @param block
|
||||||
* (function) Function block to evaluate and catch eventual thrown errors
|
* (function) Function block to evaluate and catch eventual thrown errors
|
||||||
* @param expected (optional)
|
* @param expected (optional)
|
||||||
* (mixed) This parameter can be either a RegExp, a function, or a string. The
|
* (mixed) This parameter can be either a RegExp or a function. The
|
||||||
* function is either the error type's constructor, or it's a method that returns a boolean
|
* function is either the error type's constructor, or it's a method that returns a boolean
|
||||||
* that describes the test outcome. When string value is provided, it will be used as if it
|
* that describes the test outcome.
|
||||||
* was provided as the message parameter.
|
|
||||||
* @param message (optional)
|
* @param message (optional)
|
||||||
* (string) Short explanation of the expected result
|
* (string) Short explanation of the expected result
|
||||||
*/
|
*/
|
||||||
proto.throws = function(block, expected, message) {
|
proto.throws = function(block, expected, message) {
|
||||||
let actual;
|
checkExpectedArgument(this, "throws", expected);
|
||||||
|
|
||||||
if (typeof expected === "string") {
|
let actual;
|
||||||
message = expected;
|
|
||||||
expected = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
block();
|
block();
|
||||||
@ -384,14 +391,14 @@ proto.throws = function(block, expected, message) {
|
|||||||
actual = e;
|
actual = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
message = (expected && expected.name ? " (" + expected.name + ")." : ".") +
|
message = (expected.name ? " (" + expected.name + ")." : ".") +
|
||||||
(message ? " " + message : ".");
|
(message ? " " + message : ".");
|
||||||
|
|
||||||
if (!actual) {
|
if (!actual) {
|
||||||
this.report(true, actual, expected, "Missing expected exception" + message);
|
this.report(true, actual, expected, "Missing expected exception" + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((actual && expected && !expectedException(actual, expected))) {
|
if ((actual && !expectedException(actual, expected))) {
|
||||||
throw actual;
|
throw actual;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -410,15 +417,12 @@ proto.throws = function(block, expected, message) {
|
|||||||
* (string) Short explanation of the expected result
|
* (string) Short explanation of the expected result
|
||||||
*/
|
*/
|
||||||
proto.rejects = function(promise, expected, message) {
|
proto.rejects = function(promise, expected, message) {
|
||||||
|
checkExpectedArgument(this, "rejects", expected);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (typeof expected === "string") {
|
|
||||||
message = expected;
|
|
||||||
expected = null;
|
|
||||||
}
|
|
||||||
return promise.then(
|
return promise.then(
|
||||||
() => this.report(true, null, expected, "Missing expected exception " + message),
|
() => this.report(true, null, expected, "Missing expected exception " + message),
|
||||||
err => {
|
err => {
|
||||||
if (expected && !expectedException(err, expected)) {
|
if (!expectedException(err, expected)) {
|
||||||
reject(err);
|
reject(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -81,15 +81,15 @@ function run_test() {
|
|||||||
assert.deepEqual(/a/i, /a/i);
|
assert.deepEqual(/a/i, /a/i);
|
||||||
assert.deepEqual(/a/m, /a/m);
|
assert.deepEqual(/a/m, /a/m);
|
||||||
assert.deepEqual(/a/igm, /a/igm);
|
assert.deepEqual(/a/igm, /a/igm);
|
||||||
assert.throws(makeBlock(assert.deepEqual, /ab/, /a/));
|
assert.throws(makeBlock(assert.deepEqual, /ab/, /a/), ns.Assert.AssertionError);
|
||||||
assert.throws(makeBlock(assert.deepEqual, /a/g, /a/));
|
assert.throws(makeBlock(assert.deepEqual, /a/g, /a/), ns.Assert.AssertionError);
|
||||||
assert.throws(makeBlock(assert.deepEqual, /a/i, /a/));
|
assert.throws(makeBlock(assert.deepEqual, /a/i, /a/), ns.Assert.AssertionError);
|
||||||
assert.throws(makeBlock(assert.deepEqual, /a/m, /a/));
|
assert.throws(makeBlock(assert.deepEqual, /a/m, /a/), ns.Assert.AssertionError);
|
||||||
assert.throws(makeBlock(assert.deepEqual, /a/igm, /a/im));
|
assert.throws(makeBlock(assert.deepEqual, /a/igm, /a/im), ns.Assert.AssertionError);
|
||||||
|
|
||||||
let re1 = /a/;
|
let re1 = /a/;
|
||||||
re1.lastIndex = 3;
|
re1.lastIndex = 3;
|
||||||
assert.throws(makeBlock(assert.deepEqual, re1, /a/));
|
assert.throws(makeBlock(assert.deepEqual, re1, /a/), ns.Assert.AssertionError);
|
||||||
|
|
||||||
// 7.4
|
// 7.4
|
||||||
assert.deepEqual(4, "4", "deepEqual == check");
|
assert.deepEqual(4, "4", "deepEqual == check");
|
||||||
@ -158,10 +158,10 @@ function run_test() {
|
|||||||
assert.throws(makeBlock(thrower, ns.Assert.AssertionError),
|
assert.throws(makeBlock(thrower, ns.Assert.AssertionError),
|
||||||
ns.Assert.AssertionError, "message");
|
ns.Assert.AssertionError, "message");
|
||||||
assert.throws(makeBlock(thrower, ns.Assert.AssertionError), ns.Assert.AssertionError);
|
assert.throws(makeBlock(thrower, ns.Assert.AssertionError), ns.Assert.AssertionError);
|
||||||
assert.throws(makeBlock(thrower, ns.Assert.AssertionError));
|
assert.throws(makeBlock(thrower, ns.Assert.AssertionError), ns.Assert.AssertionError);
|
||||||
|
|
||||||
// if not passing an error, catch all.
|
// if not passing an error, catch all.
|
||||||
assert.throws(makeBlock(thrower, TypeError));
|
assert.throws(makeBlock(thrower, TypeError), TypeError);
|
||||||
|
|
||||||
// when passing a type, only catch errors of the appropriate type
|
// when passing a type, only catch errors of the appropriate type
|
||||||
let threw = false;
|
let threw = false;
|
||||||
@ -183,7 +183,7 @@ function run_test() {
|
|||||||
}
|
}
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
ifError(new Error("test error"));
|
ifError(new Error("test error"));
|
||||||
});
|
}, /test error/);
|
||||||
|
|
||||||
// make sure that validating using constructor really works
|
// make sure that validating using constructor really works
|
||||||
threw = false;
|
threw = false;
|
||||||
@ -253,7 +253,7 @@ function run_test() {
|
|||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
threw = true;
|
threw = true;
|
||||||
assert.equal(e.message, "Missing expected exception..");
|
assert.equal(e.message, "Error: The 'expected' argument was not supplied to Assert.throws() - false == true");
|
||||||
}
|
}
|
||||||
assert.ok(threw);
|
assert.ok(threw);
|
||||||
|
|
||||||
@ -355,9 +355,6 @@ add_task(async function test_rejects() {
|
|||||||
let SomeErrorLikeThing = function() {};
|
let SomeErrorLikeThing = function() {};
|
||||||
|
|
||||||
// The actual tests...
|
// The actual tests...
|
||||||
// No "expected" or "message" values supplied.
|
|
||||||
await assert.rejects(Promise.reject(new Error("oh no")));
|
|
||||||
await assert.rejects(Promise.reject("oh no"));
|
|
||||||
|
|
||||||
// An explicit error object:
|
// An explicit error object:
|
||||||
// An instance to check against.
|
// An instance to check against.
|
||||||
|
Loading…
Reference in New Issue
Block a user