1 #include <cxxtest/TestRunner.h>
   2 #include <cxxtest/TestListener.h>
   3 #include <cxxtest/TestTracker.h>
   4 #include <cxxtest/ValueTraits.h>
   5 #include <cxxtest/ValueTraits.h>
   6 #include <cxxtest/ErrorPrinter.h>
   7 
   8 class CoverageScannerListener : public CxxTest::ErrorPrinter
   9 {
  10   public:
  11     CoverageScannerListener(
  12         std::ostream &o=std::cout,
  13         const char *preLine = ":",
  14         const char *postLine = ""
  15         ) : CxxTest::ErrorPrinter( o, preLine , postLine ) { }
  16 
  17     int run()
  18     {
  19       return CxxTest::ErrorPrinter::run();
  20     }
  21 
  22     void enterTest( const CxxTest::TestDescription & desc)
  23     {
  24       test_passed=true;
  25 #ifdef __COVERAGESCANNER__
  26 
  27       // Adjusting the name of the test to display the tests
  28       // in a tree view in CoverageBrowser
  29       std::string testname="CxxTest/";
  30       testname += desc.suiteName();
  31       testname += "/";
  32       testname += desc.testName();
  33 
  34       // Reset the code coverage data to get only the code coverage
  35       // of the actual unit test.
  36       __coveragescanner_clear();
  37       __coveragescanner_testname(testname.c_str());
  38 #endif
  39       return CxxTest::ErrorPrinter::enterTest( desc );
  40     }
  41 
  42     void leaveTest( const CxxTest::TestDescription & desc)
  43     {
  44 #ifdef __COVERAGESCANNER__
  45       // Recording the execution state in the coverage report
  46       if (test_passed)
  47         __coveragescanner_teststate("PASSED");
  48       else
  49         __coveragescanner_teststate("FAILED");
  50 
  51       // Saving the code coverage report of the unit test
  52       __coveragescanner_save();
  53 #endif
  54       return CxxTest::ErrorPrinter::leaveTest( desc );
  55     }
  56 
  57     void failedTest(
  58         const char *file,
  59         unsigned line,
  60         const char *expression
  61         )
  62     { // Just record that the test fails
  63       test_passed=false;
  64       return CxxTest::ErrorPrinter::failedTest(file,line,expression );
  65     }
  66 
  67     void failedAssert(
  68         const char *file,
  69         unsigned line,
  70         const char *expression
  71         )
  72     { // Just record that the test fails
  73       test_passed=false;
  74       return CxxTest::ErrorPrinter::failedAssert(file,line,expression );
  75     }
  76 
  77     void failedAssertEquals(
  78         const char *file,
  79         unsigned line,
  80         const char *xStr,
  81         const char *yStr,
  82         const char *x,
  83         const char *y
  84         )
  85     { // Just record that the test fails
  86       test_passed=false;
  87       return CxxTest::ErrorPrinter::failedAssertEquals(
  88           file,
  89           line,
  90           xStr,
  91           yStr,
  92           x,
  93           y );
  94     }
  95 
  96     void failedAssertSameData(
  97         const char *file,
  98         unsigned line,
  99         const char *xStr,
 100         const char *yStr,
 101         const char *sizeStr,
 102         const void *x,
 103         const void *y,
 104         unsigned size
 105         )
 106     { // Just record that the test fails
 107       test_passed=false;
 108       return CxxTest::ErrorPrinter::failedAssertSameData(
 109           file,
 110           line,
 111           xStr,
 112           yStr,
 113           sizeStr,
 114           x,
 115           y,
 116           size );
 117     }
 118 
 119     void failedAssertDelta(
 120         const char *file,
 121         unsigned line,
 122         const char *xStr,
 123         const char *yStr,
 124         const char *dStr,
 125         const char *x,
 126         const char *y,
 127         const char *d
 128         )
 129     { // Just record that the test fails
 130       test_passed=false;
 131       return CxxTest::ErrorPrinter::failedAssertDelta(
 132           file,
 133           line,
 134           xStr,
 135           yStr,
 136           dStr,
 137           x,
 138           y,
 139           d );
 140     }
 141 
 142     void failedAssertDiffers(
 143         const char *file,
 144         unsigned line,
 145         const char *xStr,
 146         const char *yStr,
 147         const char *value
 148         )
 149     { // Just record that the test fails
 150       test_passed=false;
 151       return CxxTest::ErrorPrinter::failedAssertDiffers(
 152           file,
 153           line,
 154           xStr,
 155           yStr,
 156           value );
 157     }
 158 
 159     void failedAssertLessThan(
 160         const char *file,
 161         unsigned line,
 162         const char *xStr,
 163         const char *yStr,
 164         const char *x,
 165         const char *y
 166         )
 167     { // Just record that the test fails
 168       test_passed=false;
 169       return CxxTest::ErrorPrinter::failedAssertLessThan(
 170           file,
 171           line,
 172           xStr,
 173           yStr,
 174           x,
 175           y );
 176     }
 177 
 178     void failedAssertLessThanEquals(
 179         const char *file,
 180         unsigned line,
 181         const char *xStr,
 182         const char *yStr,
 183         const char *x,
 184         const char *y
 185         )
 186     { // Just record that the test fails
 187       test_passed=false;
 188       return CxxTest::ErrorPrinter::failedAssertLessThanEquals(
 189           file,
 190           line,
 191           xStr,
 192           yStr,
 193           x,
 194           y );
 195     }
 196 
 197     void failedAssertRelation(
 198         const char *file,
 199         unsigned line,
 200         const char *relation,
 201         const char *xStr,
 202         const char *yStr,
 203         const char *x,
 204         const char *y
 205         )
 206     { // Just record that the test fails
 207       test_passed=false;
 208       return CxxTest::ErrorPrinter::failedAssertRelation(
 209           file,
 210           line,
 211           relation,
 212           xStr,
 213           yStr,
 214           x,
 215           y
 216           );
 217     }
 218 
 219     void failedAssertPredicate(
 220         const char *file,
 221         unsigned line,
 222         const char *predicate,
 223         const char *xStr,
 224         const char *x )
 225     { // Just record that the test fails
 226       test_passed=false;
 227       return CxxTest::ErrorPrinter::failedAssertPredicate(
 228           file,
 229           line,
 230           predicate,
 231           xStr,
 232           x
 233           );
 234     }
 235 
 236     void failedAssertThrows(
 237         const char *file,
 238         unsigned line,
 239         const char *expression,
 240         const char *type,
 241         bool otherThrown
 242         )
 243     { // Just record that the test fails
 244       test_passed=false;
 245       return CxxTest::ErrorPrinter::failedAssertThrows(
 246           file,
 247           line,
 248           expression,
 249           type,
 250           otherThrown );
 251     }
 252 
 253     void failedAssertThrowsNot(
 254         const char *file,
 255         unsigned line,
 256         const char *expression
 257         )
 258     { // Just record that the test fails
 259       test_passed=false;
 260       return CxxTest::ErrorPrinter::failedAssertThrowsNot(
 261           file,
 262           line,
 263           expression );
 264     }
 265 
 266   private:
 267     bool test_passed;
 268 };
 269 
 270 
 271 int main()
 272 {
 273 #ifdef __COVERAGESCANNER__
 274   __coveragescanner_install(argv[0]);
 275 #endif
 276 
 277   // Use "CoverageScannerListener().run()" instead of "CxxTest::ErrorPrinter().run()"
 278   return CoverageScannerListener().run();
 279 }