I discover the Scriptaculous unit test code

In my last post I whinged about not liking jsUnit. I hoped I would find something better by looking at how the Prototype people test their code. Well, I was close: I remembered seeing some unit and functional testing in one of the JS packages I use, and it turned out to be Scriptaculous I was thinking about.

Here is jsUnit’s assert function:

function assert() {
    _validateArguments(1, arguments);
    var booleanValue = nonCommentArg(1, 1, arguments);

    if (typeof(booleanValue) != 'boolean')
        error('Bad argument to assert(boolean)');

    _assert(commentArg(1, arguments), booleanValue === true, 'Call to assert(boolean) with false');
}

And here is Scriptaculous’s, from unittest.js:

assert: function(expression) {
    var message = arguments[1] || 'assert: got "' + Test.Unit.inspect(expression) + '"';
    try { expression ? this.pass() :
      this.fail(message); }
    catch(e) { this.error(e); }
  },

Ah, now that’s better. No oddly named functions, no vague multi-parameter calls, and no triple equal sign operators. Now, I don’t understand the unittest.js code yet, since I just saw it, but I have a feeling I can figure out what pass, fail, and error do.

And how swell is using || in an assignment expression?

One thought on “I discover the Scriptaculous unit test code

Comments are closed.