1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <TestLib_Runner.h>
20 #include <cppunit/TestPath.h>
21 #include <cppunit/TestResult.h>
26 Conversion of the given string to lower-case
27 @param theStr the string to convert to lower-case
28 @return the converted string
30 std::string ToLower( const std::string& theStr )
32 std::string aStr = theStr;
33 std::transform( aStr.begin(), aStr.end(), aStr.begin(), ::tolower );
38 Add a substring pattern to test runner
39 @param theSubstr the substring pattern to add
41 void TestLib_Runner::Add( const std::string& theSubstr )
43 mySubStrings.push_back( ToLower( theSubstr ) );
47 Load the configuration file with substring patterns
48 @param theConfigFile the path to the configuration file
49 @return if the operation is successful
51 bool TestLib_Runner::Load( const std::string& theConfigFile )
53 std::ifstream anInput( theConfigFile.c_str() );
54 bool isOK = anInput.is_open();
59 while( std::getline( anInput, aLine ) )
61 int aPos = aLine.find( "//" );
62 if( aPos != std::string::npos )
66 aLine.erase( 0, aLine.find_first_not_of( ' ' ) );
67 aLine.erase( aLine.find_last_not_of( ' ' ) + 1 );
77 Determine if the given test name is allowed by patterns
78 @param theTestName the test name
79 @return true if the test name is allowed
81 bool TestLib_Runner::IsAllowed( const std::string& theTestName ) const
83 if( mySubStrings.empty() )
86 std::string aTestName = ToLower( theTestName );
88 std::list<std::string>::const_iterator anIt = mySubStrings.begin(), aLast = mySubStrings.end();
89 for( ; anIt!=aLast; ++anIt )
90 if( aTestName.find( *anIt ) != std::string::npos )
98 Determine list of tests allowed by patterns from the given CPPUnit tests tree
99 @param theRoot the root of the CPPUnit tests tree
100 @param theAllowedTests the returned vector of allowed tests names
102 void TestLib_Runner::GetTests( CPPUNIT_NS::Test* theRoot, std::list<std::string>& theAllowedTests ) const
104 for( int i=0, n=theRoot->getChildTestCount(); i<n; i++ )
106 CPPUNIT_NS::Test* aTest = theRoot->getChildTestAt( i );
107 std::string aName = aTest->getName();
108 if( IsAllowed( aName ) )
109 theAllowedTests.push_back( aName );
111 GetTests( aTest, theAllowedTests );
116 Run all necessary tests
117 @param theController the controller of the tests results
119 void TestLib_Runner::Run( CPPUNIT_NS::TestResult& theController )
121 std::list<std::string> aTests;
122 GetTests( m_suite, aTests );
123 std::list<std::string>::const_iterator anIt = aTests.begin(), aLast = aTests.end();
124 for( ; anIt!=aLast; ++anIt )
126 std::string aTestPath = *anIt;
127 CPPUNIT_NS::TestPath aPath = m_suite->resolveTestPath( aTestPath );
128 CPPUNIT_NS::Test* aTest = aPath.getChildTest();
129 theController.runTest( aTest );