Bug 1596918: Part 1a - Add support for more assertion/info methods to SpecialPowers.spawn. r=mccr8

ContentTask.spawn supports some common global mochitest assertion methods as
aliases for corresponding Assert methods, along with espected-fail `todo`
variants, and the `info` method for logging messages without triggering
assertions. The easiest way to mass-convert existing callers is to just add
support for these to SpecialPowers.spawn, which this patch does.

Differential Revision: https://phabricator.services.mozilla.com/D53734

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Kris Maglione 2019-12-07 18:44:32 +00:00
Родитель 1f5edf0f70
Коммит e95b795e86
4 изменённых файлов: 67 добавлений и 13 удалений

Просмотреть файл

@ -1299,16 +1299,29 @@ function testScope(aTester, aTest, expected) {
self.record(condition, name);
}
};
this.record = function test_record(condition, name, ex, stack) {
aTest.addResult(
new testResult({
name,
pass: condition,
ex,
stack: stack || Components.stack.caller,
allowFailure: aTest.allowFailure,
})
);
this.record = function test_record(condition, name, ex, stack, expected) {
if (expected == "fail") {
aTest.addResult(
new testResult({
name,
pass: !condition,
todo: true,
ex,
stack: stack || Components.stack.caller,
allowFailure: aTest.allowFailure,
})
);
} else {
aTest.addResult(
new testResult({
name,
pass: condition,
ex,
stack: stack || Components.stack.caller,
allowFailure: aTest.allowFailure,
})
);
}
};
this.is = function test_is(a, b, name) {
self.record(

Просмотреть файл

@ -278,7 +278,7 @@ SimpleTest.ok = function (condition, name) {
}
};
SimpleTest.record = function (condition, name, diag, stack) {
SimpleTest.record = function (condition, name, diag, stack, expected) {
var test = {'result': !!condition, 'name': name, 'diag': diag};
if (SimpleTest.expected == 'fail') {
if (!test.result) {
@ -295,6 +295,9 @@ SimpleTest.record = function (condition, name, diag, stack) {
}
var successInfo = {status:"FAIL", expected:"FAIL", message:"TEST-KNOWN-FAIL"};
var failureInfo = {status:"FAIL", expected:"PASS", message:"TEST-UNEXPECTED-FAIL"};
} else if (expected == "fail") {
var successInfo = {status:"PASS", expected:"FAIL", message:"TEST-UNEXPECTED-PASS"};
var failureInfo = {status:"FAIL", expected:"FAIL", message:"TEST-KNOWN-FAIL"};
} else {
var successInfo = {status:"PASS", expected:"PASS", message:"TEST-PASS"};
var failureInfo = {status:"FAIL", expected:"PASS", message:"TEST-UNEXPECTED-FAIL"};

Просмотреть файл

@ -286,12 +286,18 @@ class SpecialPowersChild extends JSWindowActorChild {
case "Assert":
{
if ("info" in message.data) {
this.SimpleTest.info(message.data.info);
break;
}
// An assertion has been done in a mochitest chrome script
let { name, passed, stack, diag } = message.data;
let { name, passed, stack, diag, expectFail } = message.data;
let { SimpleTest } = this;
if (SimpleTest) {
SimpleTest.record(passed, name, diag, stack);
let expected = expectFail ? "fail" : "pass";
SimpleTest.record(passed, name, diag, stack, expected);
} else {
// Well, this is unexpected.
dump(name + "\n");

Просмотреть файл

@ -18,6 +18,16 @@ ChromeUtils.defineModuleGetter(
"resource://testing-common/Assert.jsm"
);
let expectFail = false;
function expectingFail(fn) {
try {
expectFail = true;
fn();
} finally {
expectFail = false;
}
}
class SpecialPowersSandbox {
constructor(name, reportCallback, opts = {}) {
this.name = name;
@ -42,6 +52,27 @@ class SpecialPowersSandbox {
configurable: true,
});
}
Object.assign(this.sandbox, {
ok: (...args) => {
this.Assert.ok(...args);
},
is: (...args) => {
this.Assert.equal(...args);
},
isnot: (...args) => {
this.Assert.notEqual(...args);
},
todo: (...args) => {
expectingFail(() => this.Assert.ok(...args));
},
todo_is: (...args) => {
expectingFail(() => this.Assert.equal(...args));
},
info: info => {
this.reportCallback({ info });
},
});
}
get Assert() {
@ -66,6 +97,7 @@ class SpecialPowersSandbox {
diag,
passed: !err,
stack: stack && stack.formattedStack,
expectFail,
});
}