Tag Archives: tdd

Unit Testing in PowerShell with PSUnit

I did a bit of playing around with wcf, PowerShell and PSUnit the other day.

PSUnit is a unit-testing framework based on NUnit (or should I say JUnit). It was a bit of work to set up but I was really impressed by the speed, robustness, and ISE integration.

It doesn’t make much sense to test application logic through a web service, or even with PowerShell at all, but it’s nice to know there’s such a great tool available. I’ve been having some issues with MSTest and 64 bit assemblies and although I’ll probably end up going with NUnit, it was a fun and educational journey!

Thanks PSUnit Guys!

Fetching Local Tweets in Ruby

Tests take forever because of all the HTTPRequests

Tests take forever because of all the HTTPRequests

As planned, I wrote a little class incorporating Geocoder and twitter_search so you can search for tweets within x miles (or kilometers) of an address.Geocoder does an excellent job of parsing addresses and twitter_search is simple and efficient.

Four thumbs up

Next phase I’d like to set up a little web interface showing tweets and tweeters. There’s something funny about hitting the internet to find out what’s going on around you.

Here’s the class, as you can see there are a few overridable defaults

require 'rubygems'
require 'geocoder'
require 'twitter_search'

class Twitter_Interface

  attr_accessor :tpp, :distance
  attr_reader   :address, :geocode
  attr          :geocoder, :client 

  def initialize addr = "", dist = "2mi", pp = 15
    @client   = TwitterSearch::Client.new
    @tpp      = pp
    @distance = dist
    @geocoder = initialize_geocoder
    set_location addr
  end

  # Could also use Yahoo API, but it requires API key.
  def initialize_geocoder geocoder = Geocoder::GeoCoderUs.new
    geocoder
  end

  def format_geocode geocode = @geocode
    if is_geocode? geocode
      return "#{geocode.latitude},#{geocode.longitude},#{@distance}"
    end
    ""
  end

  def tweets
    @client.query :geocode => format_geocode, :tpp => @tpp
  end

  def address_to_geocode addr = @address
    if addr == ""
      return ""
    end
    @geocoder.geocode addr
  end

  def is_geocode? geocode
    geocode.respond_to? "success?" and geocode.success?
  end

  def set_location addr
    @address = addr
    @geocode = address_to_geocode @address
  end

end

And here’s how you use it

t = Twitter_Interface.new "1600 pennsylvania ave nw washington dc"

#print the twitter query formatted geocode, default distance is 2 miles
puts t.format_geocode

#=>"38.898748,-77.037684,2mi"

#fetch the Twitter_Search::Tweets within 2 miles of the white house!
tweets = t.tweets

Download a .zip of the code / tests

Implicit Getters and Setters in ColdFusion

Thanks to CF8’s new onMissingMethod method, it’s trivial to implement implicit getters and setters. As easy as it is, I couldn’t google up any code. Since it’s not the kind of thing I’d rather search for than write myself, I thought I’d go ahead and do you the favor and post it here.

I realize there’s a nasty stigma attached and I don’t disagree that it’s bad practice, but it does come in handy when building a proof of concepts or programming exploratoraly.

So here you go, irregardless of whether or not it’s bad practice:

<cfcomponent name="BaseObject">

	<cffunction name="Init" output="no">
		<cfargument name="instance" default="#StructNew()#"/>

		<cfset SetInstance( arguments.instance )/>

		<cfreturn this/>
	</cffunction>

	<cffunction name="OnMissingMethod" output="no">
		<cfargument name="missingmethodname"      required="yes" />
		<cfargument name="missingmethodarguments" />

		<cfset var prefix = Left( arguments.missingmethodname, 3 ) />
		<cfset var suffix = Mid( arguments.missingmethodname,
			4,
			Len( arguments.missingmethodname )
		) />

		<cfif ( CompareNoCase( prefix, "get" ) eq 0 ) and Has( suffix ) >
			<cfreturn Get( suffix )/>
		</cfif>
		<cfif CompareNoCase( prefix, "set" ) eq 0>
			<cfreturn Set( suffix, arguments.missingmethodarguments.1 )/>
		</cfif>

		<cfthrow message="Method #arguments.missingmethodname# not found" />
	</cffunction>

	<!--- private on down --->

	<cffunction name="GetInstance" output="no" access="private">
		<cfreturn variables.instance/>
	</cffunction>

	<cffunction name="Get" output="no" access="private">
		<cfargument name="field" required="yes"/>

		<cfset var instance = GetInstance()/>

		<cfreturn instance[ arguments.field ]/>
	</cffunction>

	<cffunction name="Has" output="no" access="private">
		<cfargument name="field" required="yes"/>

		<cfreturn StructKeyExists( GetInstance(), arguments.field )/>
	</cffunction>

	<cffunction name="Set" output="no" access="private">
		<cfargument name="field" required="yes"/>
		<cfargument name="value" required="yes"/>

		<cfset var instance = GetInstance()/>
		<cfset instance[ arguments.field ] = arguments.value/>

		<cfreturn Get( arguments.field )/>
	</cffunction>

	<cffunction name="SetInstance" output="no" access="private">
		<cfargument name="instance" required="yes">

		<cfset variables.instance = arguments.instance/>

		<cfreturn GetInstance()/>
	</cffunction>

