Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 3.75 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-08-2003, 05:03 AM   PM User | #1
Alex Vincent
Moderator


 
Join Date: May 2002
Location: Hayward, CA
Posts: 1,427
Thanks: 1
Thanked 19 Times in 17 Posts
Alex Vincent is on a distinguished road
JavaScript-based assert() function

Not sure how useful this is, exactly. It requires a bit of explanation.

Code:
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");
      }
      dump("\n");
*/
      alert(e);
    }
  }
  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:

Code:
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:

Code:
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 )
__________________
"The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
June 30, 2001
author, Verbosio prototype XML Editor
author, JavaScript Developer's Dictionary
https://alexvincent.us/blog

Last edited by Alex Vincent; 11-08-2003 at 05:17 AM..
Alex Vincent is offline   Reply With Quote
Old 11-08-2003, 05:09 AM   PM User | #2
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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...
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 11-12-2003 at 08:40 AM..
liorean is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:33 AM.


Advertisement
Log in to turn off these ads.