4 \brief Implementation of the custom runner allowing start a subset of tests
7 #include <TestLib_Runner.h>
8 #include <cppunit/TestPath.h>
9 #include <cppunit/TestResult.h>
14 Conversion of the given string to lower-case
15 @param theStr the string to convert to lower-case
16 @return the converted string
18 std::string ToLower( const std::string& theStr )
20 std::string aStr = theStr;
21 std::transform( aStr.begin(), aStr.end(), aStr.begin(), ::tolower );
26 Add a substring pattern to test runner
27 @param theSubstr the substring pattern to add
29 void TestLib_Runner::Add( const std::string& theSubstr )
31 mySubStrings.push_back( ToLower( theSubstr ) );
35 Load the configuration file with substring patterns
36 @param theConfigFile the path to the configuration file
37 @return if the operation is successful
39 bool TestLib_Runner::Load( const std::string& theConfigFile )
41 std::ifstream anInput( theConfigFile.c_str() );
42 bool isOK = anInput.is_open();
47 while( std::getline( anInput, aLine ) )
49 int aPos = aLine.find( "//" );
50 if( aPos != std::string::npos )
54 aLine.erase( 0, aLine.find_first_not_of( ' ' ) );
55 aLine.erase( aLine.find_last_not_of( ' ' ) + 1 );
65 Determine if the given test name is allowed by patterns
66 @param theTestName the test name
67 @return true if the test name is allowed
69 bool TestLib_Runner::IsAllowed( const std::string& theTestName ) const
71 if( mySubStrings.empty() )
74 std::string aTestName = ToLower( theTestName );
76 std::list<std::string>::const_iterator anIt = mySubStrings.begin(), aLast = mySubStrings.end();
77 for( ; anIt!=aLast; ++anIt )
78 if( aTestName.find( *anIt ) != std::string::npos )
86 Determine list of tests allowed by patterns from the given CPPUnit tests tree
87 @param theRoot the root of the CPPUnit tests tree
88 @param theAllowedTests the returned vector of allowed tests names
90 void TestLib_Runner::GetTests( CPPUNIT_NS::Test* theRoot, std::list<std::string>& theAllowedTests ) const
92 for( int i=0, n=theRoot->getChildTestCount(); i<n; i++ )
94 CPPUNIT_NS::Test* aTest = theRoot->getChildTestAt( i );
95 std::string aName = aTest->getName();
96 if( IsAllowed( aName ) )
97 theAllowedTests.push_back( aName );
99 GetTests( aTest, theAllowedTests );
104 Run all necessary tests
105 @param theController the controller of the tests results
107 void TestLib_Runner::Run( CPPUNIT_NS::TestResult& theController )
109 std::list<std::string> aTests;
110 GetTests( m_suite, aTests );
111 std::list<std::string>::const_iterator anIt = aTests.begin(), aLast = aTests.end();
112 for( ; anIt!=aLast; ++anIt )
114 std::string aTestPath = *anIt;
115 CPPUNIT_NS::TestPath aPath = m_suite->resolveTestPath( aTestPath );
116 CPPUNIT_NS::Test* aTest = aPath.getChildTest();
117 theController.runTest( aTest );