зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1215606 - Ensure that DevToolsUtils.assert is properly exported; r=jsantell
This commit is contained in:
Родитель
9793e9dfd8
Коммит
6a89e87496
|
@ -458,6 +458,25 @@ exports.dbg_assert = function dbg_assert(cond, e) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.defineLazyGetter(this, "AppConstants", () => {
|
||||||
|
const scope = {};
|
||||||
|
Cu.import("resource://gre/modules/AppConstants.jsm", scope);
|
||||||
|
return scope.AppConstants;
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* No operation. The empty function.
|
||||||
|
*/
|
||||||
|
exports.noop = function () { };
|
||||||
|
|
||||||
|
function reallyAssert(condition, message) {
|
||||||
|
if (!condition) {
|
||||||
|
const err = new Error("Assertion failure: " + message);
|
||||||
|
exports.reportException("DevToolsUtils.assert", err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DevToolsUtils.assert(condition, message)
|
* DevToolsUtils.assert(condition, message)
|
||||||
*
|
*
|
||||||
|
@ -477,23 +496,11 @@ exports.dbg_assert = function dbg_assert(cond, e) {
|
||||||
* This is an improvement over `dbg_assert`, which doesn't actually cause any
|
* This is an improvement over `dbg_assert`, which doesn't actually cause any
|
||||||
* fatal behavior, and is therefore much easier to accidentally ignore.
|
* fatal behavior, and is therefore much easier to accidentally ignore.
|
||||||
*/
|
*/
|
||||||
exports.defineLazyGetter(exports, "assert", () => {
|
Object.defineProperty(exports, "assert", {
|
||||||
function noop(condition, msg) { }
|
get: () => (AppConstants.DEBUG || AppConstants.DEBUG_JS_MODULES || this.testing)
|
||||||
|
? reallyAssert
|
||||||
function assert(condition, message) {
|
: exports.noop,
|
||||||
if (!condition) {
|
})
|
||||||
const err = new Error("Assertion failure: " + message);
|
|
||||||
exports.reportException("DevToolsUtils.assert", err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const scope = {};
|
|
||||||
Cu.import("resource://gre/modules/AppConstants.jsm", scope);
|
|
||||||
const { DEBUG, DEBUG_JS_MODULES } = scope.AppConstants;
|
|
||||||
|
|
||||||
return (DEBUG || DEBUG_JS_MODULES || exports.testing) ? assert : noop;
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines a getter on a specified object for a module. The module will not
|
* Defines a getter on a specified object for a module. The module will not
|
||||||
|
|
|
@ -9,6 +9,11 @@ const DevToolsUtils = require("devtools/shared/DevToolsUtils");
|
||||||
|
|
||||||
// Register a console listener, so console messages don't just disappear
|
// Register a console listener, so console messages don't just disappear
|
||||||
// into the ether.
|
// into the ether.
|
||||||
|
|
||||||
|
// If for whatever reason the test needs to post console errors that aren't
|
||||||
|
// failures, set this to true.
|
||||||
|
var ALLOW_CONSOLE_ERRORS = false;
|
||||||
|
|
||||||
var errorCount = 0;
|
var errorCount = 0;
|
||||||
var listener = {
|
var listener = {
|
||||||
observe: function (aMessage) {
|
observe: function (aMessage) {
|
||||||
|
@ -35,7 +40,10 @@ var listener = {
|
||||||
while (DebuggerServer.xpcInspector.eventLoopNestLevel > 0) {
|
while (DebuggerServer.xpcInspector.eventLoopNestLevel > 0) {
|
||||||
DebuggerServer.xpcInspector.exitNestedEventLoop();
|
DebuggerServer.xpcInspector.exitNestedEventLoop();
|
||||||
}
|
}
|
||||||
do_throw("head_dbg.js got console message: " + string + "\n");
|
|
||||||
|
if (!ALLOW_CONSOLE_ERRORS) {
|
||||||
|
do_throw("head_devtools.js got console message: " + string + "\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
/* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
|
||||||
|
/* Any copyright is dedicated to the Public Domain.
|
||||||
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||||
|
|
||||||
|
// Test DevToolsUtils.assert
|
||||||
|
|
||||||
|
ALLOW_CONSOLE_ERRORS = true;
|
||||||
|
|
||||||
|
function run_test() {
|
||||||
|
// Enable assertions.
|
||||||
|
DevToolsUtils.testing = true;
|
||||||
|
|
||||||
|
const { assert } = DevToolsUtils;
|
||||||
|
equal(typeof assert, "function");
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert(true, "this assertion should not fail");
|
||||||
|
} catch (e) {
|
||||||
|
// If you catch assertion failures in practice, I will hunt you down. I get
|
||||||
|
// email notifications every time it happens.
|
||||||
|
ok(false, "Should not get an error for an assertion that should not fail. Got "
|
||||||
|
+ DevToolsUtils.safeErrorString(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
let assertionFailed = false;
|
||||||
|
try {
|
||||||
|
assert(false, "this assertion should fail");
|
||||||
|
} catch (e) {
|
||||||
|
ok(e.message.startsWith("Assertion failure:"),
|
||||||
|
"Should be an assertion failure error");
|
||||||
|
assertionFailed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ok(assertionFailed,
|
||||||
|
"The assertion should have failed, which should throw an error when assertions are enabled.");
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ skip-if = toolkit == 'android' || toolkit == 'gonk'
|
||||||
support-files =
|
support-files =
|
||||||
exposeLoader.js
|
exposeLoader.js
|
||||||
|
|
||||||
|
[test_assert.js]
|
||||||
[test_fetch-chrome.js]
|
[test_fetch-chrome.js]
|
||||||
[test_fetch-file.js]
|
[test_fetch-file.js]
|
||||||
[test_fetch-http.js]
|
[test_fetch-http.js]
|
||||||
|
|
Загрузка…
Ссылка в новой задаче