Bug 1215606 - Ensure that DevToolsUtils.assert is properly exported; r=jsantell

This commit is contained in:
Nick Fitzgerald 2015-10-20 10:56:03 +05:30
parent 84cfa9ca73
commit 83ab00ba37
4 changed files with 70 additions and 18 deletions

View File

@ -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)
*
@ -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
* fatal behavior, and is therefore much easier to accidentally ignore.
*/
exports.defineLazyGetter(exports, "assert", () => {
function noop(condition, msg) { }
function assert(condition, message) {
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;
});
Object.defineProperty(exports, "assert", {
get: () => (AppConstants.DEBUG || AppConstants.DEBUG_JS_MODULES || this.testing)
? reallyAssert
: exports.noop,
})
/**
* Defines a getter on a specified object for a module. The module will not

View File

@ -9,6 +9,11 @@ const DevToolsUtils = require("devtools/shared/DevToolsUtils");
// Register a console listener, so console messages don't just disappear
// 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 listener = {
observe: function (aMessage) {
@ -35,7 +40,10 @@ var listener = {
while (DebuggerServer.xpcInspector.eventLoopNestLevel > 0) {
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");
}
}
};

View File

@ -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.");
}

View File

@ -6,6 +6,7 @@ skip-if = toolkit == 'android' || toolkit == 'gonk'
support-files =
exposeLoader.js
[test_assert.js]
[test_fetch-chrome.js]
[test_fetch-file.js]
[test_fetch-http.js]