XpUniverseOpenSpace.
RobBlake
I use CppUnit?, and I hate the having to add new test functions to the test fixture.

I use a neat trick to add this code automatically. If you're interested, read on!




When you define a test fixture in CppUnit?, you need code like this in your header file:
CPPUNIT_REGISTER_BEGIN();
CPPUNIT_FUNCTION( testFoo );
...
CPPUNIT_FUNCTION( testBar );
CPPUNIT_REGISTER_END();

(I know the above isnt exactly correct, I have no reference material here.)

The above situation sucks. You have to remember to add every test funciton to this registration block. I often forgot to do this, and just assumed that my tests were passing. By forgetting to register the functions, I was missing out on the immediate feedback that the Unit suites provide.




I use embeded perl as a work around. I am doing this from memory, so there may be some minor errors. This should get you in the right direction though.

First, there is a perl script that does the translation from embeded perl to C++.

File execute_epl_file.pl:
#!/usr/bin/perl -w
use Embperl;

use strict;

# use as so: execute_epl_file.pl <filename>

die "Need to specify a file as an argument!\n" if not @ARGV;
my $file = shift @ARGV;
print Embperl($file, ($file)) . "\n"; #send the name of the file as the first argument


Next, there is some boilerplate at the beginning of your test fixture headers.

boilerplate:

... #includes ...

[- 
  die "Need to specify the name of this file as the first argument!\n" if not @ARGV;
  open(THIS, shift @ARGV);
  @functions = ();
  #search every line in this document for test<something> folloed by a (
  #place the function name we found in $1
  foreach $line (<THIS>) {
    if ($line ~= m,(test\w+)
                   \(
                  ,x) {
      push @functions, $1;
    }
-]

class TestFoo : public TestFixture {
  CPPUNIT_REGISTRATION_BEGIN();
  [$ foreach $func (@functions) $]
  CPPUNIT_FUNCTION( [+ $func +] );
  [$ endforeach $]
  CPPUNIT_REGISTER_END();

... rest of file ... 


Then, add this code to your makefile (assuming GNU make):
%: %.epl
    rm -f [target, i think the special var is $@ ] 
    execute_epl_file.epl [dependeincies] > [target]


Viola! The epl script will run and write you a source file! Never forget to add test functions again!




dinner time is nigh, I will finish this later.

FrontPage
RecentChanges