TestDriver.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2008, Yahoo! Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <string>
  17. #include <cppunit/TestRunner.h>
  18. #include <cppunit/CompilerOutputter.h>
  19. #include <cppunit/TestResult.h>
  20. #include <cppunit/TestResultCollector.h>
  21. #include <cppunit/TextTestProgressListener.h>
  22. #include <cppunit/BriefTestProgressListener.h>
  23. #include <cppunit/extensions/TestFactoryRegistry.h>
  24. #include <stdexcept>
  25. #include <cppunit/Exception.h>
  26. #include <cppunit/TestFailure.h>
  27. #include <cppunit/XmlOutputter.h>
  28. #include <fstream>
  29. #include "Util.h"
  30. using namespace std;
  31. CPPUNIT_NS_BEGIN
  32. class EclipseOutputter: public CompilerOutputter
  33. {
  34. public:
  35. EclipseOutputter(TestResultCollector *result,ostream &stream):
  36. CompilerOutputter(result,stream,"%p:%l: "),stream_(stream)
  37. {
  38. }
  39. virtual void printFailedTestName( TestFailure *failure ){}
  40. virtual void printFailureMessage( TestFailure *failure )
  41. {
  42. stream_<<": ";
  43. Message msg = failure->thrownException()->message();
  44. stream_<< msg.shortDescription();
  45. string text;
  46. for(int i=0; i<msg.detailCount();i++){
  47. text+=msg.detailAt(i);
  48. if(i+1!=msg.detailCount())
  49. text+=", ";
  50. }
  51. if(text.length()!=0)
  52. stream_ <<" ["<<text<<"]";
  53. stream_<<"\n";
  54. }
  55. ostream& stream_;
  56. };
  57. CPPUNIT_NS_END
  58. int main( int argc, char* argv[] ) {
  59. // if command line contains "-ide" then this is the post build check
  60. // => the output must be in the compiler error format.
  61. //bool selfTest = (argc > 1) && (std::string("-ide") == argv[1]);
  62. globalTestConfig.addConfigFromCmdLine(argc,argv);
  63. // Create the event manager and test controller
  64. CPPUNIT_NS::TestResult controller;
  65. // Add a listener that colllects test result
  66. CPPUNIT_NS::TestResultCollector result;
  67. controller.addListener( &result );
  68. // Add a listener that print dots as tests run.
  69. // CPPUNIT_NS::TextTestProgressListener progress;
  70. CPPUNIT_NS::BriefTestProgressListener progress;
  71. controller.addListener( &progress );
  72. CPPUNIT_NS::TestRunner runner;
  73. runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
  74. try
  75. {
  76. cout << "Running " << globalTestConfig.getTestName();
  77. runner.run( controller, globalTestConfig.getTestName());
  78. cout<<endl;
  79. // Print test in a compiler compatible format.
  80. CPPUNIT_NS::EclipseOutputter outputter( &result,cout);
  81. outputter.write();
  82. // Uncomment this for XML output
  83. #ifdef ENABLE_XML_OUTPUT
  84. std::ofstream file( "tests.xml" );
  85. CPPUNIT_NS::XmlOutputter xml( &result, file );
  86. xml.setStyleSheet( "report.xsl" );
  87. xml.write();
  88. file.close();
  89. #endif
  90. }
  91. catch ( std::invalid_argument &e ) // Test path not resolved
  92. {
  93. cout<<"\nERROR: "<<e.what()<<endl;
  94. return 0;
  95. }
  96. return result.wasSuccessful() ? 0 : 1;
  97. }