Hok's Macromedia Flash Blog

macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript

Ads

Search

Test-Driven Development(TDD)

I have just started reading Kent Becks Test-Driven Development
and my first impression is a good one.
This technic leads to clearer and easier coding.



The TDD-Concept, i have just started reading
so don't take this for perfect, is good as simple.
It seperates the development in three steps.

The first step is to write the test for an unit
of your software.

The second step is to write the code. This step is
ready, if the test doesn't failes.

The third step is to refactore youre code, that means
that you eleminate redundant parts and etc. Martin
Fowler
has a written a really good book about this.

Kent Beck has given this three steps the name:
Red-Phase, Green-Phase and Refactoring.

Well let us have an example, we will build an priority
stack. First we are going to the Red-Phase(Test-Writing)
it's real cool because we have to define the interface
too, and have an example usage for our class that
documents our code.

// We give all test-functions the suffix: "_Test"
// so we could search our object-tree to run all
// tests with one call, oh yeah how easy...;-)
PriorityStack_Test = function () {
   var tmp;
   //
   // Create an instance
   ps = new PriorityStack();
   //
   // Is our class existing
   if (PriorityStack == undefined) trace("ERROR: The class: 'PriorityStack' doesn't exist");
   // Well, this is an mostly useless test because, it doesn't tests something
   // that could break or is hard to implement. But look at the point that we
   // does not have irretating output if our class exist.
   //
   // Let's go on
   ps.addItem("two", 2);
   ps.addItem("four", 4);
   ps.addItem("one", 1);
   ps.addItem("three", 3);
   ps.addItem("five", 5);
   //
   // Test the length-Property
   if (ps.length != 5) trace("ERROR: PriorityStack.length-Property doesn't return 5");
   //
   // Test the pop-method
   if ((tmp = ps.pop()) != "five") trace("ERROR: PriorityStack.pop-Method doesn't return 'five' it has returned: " + tmp);
   if ((tmp = ps.pop()) != "four") trace("ERROR: PriorityStack.pop-Method doesn't return 'four' it has returned: " + tmp);
   if ((tmp = ps.pop()) != "three") trace("ERROR: PriorityStack.pop-Method doesn't return 'three' it has returned: " + tmp);
   if ((tmp = ps.pop()) != "two") trace("ERROR: PriorityStack.pop-Method doesn't return 'two' it has returned: " + tmp);
   if ((tmp = ps.pop()) != "one") trace("ERROR: PriorityStack.pop-Method doesn't return 'one' it has returned: " + tmp);
   //
   // Test the length-Property again
   if (ps.length != 0) trace("ERROR: PriorityStack.length-Property doesn't return 0");
};

Well, now we have an nice result in the output window:
ERROR: The class: 'PriorityStack' doesn't exist
ERROR: PriorityStack.length-Property doesn't return 5
ERROR: PriorityStack.pop-Method doesn't return 'five'
ERROR: PriorityStack.pop-Method doesn't return 'four'
ERROR: PriorityStack.pop-Method doesn't return 'three'
ERROR: PriorityStack.pop-Method doesn't return 'two'
ERROR: PriorityStack.pop-Method doesn't return 'one'
ERROR: PriorityStack.length-Property doesn't return 0

Now we can start to write our class:
PriorityStack = function () {
   this.elements = new Array();
};
//
PriorityStack.prototype.addItem = function (data, priority) {
   var e = this.elements;
   var l = e.length;
   //
   while (l--) {
      if (e[l].p <= priority) break;
   }
   //
   e.splice(++l, 0, {d:data, p:priority});
};
//
PriorityStack.prototype.pop = function () {
   return this.elements.pop().d;
};
//
PriorityStack.prototype.addProperty("length", function () { return this.elements.length }, null);
//
// Test
PriorityStack_Test();

Cool, we have no output, thats fine, now we could optimise
or/and refactor the code.
An interesting thing is that you can also write performance
tests, so that we could easier find faster algorithms.

Posted by hOk at August 14, 2003 05:07 PM

Comments (3)

I'm translating Test Driven Development by Example to Korean now. And I also interested in Flash MX.

There are some unit testing framework for Flash Action Script. Check

http://c2.com/cgi/wiki?AsUnit

and one that I made(I think this one is simpler to use):

http://jania.codns.com/jwiki/moin.cgi/AsUnit

Posted by jania at September 9, 2003 12:22 AM

Thanks, for that tip,
best regards, Holger

Posted by hOk at September 9, 2003 06:53 PM

Wow, this makes it so much easier to test. I have to agree with hOk, this is indeed one the best tips here.

Posted by Online Coupons at November 1, 2003 10:21 PM


Name:


Email Address:


URL:


Comments:



You can use <code>code that should be highlightned</code> to highlight code!
Optionally you can use the attribut language="php|perl|java" within the code tag, otherwise
actionscript-highlightning will be used.

New Place