[Cppunit-cvs] cppunit2/src/cpput assert.cpp,1.12,1.13 exceptionguard.cpp,1.6,1.7 lighttestrunner.cpp
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-08-10 21:34:49
|
Update of /cvsroot/cppunit/cppunit2/src/cpput In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27815/src/cpput Modified Files: assert.cpp exceptionguard.cpp lighttestrunner.cpp testcase.cpp testinfo.cpp Log Message: * Replaced function in namespace TestInfo by exposing the TestInfoData class and renaming it TestInfo. TestInfo in now an exposed per thread singleton. * startNewTest() is now called from TestCase::runTest() instead of being the test runner responsability. There still a few call to this method in cpput bootstrap test (which use assertion without run a test case). Index: assert.cpp =================================================================== RCS file: /cvsroot/cppunit/cppunit2/src/cpput/assert.cpp,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** assert.cpp 10 Aug 2005 07:45:23 -0000 1.12 --- assert.cpp 10 Aug 2005 21:34:29 -0000 1.13 *************** *** 12,17 **** fail( const Message &message ) { ! TestInfo::currentAssertion().setMessages( message ); ! TestInfo::realizeAssertion(); } --- 12,17 ---- fail( const Message &message ) { ! currentAssertion().setMessages( message ); ! realizeAssertion(); } Index: exceptionguard.cpp =================================================================== RCS file: /cvsroot/cppunit/cppunit2/src/cpput/exceptionguard.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** exceptionguard.cpp 8 Aug 2005 22:10:21 -0000 1.6 --- exceptionguard.cpp 10 Aug 2005 21:34:29 -0000 1.7 *************** *** 73,77 **** messages.add( "caught unexpected exception of unknown type (not a subclass of std::exception)." ); fault.setMessages( messages ); ! TestInfo::handleUnexpectedException( fault ); } --- 73,77 ---- messages.add( "caught unexpected exception of unknown type (not a subclass of std::exception)." ); fault.setMessages( messages ); ! TestInfo::threadInstance().handleUnexpectedException( fault ); } *************** *** 91,95 **** fault.setTestData( "expection-type", exceptionType ); fault.setTestData( "expection-message", what ); ! TestInfo::handleUnexpectedException( fault ); } }; --- 91,95 ---- fault.setTestData( "expection-type", exceptionType ); fault.setTestData( "expection-message", what ); ! TestInfo::threadInstance().handleUnexpectedException( fault ); } }; Index: lighttestrunner.cpp =================================================================== RCS file: /cvsroot/cppunit/cppunit2/src/cpput/lighttestrunner.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** lighttestrunner.cpp 10 Aug 2005 07:45:23 -0000 1.6 --- lighttestrunner.cpp 10 Aug 2005 21:34:29 -0000 1.7 *************** *** 30,34 **** LightTestRunner::runAllTests() { ! TestInfo::setTestResultUpdater( *this ); for ( Tests::iterator it = tests_.begin(); it != tests_.end(); ++it ) runTest( *it ); --- 30,34 ---- LightTestRunner::runAllTests() { ! TestInfo::threadInstance().setTestResultUpdater( *this ); for ( Tests::iterator it = tests_.begin(); it != tests_.end(); ++it ) runTest( *it ); *************** *** 74,78 **** printf( "Testing %s : ", getTestPath().c_str() ); assertions_.clear(); - TestInfo::startNewTest(); ++testRun_; bool succeeded = testCase->runTest(); --- 74,77 ---- Index: testinfo.cpp =================================================================== RCS file: /cvsroot/cppunit/cppunit2/src/cpput/testinfo.cpp,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** testinfo.cpp 10 Aug 2005 07:42:52 -0000 1.13 --- testinfo.cpp 10 Aug 2005 21:34:29 -0000 1.14 *************** *** 18,21 **** --- 18,22 ---- } + void Assertion::setLocation( const SourceLocation &location ) *************** *** 24,27 **** --- 25,29 ---- } + const SourceLocation & Assertion::location() const *************** *** 30,33 **** --- 32,36 ---- } + void Assertion::setKind( Kind kind ) *************** *** 36,39 **** --- 39,43 ---- } + Assertion::Kind Assertion::kind() const *************** *** 42,45 **** --- 46,50 ---- } + void Assertion::setMessages( const Message &messages ) *************** *** 48,51 **** --- 53,57 ---- } + const Message & Assertion::messages() const *************** *** 54,57 **** --- 60,64 ---- } + void Assertion::setTestDataType( const CppTL::ConstString &type ) *************** *** 60,63 **** --- 67,71 ---- } + const CppTL::ConstString & Assertion::testDataType() const *************** *** 66,69 **** --- 74,78 ---- } + void Assertion::setTestData( const CppTL::ConstString &name, *************** *** 75,78 **** --- 84,88 ---- } + const OpenTest::Properties & Assertion::testData() const *************** *** 81,84 **** --- 91,95 ---- } + CppTL::ConstString Assertion::toString() const *************** *** 107,110 **** --- 118,123 ---- } + + // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// *************** *** 157,325 **** // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! // Class TestInfoData // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! class TestInfoData { ! public: ! TestInfoData() ! : assertionType_( abortingAssertion ) ! , assertionCount_( 0 ) ! , failedAssertionCount_( 0 ) ! , updater_( 0 ) ! { ! } - void startNewTest() - { - testStatus_ = TestStatus( TestStatus::passed ); - currentAssertion_ = Assertion(); - assertionCount_ = 0; - failedAssertionCount_ = 0; - assertionType_ = abortingAssertion; - } ! void realizeAssertion() ! { ! ++failedAssertionCount_; ! if ( updater_ ) ! updater_->addResultAssertion( currentAssertion_ ); - if ( assertionType_ == abortingAssertion ) - { - std::string message; - if ( abortingAssertionMode_ != fastAbortingAssertion ) - message = currentAssertion_.toString().c_str(); - throw AbortingAssertionException( message ); - } - } ! TestStatus &getUpdatedTestStatus() ! { ! if ( !testStatus_.hasFailed() && failedAssertionCount_ > 0 ) ! testStatus_.setStatus( TestStatus::failed ); ! return testStatus_; ! } - TestResultUpdater *updater_; - TestStatus testStatus_; - Assertion currentAssertion_; - AssertionType assertionType_; - AbortingAssertionMode abortingAssertionMode_; - unsigned int assertionCount_; - unsigned int failedAssertionCount_; - }; ! // ////////////////////////////////////////////////////////////////// ! // ////////////////////////////////////////////////////////////////// ! // Namespace TestInfo ! // ////////////////////////////////////////////////////////////////// ! // ////////////////////////////////////////////////////////////////// - namespace TestInfo { - CppTL::ThreadLocalStorage<TestInfoDataPtr> staticData; ! static TestInfoData &data() ! { ! TestInfoDataPtr &data = staticData.get(); ! if ( !data ) ! data.reset( new TestInfoData() ); ! return *data; ! } - void setTestResultUpdater( TestResultUpdater &updater ) - { - data().updater_ = &updater; - } ! void startNewTest() ! { ! data().startNewTest(); ! } ! void newAssertion( AssertionType type, ! const char *fileName, ! unsigned int lineNumber, ! const char *functionName ) ! { ! data().currentAssertion_ = Assertion( Assertion::assertion, ! SourceLocation( fileName, ! lineNumber, ! functionName ) ); ! ++(data().assertionCount_); ! data().assertionType_ = type; ! } ! void setAssertionType( AssertionType type ) { ! data().assertionType_ = type; } - AssertionType assertionType() - { - return data().assertionType_; - } ! void realizeAssertion() ! { ! data().realizeAssertion(); ! } - void log( const OpenTest::Value &log ) - { - if ( data().updater_ ) - data().updater_->addResultLog( log ); - } ! void handleUnexpectedException( const Assertion &fault ) ! { ! if ( data().updater_ ) ! data().updater_->addResultAssertion( fault ); ! data().testStatus_.setStatus( TestStatus::failed ); ! } - void setAbortingAssertionMode( AbortingAssertionMode mode ) - { - data().abortingAssertionMode_ = mode; - } - Assertion & - currentAssertion() - { - return data().currentAssertion_; - } ! TestStatus & ! getUpdatedTestStatus() ! { ! return data().getUpdatedTestStatus(); ! } // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! // Class ScopedContextOverride // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// - ScopedContextOverride::ScopedContextOverride() - : context_( staticData.get() ) - { - staticData.get().reset( new TestInfoData() ); - } ! ScopedContextOverride::~ScopedContextOverride() ! { ! staticData = context_; ! } ! } // namespace TestInfo --- 170,339 ---- // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! // Class TestInfo // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// + static CppTL::ThreadLocalStorage<TestInfoPtr> perThreadStaticData; + + TestInfo & + TestInfo::threadInstance() + { + TestInfoPtr &data = perThreadStaticData.get(); + if ( !data ) + data.reset( new TestInfo() ); + return *data; + } + ! TestInfo::TestInfo() ! : assertionType_( abortingAssertion ) ! , assertionCount_( 0 ) ! , failedAssertionCount_( 0 ) ! , updater_( 0 ) { ! } ! void ! TestInfo::setTestResultUpdater( TestResultUpdater &updater ) ! { ! updater_ = &updater; ! } ! void ! TestInfo::startNewTest() ! { ! testStatus_ = TestStatus( TestStatus::passed ); ! currentAssertion_ = Assertion(); ! assertionCount_ = 0; ! failedAssertionCount_ = 0; ! assertionType_ = abortingAssertion; ! } ! TestStatus & ! TestInfo::getUpdatedTestStatus() ! { ! if ( !testStatus_.hasFailed() && failedAssertionCount_ > 0 ) ! testStatus_.setStatus( TestStatus::failed ); ! return testStatus_; ! } ! void ! TestInfo::newAssertion( AssertionType type, ! const char *fileName, ! unsigned int lineNumber, ! const char *functionName ) ! { ! currentAssertion_ = Assertion( Assertion::assertion, ! SourceLocation( fileName, ! lineNumber, ! functionName ) ); ! ++assertionCount_; ! assertionType_ = type; ! } ! Assertion & ! TestInfo::currentAssertion() ! { ! return currentAssertion_; ! } ! void ! TestInfo::realizeAssertion() ! { ! ++failedAssertionCount_; ! if ( updater_ ) ! updater_->addResultAssertion( currentAssertion_ ); ! if ( assertionType_ == abortingAssertion ) { ! std::string message; ! if ( abortingAssertionMode_ != fastAbortingAssertion ) ! message = currentAssertion_.toString().c_str(); ! throw AbortingAssertionException( message ); } + } ! void ! TestInfo::handleUnexpectedException( const Assertion &fault ) ! { ! if ( updater_ ) ! updater_->addResultAssertion( fault ); ! testStatus_.setStatus( TestStatus::failed ); ! } ! void ! TestInfo::setAbortingAssertionMode( AbortingAssertionMode mode ) ! { ! abortingAssertionMode_ = mode; ! } + void + TestInfo::log( const OpenTest::Value &log ) + { + if ( updater_ ) + updater_->addResultLog( log ); + } ! // ////////////////////////////////////////////////////////////////// ! // ////////////////////////////////////////////////////////////////// ! // Class ScopedContextOverride ! // ////////////////////////////////////////////////////////////////// ! // ////////////////////////////////////////////////////////////////// + TestInfo::ScopedContextOverride::ScopedContextOverride() + : context_( perThreadStaticData.get() ) + { + perThreadStaticData.get().reset( new TestInfo() ); + } + TestInfo::ScopedContextOverride::~ScopedContextOverride() + { + perThreadStaticData = context_; + } // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! // Short-hand functions in namespace CppUT // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// ! //void newAssertion( AssertionType type, ! // const char *fileName, ! // unsigned int lineNumber, ! // const char *functionName ) ! //{ ! // TestInfo::data().newAssertion( type, fileName, lineNumber, functionName ); ! //} ! Assertion & ! currentAssertion() ! { ! return TestInfo::threadInstance().currentAssertion(); ! } ! ! ! void realizeAssertion() ! { ! TestInfo::threadInstance().realizeAssertion(); ! } ! ! ! void log( const OpenTest::Value &log ) ! { ! TestInfo::threadInstance().log( log ); ! } Index: testcase.cpp =================================================================== RCS file: /cvsroot/cppunit/cppunit2/src/cpput/testcase.cpp,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** testcase.cpp 8 Aug 2005 22:10:21 -0000 1.11 --- testcase.cpp 10 Aug 2005 21:34:29 -0000 1.12 *************** *** 45,48 **** --- 45,49 ---- AbstractTestCase::runTest( const ExceptionGuard &guardsChain ) { + TestInfo::threadInstance().startNewTest(); bool initialized = guardsChain.protect( CppTL::memfn0( this, &AbstractTestCase::setUp ) ); *************** *** 54,58 **** } ! return !TestInfo::getUpdatedTestStatus().hasFailed(); } --- 55,59 ---- } ! return !TestInfo::threadInstance().getUpdatedTestStatus().hasFailed(); } |