cxxtestgen.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Getopt::Long;
  4. sub usage() {
  5. print STDERR "Usage: $0 [OPTIONS] <input file(s)>\n";
  6. print STDERR "Generate test source file for CxxTest.\n";
  7. print STDERR "\n";
  8. print STDERR " -v, --version Write CxxTest version\n";
  9. print STDERR " -o, --output=NAME Write output to file NAME\n";
  10. print STDERR " --runner=CLASS Create a main() function that runs CxxTest::CLASS\n";
  11. print STDERR " --gui=CLASS Like --runner, with GUI component\n";
  12. print STDERR " --error-printer Same as --runner=ErrorPrinter\n";
  13. print STDERR " --abort-on-fail Abort tests on failed asserts (like xUnit)\n";
  14. print STDERR " --have-std Use standard library (even if not found in tests)\n";
  15. print STDERR " --no-std Don't use standard library (even if found in tests)\n";
  16. print STDERR " --have-eh Use exception handling (even if not found in tests)\n";
  17. print STDERR " --no-eh Don't use exception handling (even if found in tests)\n";
  18. print STDERR " --longlong=[TYPE] Use TYPE as `long long' (defaut = long long)\n";
  19. print STDERR " --template=TEMPLATE Use TEMPLATE file to generate the test runner\n";
  20. print STDERR " --include=HEADER Include \"HEADER\" in test runner before other headers\n";
  21. print STDERR " --root Write CxxTest globals\n";
  22. print STDERR " --part Don't write CxxTest globals\n";
  23. print STDERR " --no-static-init Don't rely on static initialization\n";
  24. exit -1;
  25. }
  26. main();
  27. sub main {
  28. parseCommandline();
  29. scanInputFiles();
  30. writeOutput();
  31. }
  32. #
  33. # Handling the command line
  34. #
  35. my ($output, $runner, $gui, $template, $abortOnFail, $haveEh, $noEh, $haveStd, $noStd);
  36. my ($root, $part, $noStaticInit, $longlong, $factor);
  37. my @headers = ();
  38. sub parseCommandline() {
  39. @ARGV = expandWildcards(@ARGV);
  40. GetOptions( 'version' => \&printVersion,
  41. 'output=s' => \$output,
  42. 'template=s' => \$template,
  43. 'runner=s' => \$runner,
  44. 'gui=s', => \$gui,
  45. 'error-printer' => sub { $runner = 'ErrorPrinter'; $haveStd = 1; },
  46. 'abort-on-fail' => \$abortOnFail,
  47. 'have-eh' => \$haveEh,
  48. 'no-eh' => \$noEh,
  49. 'have-std' => \$haveStd,
  50. 'no-std' => \$noStd,
  51. 'include=s' => \@headers,
  52. 'root' => \$root,
  53. 'part' => \$part,
  54. 'no-static-init' => \$noStaticInit,
  55. 'factor' => \$factor,
  56. 'longlong:s' => \$longlong
  57. ) or usage();
  58. scalar @ARGV or $root or usage();
  59. if ( defined($noStaticInit) && (defined($root) || defined($part)) ) {
  60. die "--no-static-init cannot be used with --root/--part\n";
  61. }
  62. if ( $gui && !$runner ) {
  63. $runner = 'StdioPrinter';
  64. }
  65. if ( defined($longlong) && !$longlong ) {
  66. $longlong = 'long long';
  67. }
  68. foreach my $header (@headers) {
  69. if ( !($header =~ m/^["<].*[>"]$/) ) {
  70. $header = "\"$header\"";
  71. }
  72. }
  73. }
  74. sub printVersion() {
  75. print "This is CxxTest version 3.10.1.\n";
  76. exit 0;
  77. }
  78. sub expandWildcards() {
  79. my @result = ();
  80. while( my $fn = shift @_ ) {
  81. push @result, glob($fn);
  82. }
  83. return @result;
  84. }
  85. #
  86. # Reading the input files and scanning for test cases
  87. #
  88. my (@suites, $suite, $test, $inBlock);
  89. my $numTotalTests = 0;
  90. sub scanInputFiles() {
  91. foreach my $file (@ARGV) {
  92. scanInputFile( $file );
  93. }
  94. scalar @suites or $root or die("No tests defined\n");
  95. }
  96. sub scanInputFile($) {
  97. my ($file) = @_;
  98. open FILE, "<$file" or die("Cannot open input file \"$file\"\n");
  99. my $line;
  100. while (defined($line = <FILE>)) {
  101. scanLineForExceptionHandling( $line );
  102. scanLineForStandardLibrary( $line );
  103. scanLineForSuiteStart( $file, $., $line );
  104. if ( $suite ) {
  105. if ( lineBelongsToSuite( $suite, $., $line ) ) {
  106. scanLineForTest( $., $line );
  107. scanLineForCreate( $., $line );
  108. scanLineForDestroy( $., $line );
  109. }
  110. }
  111. }
  112. closeSuite();
  113. close FILE;
  114. }
  115. sub lineBelongsToSuite($$$) {
  116. my ($suite, $lineNo, $line) = @_;
  117. if ( !$suite->{'generated'} ) {
  118. return 1;
  119. }
  120. if ( !$inBlock ) {
  121. $inBlock = lineStartsBlock( $line );
  122. }
  123. if ( $inBlock ) {
  124. addLineToBlock( $suite->{'file'}, $lineNo, $line );
  125. }
  126. return $inBlock;
  127. }
  128. sub scanLineForExceptionHandling($) {
  129. my ($line) = @_;
  130. if ( $line =~ m/\b(try|throw|catch|TSM?_ASSERT_THROWS[A-Z_]*)\b/ ) {
  131. addExceptionHandling();
  132. }
  133. }
  134. sub scanLineForStandardLibrary($) {
  135. my ($line) = @_;
  136. if ( $line =~ m/\b(std\s*::|CXXTEST_STD|using\s+namespace\s+std\b|^\s*\#\s*include\s+<[a-z0-9]+>)/ ) {
  137. addStandardLibrary();
  138. }
  139. }
  140. sub scanLineForSuiteStart($$$) {
  141. my ($fileName, $lineNo, $line) = @_;
  142. if ( $line =~ m/\bclass\s+(\w+)\s*:\s*public\s+((::)?\s*CxxTest\s*::\s*)?TestSuite\b/ ) {
  143. startSuite( $1, $fileName, $lineNo, 0 );
  144. }
  145. if ( $line =~ m/\bCXXTEST_SUITE\s*\(\s*(\w*)\s*\)/ ) {
  146. print "$fileName:$lineNo: Warning: Inline test suites are deprecated.\n";
  147. startSuite( $1, $fileName, $lineNo, 1 );
  148. }
  149. }
  150. sub startSuite($$$$) {
  151. my ($name, $file, $line, $generated) = @_;
  152. closeSuite();
  153. $suite = { 'name' => $name,
  154. 'file' => $file,
  155. 'line' => $line,
  156. 'generated' => $generated,
  157. 'create' => 0,
  158. 'destroy' => 0,
  159. 'tests' => [],
  160. 'lines' => [] };
  161. }
  162. sub lineStartsBlock($) {
  163. my ($line) = @_;
  164. return $line =~ m/\bCXXTEST_CODE\s*\(/;
  165. }
  166. sub scanLineForTest($$) {
  167. my ($lineNo, $line) = @_;
  168. if ( $line =~ m/^([^\/]|\/[^\/])*\bvoid\s+([Tt]est\w+)\s*\(\s*(void)?\s*\)/ ) {
  169. addTest( $2, $lineNo );
  170. }
  171. }
  172. sub addTest($$$) {
  173. my ($name, $line) = @_;
  174. $test = { 'name' => $name,
  175. 'line' => $line };
  176. push @{suiteTests()}, $test;
  177. }
  178. sub addLineToBlock($$$) {
  179. my ($fileName, $lineNo, $line) = @_;
  180. $line = fixBlockLine( $fileName, $lineNo, $line );
  181. $line =~ s/^.*\{\{//;
  182. my $end = ($line =~ s/\}\}.*//s);
  183. push @{$suite->{'lines'}}, $line;
  184. if ( $end ) {
  185. $inBlock = 0;
  186. }
  187. }
  188. sub fixBlockLine($$$) {
  189. my ($fileName, $lineNo, $line) = @_;
  190. my $fileLine = cstr($fileName) . "," . $lineNo;
  191. $line =~ s/\b(E?TSM?_(ASSERT[A-Z_]*|FAIL))\s*\(/_$1($fileLine,/g;
  192. return $line;
  193. }
  194. sub scanLineForCreate($$) {
  195. my ($lineNo, $line) = @_;
  196. if ( $line =~ m/\bstatic\s+\w+\s*\*\s*createSuite\s*\(\s*(void)?\s*\)/ ) {
  197. addCreateSuite( $lineNo );
  198. }
  199. }
  200. sub scanLineForDestroy($$) {
  201. my ($lineNo, $line) = @_;
  202. if ( $line =~ m/\bstatic\s+void\s+destroySuite\s*\(\s*\w+\s*\*\s*\w*\s*\)/ ) {
  203. addDestroySuite( $lineNo );
  204. }
  205. }
  206. sub closeSuite() {
  207. if ( $suite && scalar @{suiteTests()} ) {
  208. verifySuite();
  209. rememberSuite();
  210. }
  211. undef $suite;
  212. }
  213. sub addCreateSuite($) {
  214. $suite->{'createSuite'} = $_[0];
  215. }
  216. sub addDestroySuite($) {
  217. $suite->{'destroySuite'} = $_[0];
  218. }
  219. sub addExceptionHandling() {
  220. $haveEh = 1 unless defined($noEh);
  221. }
  222. sub addStandardLibrary() {
  223. $haveStd = 1 unless defined($noStd);
  224. }
  225. sub verifySuite() {
  226. if (suiteCreateLine() || suiteDestroyLine()) {
  227. die("Suite ", suiteName(), " must have both createSuite() and destroySuite()\n")
  228. unless (suiteCreateLine() && suiteDestroyLine());
  229. }
  230. }
  231. sub rememberSuite() {
  232. push @suites, $suite;
  233. $numTotalTests += scalar @{$suite->{'tests'}};
  234. }
  235. sub suiteName() { return $suite->{'name'}; }
  236. sub suiteTests() { return $suite->{'tests'}; }
  237. sub suiteCreateLine() { return $suite->{'createSuite'}; }
  238. sub suiteDestroyLine() { return $suite->{'destroySuite'}; }
  239. sub fileName() { return $suite->{'file'}; }
  240. sub fileString() { return cstr(fileName()); }
  241. sub testName() { return $test->{'name'}; }
  242. sub testLine() { return $test->{'line'}; }
  243. sub suiteObject() { return "suite_".suiteName(); }
  244. sub cstr($) {
  245. my $file = $_[0];
  246. $file =~ s/\\/\\\\/g;
  247. return "\"".$file."\"";
  248. }
  249. #
  250. # Writing the test source file
  251. #
  252. sub writeOutput() {
  253. $template ? writeTemplateOutput() : writeSimpleOutput();
  254. }
  255. sub startOutputFile() {
  256. if ( !standardOutput() ) {
  257. open OUTPUT_FILE,">$output" or die("Cannot create output file \"$output\"\n");
  258. select OUTPUT_FILE;
  259. }
  260. print "/* Generated file, do not edit */\n\n";
  261. }
  262. sub standardOutput() {
  263. return !$output;
  264. }
  265. sub writeSimpleOutput() {
  266. startOutputFile();
  267. writePreamble();
  268. writeMain();
  269. writeWorld();
  270. }
  271. my ($didPreamble, $didWorld);
  272. sub writeTemplateOutput() {
  273. openTemplateFile();
  274. startOutputFile();
  275. my $line;
  276. while (defined($line = <TEMPLATE_FILE>)) {
  277. if ( $line =~ m/^\s*\#\s*include\s*<cxxtest\// ) {
  278. writePreamble();
  279. print $line;
  280. } elsif ( $line =~ m/^\s*<CxxTest\s+preamble>\s*$/ ) {
  281. writePreamble();
  282. } elsif ( $line =~ m/^\s*<CxxTest\s+world>\s*$/ ) {
  283. writeWorld();
  284. } else {
  285. print $line;
  286. }
  287. }
  288. }
  289. sub openTemplateFile() {
  290. open TEMPLATE_FILE, "<$template" or die("Cannot open template file \"$template\"\n");
  291. }
  292. sub writePreamble() {
  293. return if $didPreamble;
  294. print "#ifndef CXXTEST_RUNNING\n";
  295. print "#define CXXTEST_RUNNING\n";
  296. print "#endif\n";
  297. print "\n";
  298. if ( $haveStd ) {
  299. print "#define _CXXTEST_HAVE_STD\n";
  300. }
  301. if ( $haveEh ) {
  302. print "#define _CXXTEST_HAVE_EH\n";
  303. }
  304. if ( $abortOnFail ) {
  305. print "#define _CXXTEST_ABORT_TEST_ON_FAIL\n";
  306. }
  307. if ( $longlong ) {
  308. print "#define _CXXTEST_LONGLONG $longlong\n";
  309. }
  310. if ( $factor ) {
  311. print "#define _CXXTEST_FACTOR\n";
  312. }
  313. print "#include \"sys.hpp\"\n";
  314. foreach my $header (@headers) {
  315. print "#include $header\n";
  316. }
  317. print "#include <cxxtest/TestListener.h>\n";
  318. print "#include <cxxtest/TestTracker.h>\n";
  319. print "#include <cxxtest/TestRunner.h>\n";
  320. print "#include <cxxtest/RealDescriptions.h>\n";
  321. print "#include <cxxtest/$runner.h>\n" if $runner;
  322. print "#include <cxxtest/$gui.h>\n" if $gui;
  323. print "\n";
  324. $didPreamble = 1;
  325. }
  326. sub writeWorld() {
  327. return if $didWorld;
  328. writePreamble();
  329. writeSuites();
  330. ($root or !$part) and writeRoot();
  331. $noStaticInit and writeInitialize();
  332. $didWorld = 1;
  333. }
  334. sub writeSuites() {
  335. foreach (@suites) {
  336. $suite = $_;
  337. writeInclude(fileName());
  338. if ( $suite->{'generated'} ) { generateSuite(); }
  339. dynamicSuite() ? writeSuitePointer() : writeSuiteObject();
  340. writeTestList();
  341. writeSuiteDescription();
  342. writeTestDescriptions();
  343. }
  344. }
  345. sub dynamicSuite() {
  346. return suiteCreateLine();
  347. }
  348. my $lastIncluded;
  349. sub writeInclude($) {
  350. my $file = $_[0];
  351. return if $lastIncluded && ($file eq $lastIncluded);
  352. print "#include \"$file\"\n\n";
  353. $lastIncluded = $file;
  354. }
  355. sub generateSuite() {
  356. print "class ", suiteName(), " : public CxxTest::TestSuite {\n";
  357. print "public:\n";
  358. foreach my $line (@{$suite->{'lines'}}) {
  359. print $line;
  360. }
  361. print "};\n\n";
  362. }
  363. sub writeTestDescriptionsBase() {
  364. my $class = "TestDescriptionBase_" . suiteName();
  365. print "class $class : public CxxTest::TestDescription {\n";
  366. print "public:\n";
  367. print " const char *file() const { return ", fileString(), "; }\n";
  368. print " const char *suiteName() const { return \"", suiteName(), "\"; }\n";
  369. print "};\n\n";
  370. }
  371. sub writeSuitePointer() {
  372. if ( $noStaticInit ) {
  373. print "static ", suiteName(), " *", suiteObject(), ";\n\n";
  374. } else {
  375. print "static ", suiteName(), " *", suiteObject(), " = 0;\n\n";
  376. }
  377. }
  378. sub writeSuiteObject() {
  379. print "static ", suiteName(), " ", suiteObject(), ";\n\n";
  380. }
  381. sub testList() {
  382. return "Tests_" . suiteName();
  383. }
  384. sub writeTestList() {
  385. if ( $noStaticInit ) {
  386. printf "static CxxTest::List %s;\n", testList();
  387. } else {
  388. printf "static CxxTest::List %s = { 0, 0 };\n", testList();
  389. }
  390. }
  391. sub writeTestDescriptions() {
  392. foreach (@{suiteTests()}) {
  393. $test = $_;
  394. writeTestDescription();
  395. }
  396. }
  397. sub suiteDescription() {
  398. return "suiteDescription_" . suiteName();
  399. }
  400. sub writeTestDescription() {
  401. my $class = "TestDescription_" . suiteName() . "_" . testName();
  402. printf "static class $class : public CxxTest::RealTestDescription {\n";
  403. printf "public:\n";
  404. $noStaticInit or
  405. printf " $class() : CxxTest::RealTestDescription( %s, %s, %s, \"%s\" ) {}\n",
  406. testList(), suiteDescription(), testLine(), testName();
  407. printf " void runTest() { %s }\n", dynamicSuite() ? dynamicRun() : staticRun();
  408. printf "} testDescription_%s_%s;\n\n", suiteName(), testName();
  409. }
  410. sub dynamicRun() {
  411. return sprintf( "if ( %s ) %s->%s();", suiteObject(), suiteObject(), testName() );
  412. }
  413. sub staticRun() {
  414. return sprintf( "%s.%s();", suiteObject(), testName() );
  415. }
  416. sub writeSuiteDescription() {
  417. dynamicSuite() ? writeDynamicDescription() : writeStaticDescription();
  418. }
  419. sub writeDynamicDescription() {
  420. printf "CxxTest::DynamicSuiteDescription<%s> %s", suiteName(), suiteDescription();
  421. if ( !$noStaticInit ) {
  422. printf "( %s, %s, \"%s\", %s, %s, %s, %s )",
  423. fileString(), $suite->{'line'}, suiteName(), testList(),
  424. suiteObject(), suiteCreateLine(), suiteDestroyLine();
  425. }
  426. print ";\n\n";
  427. }
  428. sub writeStaticDescription() {
  429. printf "CxxTest::StaticSuiteDescription %s", suiteDescription();
  430. if ( !$noStaticInit ) {
  431. printf "( %s, %s, \"%s\", %s, %s )", fileString(), $suite->{'line'}, suiteName(), suiteObject(), testList();
  432. }
  433. print ";\n\n";
  434. }
  435. sub writeRoot() {
  436. print "#include <cxxtest/Root.cpp>\n";
  437. }
  438. sub writeInitialize() {
  439. print "namespace CxxTest {\n";
  440. print " void initialize()\n";
  441. print " {\n";
  442. foreach (@suites) {
  443. $suite = $_;
  444. printf " %s.initialize();\n", testList();
  445. if ( dynamicSuite() ) {
  446. printf " %s = 0;\n", suiteObject();
  447. printf " %s.initialize( %s, %s, \"%s\", %s, %s, %s, %s );\n",
  448. suiteDescription(), fileString(), $suite->{'line'}, suiteName(), testList(),
  449. suiteObject(), suiteCreateLine(), suiteDestroyLine();
  450. } else {
  451. printf " %s.initialize( %s, %s, \"%s\", %s, %s );\n",
  452. suiteDescription(), fileString(), $suite->{'line'}, suiteName(), suiteObject(), testList();
  453. }
  454. foreach (@{suiteTests()}) {
  455. $test = $_;
  456. printf " testDescription_%s_%s.initialize( %s, %s, %s, \"%s\" );\n",
  457. suiteName(), testName(), testList(), suiteDescription(), testLine(), testName();
  458. }
  459. }
  460. print " }\n";
  461. print "}\n";
  462. }
  463. sub writeMain() {
  464. if ( $gui ) {
  465. print "int main( int argc, char *argv[] ) {\n";
  466. $noStaticInit &&
  467. print " CxxTest::initialize();\n";
  468. print " return CxxTest::GuiTuiRunner<CxxTest::$gui, CxxTest::$runner>( argc, argv ).run();\n";
  469. print "}\n";
  470. }
  471. elsif ( $runner ) {
  472. print "int main() {\n";
  473. $noStaticInit &&
  474. print " CxxTest::initialize();\n";
  475. print " return CxxTest::$runner().run();\n";
  476. print "}\n";
  477. }
  478. }