Prototype Patch Re-submitted

Well I finally got off my high horse and made some time to build out a collection of unit tests for my inheritance library. I took the latest code along will all the newly written unit tests and bundled them into a nice patch for prototype. I re-attached them to my original patch so hopefully it will be included in a future version.

Now that I have your attention, I thought I'd spend a bit of time to discuss some code I discovered while building out this latest set of unit tests. While looking through the scriptaculous unit testing suite I found a neat little entry at the bottom of unittest.js. I'm not sure when it was added, so if this is really old news I apologize for being behind the times.

At the bottom of unittest.js there is an experimental BDD implementation in the works. This is neat, I've seen BDD (Behavior Driven Development) frameworks in Java as well as Ruby and now there's an implementation for JavaScript.

So what does all this mean? Well, it means you can write your unit tests like a product specification. Using an example from RSpec, we can write the following test suite that reads like a specification:

var stack;

Test.context("An empty stack", {

  setup: function() {
    stack = new Stack();
  },

  "should be empty": function() {
    assertReturnsTrue('empty', stack)
  },

  "should no longer be empty after 'push'": function(){
    stack.push("anything");
    assertReturnsFalse('empty', stack)
  },

  "should complain when sent 'peek'": function(){
    assertRaise('StackUnderflowError',
      function(){stack.peek()});
  },

  "should complain when sent 'pop'": function(){
    assertRaise('StackUnderflowError',
      function(){stack.pop()});
  }

});

Now, in true BDD fashion, we should actually write our Stack implementation. Our test suite not only acts like the specification but allows us to programatically ensure that the Stack implementation we do end up writing meets that specification as well.