I've created a Python hack so I don't have to create/change my CppUnit test names in three places. The script reads a .cpp file ( eg tests.cpp ), finds the names of all your tests, and writes the appropriate declarations to the corresponding .h file.
Note that this is a personal hack, released here in case it's useful to anyone, and comes with no guarantees, assurances, or claims to being good code : ) This might also duplicate known techniques, be an inferior solution, be brittle code, etc, etc.
However, I have found it extremely useful, and thus I share it here.
Usage:
Note that the script expects spaces for your ( C++ ) source code indentation; if you use tabs this script may need modification.
Feel free to refactor this source code, or tell me why it sucks, or whatever you like : )
Python script follows:
import os, re
# change these to the name your test files
testSourceFileName = 'tests.cpp'
testHeaderFileName = 'tests.h'
# gather test declarations from the source file:
testSourceFile = file( testSourceFileName, 'r')
source = testSourceFile.read()
testSourceFile.close()
# note that we ignore tests whose names are line-commented out with '//'
# but would still include multi-line commented out tests, if the comment began on an earlier line
# so use line-comments '//' if you want to comment out tests
testNames = re.findall( r'\ns*void Tests::(test\w+)\(\)', source )
testMacros = [ ( ' CPPUNIT_TEST( %s );' % testName ) for testName in testNames ]
memberDeclarations = [ ( ' void %s();' % testName ) for testName in testNames ]
# read header file, delete old version:
testHeaderFile = file( testHeaderFileName, 'r')
header = testHeaderFile.read()
testHeaderFile.close()
os.remove( testHeaderFileName )
# substitute in the test names:
cre = re.compile( '( *// BEGIN_GENERATED_BLOCK_TEST_MACROS //)' +
'(.*?)' +
'( *// END_GENERATED_BLOCK_TEST_MACROS //)',
re.DOTALL )
header = cre.sub( r'\1\n\n' + '\n'.join( testMacros ) + r'\n\n\3', header )
cre = re.compile( '( *// BEGIN_GENERATED_BLOCK_MEMBER_DECLARATIONS //)' +
'(.*?)' +
'( *// END_GENERATED_BLOCK_MEMBER_DECLARATIONS //)',
re.DOTALL )
header = cre.sub( r'\1\n\n' + '\n'.join( memberDeclarations ) + r'\n\n\3', header )
# finally, create a 'new' header file with the generated contents:
testHeaderFile = file( testHeaderFileName, 'w')
testHeaderFile.write( header )
testHeaderFile.close()