Developing a Vectorworks 2011 Plug-in, TDD-style – Episode 2

In Episode 1 – How to start?, we’ve cleared the first hurdle – we actually wrote a passing test. It was rather trivial, but we ended up with a class representing a cabinet with the properties “Width” and “Height”. What’s next? How about spicing up the SimpleCabinet class? (At the current state, just adding some salt to the class will spice it up substantially). How about allowing us to calculate the position of the left & right side of the cabinet? The left side of the cabinet should be located at x = 0 (in the local coordinate space of the plug-in object):

TEST_N(SimpleCabinetGetLeftSideXLocation)
{
	SimpleCabinet simpleCabinet(600, 800);
	
	CHECK_EQUAL(0, simpleCabinet.GetLeftSideXLocation());
}

Passing this test is (again) rather trivial. We add the following message to the SimpleCabinet class:

	WorldCoord GetLeftSideXLocation() const {
		return 0;
	}

If the left side’s located at x = 0, then the right side’s x location should be the width of the cabinet minus the board thickness (assuming that the width property denotes the overall width of the cabinet).

TEST_N(SimpleCabinetGetRightSideXLocation)
{
	SimpleCabinet simpleCabinet(600, 800);
	
	CHECK_EQUAL(600 - SimpleCabinet::kBoardThickness, 
				simpleCabinet.GetRightSideXLocation());
}

Adding the following method to SimpleCabinet will make the test pass. We didn’t plan for this in episode 1, but we can re-use the fWidth instance variable for the purpose of deriving the right side’s x location.


	static const WorldCoord kBoardThickness;

	WorldCoord GetRightSideXLocation() const {
		return fWidth - kBoardThickness;
	}	

While we’re at it, we could also test the x location of the cabinet’s bottom, which should be at x = kBoardThickness (as the cabinet is supposed to have continuous sides):

TEST_N(SimpleCabinetGetBottomXLocation)
{
	SimpleCabinet simpleCabinet(600, 800);
	
	CHECK_EQUAL(SimpleCabinet::kBoardThickness, 
				simpleCabinet.GetBottomXLocation());
}

Making the test pass is simple:

	WorldCoord GetBottomXLocation() const {
		return kBoardThickness;
	}

Where are we at? At last, we’re starting to actually compute something. Our “model” class is able to deliver a cabinet’s overall width & height plus the x location of the cabinet’s left side, right side & bottom. We sticked to writing the tests first. Not an awful lot of sophistication here (yet), but progress.

Tune in next week when you hear Dr. Bob say: “I’m calling bullshit on this x coordinate testing…”.

Previous Episode | Next Episode