Salome HOME
refs #1327: debug of automatic tests
[modules/hydro.git] / src / HYDRO_tests / TestLib_Runner.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <TestLib_Runner.h>
20 #include <cppunit/TestPath.h>
21 #include <cppunit/TestResult.h>
22 #include <algorithm>
23 #include <fstream>
24
25 /**
26   Conversion of the given string to lower-case
27   @param theStr the string to convert to lower-case
28   @return the converted string
29 */
30 std::string ToLower( const std::string& theStr )
31 {
32   std::string aStr = theStr;
33   std::transform( aStr.begin(), aStr.end(), aStr.begin(), ::tolower );
34   return aStr;
35 }
36
37 /**
38   Add a substring pattern to test runner
39   @param theSubstr the substring pattern to add
40 */
41 void TestLib_Runner::Add( const std::string& theSubstr )
42 {
43   mySubStrings.push_back( ToLower( theSubstr ) );
44 }
45
46 /**
47   Load the configuration file with substring patterns
48   @param theConfigFile the path to the configuration file
49   @return if the operation is successful
50 */
51 bool TestLib_Runner::Load( const std::string& theConfigFile )
52 {
53   std::ifstream anInput( theConfigFile.c_str() );
54   bool isOK = anInput.is_open();
55   if( !isOK )
56     return false;
57
58   std::string aLine;
59   while( std::getline( anInput, aLine ) )
60   {
61     int aPos = aLine.find( "//" );
62     if( aPos != std::string::npos )
63       aLine.erase( aPos );
64
65     //trim 
66     aLine.erase( 0, aLine.find_first_not_of( ' ' ) );
67     aLine.erase( aLine.find_last_not_of( ' ' ) + 1 );
68
69     if( !aLine.empty() )
70       Add( aLine );
71   }
72
73   return true;
74 }
75
76 /**
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
80 */
81 bool TestLib_Runner::IsAllowed( const std::string& theTestName ) const
82 {
83   if( mySubStrings.empty() )
84     return true;
85
86   std::string aTestName = ToLower( theTestName );
87
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 )
91       return true;
92
93   return false;
94 }
95
96
97 /**
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
101 */
102 void TestLib_Runner::GetTests( CPPUNIT_NS::Test* theRoot, std::list<std::string>& theAllowedTests ) const
103 {
104   for( int i=0, n=theRoot->getChildTestCount(); i<n; i++ )
105   {
106     CPPUNIT_NS::Test* aTest = theRoot->getChildTestAt( i );
107     std::string aName = aTest->getName();
108     if( IsAllowed( aName ) )
109       theAllowedTests.push_back( aName );
110     else
111       GetTests( aTest, theAllowedTests );
112   }
113 }
114
115 /**
116   Run all necessary tests
117   @param theController the controller of the tests results
118 */
119 void TestLib_Runner::Run( CPPUNIT_NS::TestResult& theController )
120 {
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 )
125   {
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 );
130   }
131 }
132