</cfcomponent>

Download it and the MxUnit Tests!

MXUnit and Me

mxunit1If you’ve been keeping up with my blog over the last couple months, then you already know that I’ve been experimenting with test driven development. I love the work flow, the way it makes me view my code, and the peace of mind…I just don’t know that it will pay off in my work environment. Hence the experimentation.

Most of my TDD dabbling up to this point has been done in Ruby. I’ve finally gotten around to messing around with some of the major ColdFusion testing frameworks: cfunit, cfcUnit and finally MXUnit.

After doing small projects with each of them, I’ve finally decided to settle down with MXUnit. It’s got decent docs, a nice work around for private methods, I dig the web interface, the eclipse plug-in is great and creating my own stubs for the generator was a snap!

In short: Two thumbs up!

Here’s my test. I’m still really new to this sort of thing, so I’d love feedback should you feel so inclined:


	

		function Setup() {
			this.cfc = CreateObject( "component" , "BaseObject" );
			this.cfc = this.cfc.init();
	
			makePublic( this.cfc, "SetInstance" );
			makePublic( this.cfc, "Set" );
			makePublic( this.cfc, "Has" );
			makePublic( this.cfc, "Get" );
			makePublic( this.cfc, "OnMissingMethod" );
		}
	
		function TestSetInstance() {
			var value = "a";
			AssertEquals( value, this.cfc.SetInstance( value ) );
	
			value = StructNew();
			AssertEquals( value, this.cfc.SetInstance( value ) );
		}
	
		function TestGetInstance() {
			var value = "a";
	
			this.cfc.SetInstance( value );
			AssertEquals( value, this.cfc.GetInstance() );
	
			value = "b";
			this.cfc.SetInstance( value );
			AssertEquals( value, this.cfc.GetInstance() );
		}
	
		function Testinit() {
			var value = "a";
	
			this.cfc = this.cfc.init( value );
			AssertEquals( value, this.cfc.GetInstance() );
	
			this.cfc = this.cfc.init();
			AssertEquals( StructNew(), this.cfc.GetInstance() );
		}
	
		function TestSet() {
			var field = "a";
			var value = "1";
	
			AssertEquals( value, this.cfc.Set( field, value ) );
	
			field = "w";
			value = "2";
	
			AssertEquals( value, this.cfc.Set( field, value ) );
		}
	
		function TestHas() {
			var field = "a";
			var value = "1";
	
			AssertEquals( false, this.cfc.Has( field ) );
	
			this.cfc.Set( field, value );
			AssertEquals( true,  this.cfc.Has( field ) );
	
			field = "w";
			value = "2";
	
			AssertEquals( false, this.cfc.Has( field ) );
		}
	
		function TestGet() {
			var field = "a";
			var value = "1";
	
			this.cfc.Set( field, value );
			AssertEquals( value, this.cfc.Get( field ) );
	
			field = "w";
			value = "2";
	
			this.cfc.Set( field, value );
			AssertEquals( value, this.cfc.Get( field ) );
		}
	
		function OnMissingMethod() {
			var value = "a";
	
			AssertEquals( value, this.cfc.SetSomething( value ) );
			AssertEquals( value, this.cfc.GetSomething() );
		}

	

Click to download the component I’m testing and the file above. Now.

Gosu Extensions Update

tests_complete

I’ve finally finished updating my Gosu Extensions. It’s far from perfect, but it’s in a good spot and I’m relatively happy with it. It feels good to get something “done”.

Although my coding process hasn’t been 100% TDD, I would say at least it’s been test centric and irregardless of whether I actually do anything with this, I think it’s been a good experience.

I plan on updating some of my “games” to use this new version, so I can see if these extensions actually save me any work.

Here are a few of the highlights

  • JavaScript like elements and events ( on_click, on_focus etc )
  • Wrappers for Gosu Images, Samples, Fonts and Text
  • Bounding Boxes for basic collision detection
  • Easy cursor support
  • Basic grid / matrix support
  • Basic scheduling system

Enough chit chat, Download the code!