View Full Version : JavaScript-based assert() function

Alex Vincent
11-08-2003, 06:03 AM
Not sure how useful this is, exactly. It requires a bit of explanation.

function assert(assertString, thisObj) {
var throwException = arguments.length > 2 ? arguments[2] : false;
var argsObj = arguments.length > 3 ? arguments[3] : {};
var argsString = "";
for (var property in argsObj) {
argsString += ("var " + property + " = " + argsObj[property].toSource() +";\n");
var func = new Function(argsString + "return (" + assertString + ");");
var mustBeTrue = false;
try {
mustBeTrue = func.apply(thisObj);
catch(e) {
// fall through. An exception will leave mustBeTrue as false, and the assertion still fails.

try {
if (!mustBeTrue) {
throw new Error("ECMAScript assertion failed: (" + assertString + ")");
catch(e) {
if (throwException) {
/* For Mozilla, use
throw new Error(e.message + " stack:\n" + e.stack);
throw e;
} else {
/* For Mozilla, use
dump ("Warning: " + e.message + " stack:\n" + e.stack);
for (property in argsObj) {
dump(property + " = " + argsObj[property] + "\n");
return mustBeTrue;

So what is an assert()? In C++, it's usually a macro the programmer uses to test a statement that must be true for the code to function properly. The statement, I believe, is called an assertion.

If the assertion evaluates to false, then there's a bug in the program. It's basically a way for the programmer to detect for bugs in the program that the program wouldn't be expected to know about.

This assert() function takes a string as its first argument, which must evaluate to true. If said string is true, everything's hunky-dory, and the function returns true. If said string is false, then the function notifies either the user or the application (depending on the third argument, which is optional), and returns false. If there is an exception generated while testing the assertion, the assert() function returns false.

The second argument is the this object of the code calling the assert(). So you could basically call the assert() like this:

assert("1 == 1", this);

The optional third argument, which defaults to false, determines if a JavaScript error should be thrown. A false value means notify the user, but don't stop execution (dump() is a Mozilla-specific function, used in debug builds). A true value means throw the exception.

The fourth argument should be an object literal for passing variables into the assertion for evaluation. For instance:

if (assert("x == 1", this, false, {
x: 1
} )) {
html_input.value = "x is indeed equal to one!";

All this works because the assert() function creates a local function object based on the information you feed it, and then executes that function.

This code is available under the Mozilla Public License (MPL1.1) tri-license scheme. Feedback?

(update: ok, liorean, that was a dumb thing for me to say :) )

11-08-2003, 06:09 AM
if..then? then isn't a JavaScript construct, unless I'm entirely mistaken. It's not needed since JavaScript uses the parens for function calling, which means the end paren will always separate the condition from the statement.

[:edit:]Not at all dumb, just not the right language...