Stephen Cebula's Blog

Agility without Fragility

  Home  |   Contact  |   Syndication    |   Login
  6 Posts | 1 Stories | 5 Comments | 0 Trackbacks

News

Archives

Post Categories

Image Galleries

Articles

Currently Reading

Design

Favorite Books

Favorite Websites

Health

Keeping IT in Perspective

Learning from the Past

Mobile

Recently Read

XP

Fitnesse is a very useful tool for defining acceptance tests and verifying them against your application code.  Fitnesse is available for different languages, among them, Java, the .NET runtime and C++.

Unfortunately, the C++ version of Fitnesse has lagged a bit behind in features compared to its Java and .NET cousins.

One useful fixture missing from C++ is the TimedActionFixture.  A need presented itself at work one time, so I wrote a C++ version on my own time (since the effort wasn't covered by any of our stories in the current iteration).

The nice thing about this fixture is that it is a drop-in replacement for the standard ActionFixture.  If you have an existing test table using the ActionFixture, you merely rename it!

Here is the source code:

#ifndef TIMEDACTIONFIXTURE_H
#define TIMEDACTIONFIXTURE_H

#include "ActionFixture.h"

class TimedActionFixture : public ActionFixture
{
public:
    TimedActionFixture();

    ~TimedActionFixture();

   //Overridden
   void doTable(ParsePtr table);
   void doCells(ParsePtr cells);


private:
   ParsePtr td(std::string body);
};

#endif  //TIMEDACTIONFIXTURE_H



#include <time.h>
#include <assert.h>
#include "TimedActionFixture.h"


TimedActionFixture::TimedActionFixture() : ActionFixture()
{
}

TimedActionFixture::~TimedActionFixture()
{
}

void TimedActionFixture::doTable(ParsePtr table)
{
   ActionFixture::doTable(table);

   table->parts->parts->last()->more = td(std::string("startTime"));
   table->parts->parts->last()->more = td(std::string("seconds"));
}

void TimedActionFixture::doCells(ParsePtr cells)
{
   bool bClockError = false;

   time_t  start;
   ::time( &start );

   clock_t cStart = clock();
   if ( cStart == (clock_t)-1 )
      bClockError = true;

   ActionFixture::doCells(cells);

   time_t  now;
   ::time( &now );
  
   struct tm *tstart = localtime( &start );

   clock_t cNow = clock();
   if ( cNow == (clock_t)-1 )
      bClockError = true;

   char buf[100];

   double split = (double)(cNow - cStart) / CLOCKS_PER_SEC;

   if ( bClockError )
   {
      strcpy( buf, "Elapsed time unavailable" );
   }
   else
   {
      sprintf( buf, "%.3lf", split );
   }

   cells->last()->more = td( std::string(asctime( tstart )) );
   cells->last()->more = td( std::string(buf) );
}

ParsePtr TimedActionFixture::td(std::string body)
{
   Parse *p = new Parse("td", body, 0, 0);
   return( p );
}




































































The last piece needed is the "hook" to register the new fixture like a standard fixture.  This can be done with the following lines of code in the method DynamicMaker::makeStaticFixture() found in DynamicMaker.cpp

    if (name == "TimedActionFixture")
        return new TimedActionFixture;

posted on Saturday, March 29, 2008 11:29 AM

Feedback

# re: TimedActionFixture for C++ 9/15/2009 4:39 PM vjr
Only 18 months after the fact, but well done, haven't used it yet but any contributions to the C++ fitnesse effort is to be applauded. Still looking for more Fitnesse resources...

Post A Comment
Title:
Name:
Email:
Comment:
Verification: