explorers' club

explorations in dev, science, sci-fi, games, and other fun stuff!

FlexUnit + Hamcrest: Tip (sorta…. I’m still figuring this out)

Leave a comment

This is more like a note to self but I thought I would share in case someone has a better way of tackling this.


Right now I am working on as3isolib.v2.r05 (yes that’s right revision number 5).  One thing I wanted to do in this revision was to totally strip the rendering logic of any unnecessary calculations.  r04 had something on the order of 600+ lines of code just to parse a single object.  While it was super fast, it still suffered from redundant calculations.  After much mulling around on the mathematics and formulas, I decide that that particular logic could be streamlined to about 100-150 lines of code.  But I wanted to do make sure I tested each formula so that I didn’t have to guess when a problem might popup.

in depth

So unit testing really doesn’t deal with visual testing so much as it deals with testing of data and logic.  For instance, does method foo return an expected response based on a certain input.  Well that is a rather simplistic example but you get the idea.  One thing I was really psyched to get into were the declarative APIs of the Hamcrest libs in FlexUnit.   The API is pretty darn simple:

var expected:Array = [ 0, 1, 2, 3 ];
var result:Array = [ 0, 1, 2, 3 ];
assertThat( result, equalTo( expected )); //pass :)


the problem

Hamcrest provides plenty of APIs for testing Arrays of simple objects to other Arrays of simple objects.  It also provides APIs for object comparison in some limited fashions.  What I didn’t see in the docs or examples was a way to compare a collection value objects against another collection of value objects:

//assume the following are a custom value object of type FooVO
var expected:Array = [ objA0, objB0, objC0 ];
//assume the following have the same values as the above (clones essentially)
var  results:Array = [ objA1, objB1, objC1 ];
assertThat( results, equalTo( expected ));  //didn't seem to work :(

the pro tempore solution

So the work around I did was to implement a toString() method on the value objects:

public function toString():String
    var a:Array = [];
    a.push( "propA:" + propA );
    a.push( "propB:" + propB );
    a.push( ....... );

    return a.toString();

And then we need to get our collection of objects to spit out a long string to compare:

var data:Array = [ 0, 1, 2, 3 ];
var expected:String = "";
for each (var o:Object in data )
    expected += o.toString() + "\n";

var input:Array = [ 0, 1, 2, 3 ];
var result:String = "";
for each (o in input)
    result += o.toString() + "\n";

assertThat( result, equalTo( expected )); //pass :)

So it works for me.  But if you have any suggestions or want to point out a hidden API in Hamcrest or FlexUnit, please let me know.  In the meantime I think this works in most cases where you have simple data objects to test against.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s