Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts

    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 )
    Last edited by Alex Vincent; 11-08-2003 at 06:17 AM.
    "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

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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...
    Last edited by liorean; 11-12-2003 at 09:40 AM.
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •