Rob - 2003-06-12

Hi, I've been using CppUnit for a while now.  I find that I always forget to add new "test..." functions to the CPPUINT_TEST_SUITE BEGIN/END blocks.

In JUnit, any function in a test suite class that begins with "test" is automatically added to the test suite.  Wouldn't that be nice?

I just discovered a trick that can emulate this behavior using perl.  To use it, you will need a recent version of perl and a module called Embperl availiable from CPAN.

here is the perl script:
<pre>----------------------------------------(snip)
#!/usr/bin/perl -w
# This script is used to process epl files

use Embperl;

die "Need to specify at least one argument!\n"
  if not $ARGV[0];

Embperl::Execute($ARGV[0], $ARGV[0]);
------------------------------------(snip)</pre>

Here is an example of the code in TestSkell.hpp.epl
<pre>---------------------------------------
....

[-
my $file = $ARGV[0];
open(INPUT, $file);
@functions = ();
foreach $line (<INPUT>) {
   if ($line =~ m,(test\w+)\s*\(,) {
     push @functions, $1;
   }
}
-]

class TestCBody : public CppUnit::TestFixture  {
  CPPUNIT_TEST_SUITE( TestCBody );
  [$ foreach $name (@functions) $]
  CPPUNIT_TEST( [+ $name +] );
  [$ endforeach $]
  CPPUNIT_TEST_SUITE_END();
....
-----------------------------------</pre>
save the above in `file`.epl

Here is the unix makefile snippet that automates creating the source file.

<pre>--------------------------------------
%: %.epl
    rm -f $@
    perl -w execute_epl_file.pl $< > $@
------------------------------------------</pre>

Happy Coding!

add these lines to your makefile: