Bug 1412282 - Allow calling Assert.jsm methods directly from other test-only modules. r=mikedeboer

This adds a global instance that can be used by invoking assertion methods directly on the imported Assert object. The test suites set the global reporter function to the one for the currently running test.

MozReview-Commit-ID: 8dksVc9o7r

--HG--
extra : rebase_source : 3e382c6d24c6019d29963811c37469cfc23b928f
This commit is contained in:
Paolo Amadini 2017-10-27 14:38:30 +01:00
parent 321bfe31ef
commit dac52b86d6
8 changed files with 18 additions and 39 deletions

View File

@ -730,9 +730,8 @@ Tester.prototype = {
stack,
allowFailure: currentTest.allowFailure,
}));
});
}, true);
this.PromiseTestUtils.Assert = this.currentTest.scope.Assert;
this.ContentTask.setTestScope(currentScope);
// Allow Assert.jsm methods to be tacked to the current scope.

View File

@ -25,15 +25,25 @@ XPCOMUtils.defineLazyModuleGetter(this, "Promise",
* 1. The assert module provides functions that throw AssertionError's when
* particular conditions are not met.
*
* To use the module you'll need to instantiate it first, which allows consumers
* To use the module you may instantiate it first, which allows consumers
* to override certain behavior on the newly obtained instance. For examples,
* see the javadoc comments for the `report` member function.
*
* The isDefault argument is used by test suites to set reporterFunc as the
* default used by the global instance, which is called for example by other
* test-only modules. This is false when the reporter is set by content scripts,
* because they may still run in the parent process.
*/
var Assert = this.Assert = function(reporterFunc) {
var Assert = this.Assert = function(reporterFunc, isDefault) {
if (reporterFunc)
this.setReporter(reporterFunc);
if (isDefault)
Assert.setReporter(reporterFunc);
};
// This allows using the Assert object as an additional global instance.
Object.setPrototypeOf(Assert, Assert.prototype);
function instanceOf(object, type) {
return Object.prototype.toString.call(object) == "[object " + type + "]";
}

View File

@ -51,7 +51,7 @@ var Assert = new AssertCls(function(err, message, stack) {
} else {
do_report_result(true, message, stack);
}
});
}, true);
var _add_params = function(params) {
if (typeof _XPCSHELL_PROCESS != "undefined") {
@ -510,7 +510,6 @@ function _execute_test() {
_fakeIdleService.activate();
_PromiseTestUtils.init();
_PromiseTestUtils.Assert = Assert;
let coverageCollector = null;
if (typeof _JSCOV_DIR === "string") {

View File

@ -16,20 +16,14 @@ const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://testing-common/Assert.jsm");
Cu.import("resource://testing-common/TestUtils.jsm");
const LoginInfo =
Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
"nsILoginInfo", "init");
// For now, we need consumers to provide a reference to Assert.jsm.
var Assert = null;
this.LoginTestUtils = {
set Assert(assert) {
Assert = assert; // eslint-disable-line no-native-reassign
},
/**
* Forces the storage module to save all data, and the Login Manager service
* to replace the storage module with a newly initialized instance.

View File

@ -12,6 +12,7 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/LoginRecipes.jsm");
Cu.import("resource://gre/modules/LoginHelper.jsm");
Cu.import("resource://testing-common/LoginTestUtils.jsm");
Cu.import("resource://testing-common/MockDocument.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DownloadPaths",
@ -25,10 +26,6 @@ const LoginInfo =
Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
"nsILoginInfo", "init");
// Import LoginTestUtils.jsm as LoginTestUtils.
XPCOMUtils.defineLazyModuleGetter(this, "LoginTestUtils",
"resource://testing-common/LoginTestUtils.jsm");
LoginTestUtils.Assert = Assert;
const TestData = LoginTestUtils.testData;
const newPropertyBag = LoginHelper.newPropertyBag;

View File

@ -6,9 +6,6 @@
* will fail tests in this case, unless the test whitelists itself.
*/
// For this test we allow the global Assert to be modified.
/* global Assert:true */
"use strict";
this.EXPORTED_SYMBOLS = [
@ -18,13 +15,11 @@ this.EXPORTED_SYMBOLS = [
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/Services.jsm", this);
Cu.import("resource://testing-common/Assert.jsm", this);
// Keep "JSMPromise" separate so "Promise" still refers to DOM Promises.
let JSMPromise = Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
// For now, we need test harnesses to provide a reference to Assert.jsm.
let Assert = null;
this.PromiseTestUtils = {
/**
* Array of objects containing the details of the Promise rejections that are
@ -136,13 +131,6 @@ this.PromiseTestUtils = {
this.uninit();
},
/**
* Sets or updates the Assert object instance to be used for error reporting.
*/
set Assert(assert) {
Assert = assert;
},
// UncaughtRejectionObserver
onLeftUncaught(promise) {
let message = "(Unable to convert rejection reason to string.)";

View File

@ -16,6 +16,7 @@ const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/AppConstants.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://testing-common/Assert.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "gExternalProtocolService",
"@mozilla.org/uriloader/external-protocol-service;1",
@ -24,14 +25,7 @@ XPCOMUtils.defineLazyServiceGetter(this, "gMIMEService",
"@mozilla.org/mime;1",
"nsIMIMEService");
// For now, we need consumers to provide a reference to Assert.jsm.
var Assert = null;
this.HandlerServiceTestUtils = {
set Assert(assert) {
Assert = assert; // eslint-disable-line no-native-reassign
},
/**
* This has to be initialized to the nsIHandlerService instance under testing.
*

View File

@ -27,8 +27,6 @@ XPCOMUtils.defineLazyServiceGetter(this, "gHandlerServiceRDF",
"@mozilla.org/uriloader/handler-service-rdf;1",
"nsIHandlerService");
HandlerServiceTestUtils.Assert = Assert;
do_get_profile();
let jsonPath = OS.Path.join(OS.Constants.Path.profileDir, "handlers.json");